簡體   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