[英]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.