【发布时间】:2010-02-25 01:23:32
【问题描述】:
有没有办法获得正确大小的 NSString 使用:
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
不会被 2 或 300 个字符串抛弃。目前,如果我尝试在这些长字符串上使用此方法,则会错误地计算它们,最终在 UITextView 的底部会出现大量空白。
我尝试过使用 UILineBreakModeWordWrap 和 UILineBreakModeCharacterWrap。
正在调整大小
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat result = 44.0f;
NSString* text = nil;
CGFloat width = 0;
CGFloat tableViewWidth;
CGRect bounds = [UIScreen mainScreen].bounds;
tableViewWidth = bounds.size.width;
width = tableViewWidth - 150;
text = stringWithLongWords;
if (text) {
CGSize textSize = { width, 20000.0f };
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:10.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 50.0f;
result = MAX(size.height, 44.0f+30.0f);
}
return result;
}
【问题讨论】:
-
你到底想做什么?我认为 sizeWithFont: 效果很好,也许这就是您将这些信息应用于 UITextView 的方式。请解释一下。
-
我有一个自定义 UITableView 单元格,其中有 2 个 UILabel 和一个 UITextView,使用 sizeWithFont 来计算单元格高度所需的大小,有时单元格最终会超过 400 像素。
标签: iphone objective-c nsstring uitextfield