Tre*_*vor 3 iphone text sizewithfont
我有以下代码:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *currentMessage = [FeedMessages objectAtIndex:indexPath.row];
NSLog(currentMessage);
UIFont *font = [UIFont systemFontOfSize:14];
NSLog([NSString stringWithFormat:@"Height: %@",[currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap].height]);
return [currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap].height;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我为什么"[currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap] .height"总是返回null或nil?
我检查过并正确填充了currentMessage.
有任何想法吗?
回答这个问题.它描述了 - 您必须使用CGFLOAT_MAX或查看以下代码(从那里抓取).
NSString *text = @"A really long string in here";
CGSize theSize = [text sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSString *stringHeight = [NSString stringWithFormat:@"%f", theSize.height];
Run Code Online (Sandbox Code Playgroud)