我需要計算UItextview的高度。但文字視圖有新行(\ n)字符表示,其不正確計算。 例如:如何使用換行符( n)獲取UITextView的內容高度?
測試 \ n Lorem存有簡直是的\ n 虛擬文本打印 \ n 和排版行業
如何衡量與新行字符UITextView的內容高度?
我需要計算UItextview的高度。但文字視圖有新行(\ n)字符表示,其不正確計算。 例如:如何使用換行符( n)獲取UITextView的內容高度?
測試 \ n Lorem存有簡直是的\ n 虛擬文本打印 \ n 和排版行業
如何衡量與新行字符UITextView的內容高度?
高度只需使用下面的代碼,可以幫助你。
NSString *strtest [email protected]"Test \n Lorem Ipsum is simply \n dummy text of the printing \n and typesetting industry";
yourtextView.text = strtest;
CGRect framefortext = _textView.frame;
framefortext.size.height = yourtextView.contentSize.height;
yourtextView.frame = framefortext;
您可以使用NSPtring的componentsSeparatedBy:
方法和sizeWithFont:
方法。
使用sizeWithFont方法獲取所有組件的尺寸,然後總結所有高度 - 它將得到高度,並選擇最大寬度 - 它將是結果寬度。
f.e.
- (CGSize)sizeFromString:(NSString *)string {
NSArray *componentsArray = [string componentsSeparatedBy:@"n\\"];
CGFloat resultHeight = 0.f;
CGFloat resultWidth = 0.f;
for (NSString *component in componentsArray) {
CGSize componentSize = [component sizeWithAttributes: ....]; // some attributes
resultHeight += componentSize.height;
componentWidth = componentSize.width;
if (componentWidth > resultWidth) {
resultWidth = componentWidth
}
}
return CGSizeMake(resultWidth, resultHeight);
}
也許你也應該總結的「垂直墊襯」(文本行之間的間隔)
我使用這個代碼,以查找並設置標籤的準確高度,希望它可以幫助你太
[label setNumberOfLines:0];
[label sizeToFit];
int heightofbottom = label.frame.size.height;
NSLog(@"%d",heightofbottom);
它不是在文本視圖中的「\ n」個字符的工作。它使用換行符文本嗎? – Mani
是的顯然這是工作,看到我編輯的代碼 –
是的。它的工作正常。非常感謝。 – Mani