[英]Finding the number of lines in UITextView
我正在使用UIScrollView
,其中放置了UIImageView
和UITextView
。 我使UIScrollView
可以滚动图像和文本,并且效果很好,但是我的UITextView
包含动态文本(即每次的行数都不相同)。 所以我找不到分配UITextView
contentSize的方法。 有什么办法吗?
使用以下代码计算文本宽度并定义您的文本视图宽度
+(float) calculateHeightOfTextFromWidth:(NSString *) text: (UIFont *)withFont: (float)width
:(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(215, 1000) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
并在您希望将动态文本显示为
float titleHeight;
titleHeight = [Your view controllre ViewController calculateHeightOfTextFromWidth:[NSString stringWithFormat:@"%@",[dict objectForKey:@"title"]] :[UIFont systemFontOfSize:14]:300 :UILineBreakModeTailTruncation];
将此titleHeight赋予textview或使用titleheight进行计算除法,您可以获得行数
如果您遇到问题,请回复
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.