在我的问题" 我如何-[NSString sizeWithFont:forWidth:lineBreakMode:]开始工作? "中,我了解到这-[NSString sizeWithFont:constrainedToSize:lineBreakMode:]实际上就是我所需要的.
-[NSString sizeWithFont:forWidth:lineBreakMode:]州的文档解释说它实际上并没有将文本包装到其他行.那我该如何使用呢?(例子会有所帮助.)
我更新了我的问题" 调整UILabel(在iPhone SDK中)以适应? ",并在建议的解决方案中描述了我的问题,但没有得到答案.也许我会通过提出一个新问题来获得更好的结果......
我在以下代码后设置了一个断点:
NSString *theText = @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.";
CGSize …Run Code Online (Sandbox Code Playgroud) 有没有办法使用以下方法获得正确的NSString大小:
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
Run Code Online (Sandbox Code Playgroud)
不会被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;
}
Run Code Online (Sandbox Code Playgroud)