boundingRectWithSize does not respect word wrapping

隐身守侯 提交于 2019-12-05 01:40:09
JulianSymes

You can tell boundingRectWithSize to process the string in word-wrapping mode. You have to add an NSParagraphStyle attribute to the attributes parameter, with its lineBreakMode set to NSLineBreakByWordWrapping. So:

NSMutableDictionary *attr = [NSMutableDictionary dictionary];     
// ...whatever other attributes you need...
NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paraStyle.lineBreakMode = NSLineBreakByWordWrapping;
[attr setObject:paraStyle forKey:NSParagraphStyleAttributeName];

then use attr as the attributes argument to boundingRectWithSize.

You can easily extend/generalise this technique to read other attributes including existing paragraph style attributes from whatever source makes sense.

Should use (NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) for options parameter.

vqdave

Did some more research and ended up finding this.

CGSize textSize = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, FLT_MAX)];
CGFloat textHeight = textSize.height;

Hope this helps someone out there!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!