繁体   English   中英

在UITextView中查找行数

[英]Finding the number of lines in UITextView

我正在使用UIScrollView ,其中放置了UIImageViewUITextView 我使UIScrollView可以滚动图像和文本,并且效果很好,但是我的UITextView包含动态文本(即每次的行数都不相同)。 所以我找不到分配UITextView contentSize的方法。 有什么办法吗?

可以帮助你。
您可以设置一个条件,如果新CGSize的宽度大于textview的宽度,则行数= 2否则为1。

使用以下代码计算文本宽度并定义您的文本视图宽度

+(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM