2013-10-09 51 views

回答

4

高度只需使用下面的代碼,可以幫助你。

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; 
+0

它不是在文本視圖中的「\ n」個字符的工作。它使用換行符文本嗎? – Mani

+0

是的顯然這是工作,看到我編輯的代碼 –

+0

是的。它的工作正常。非常感謝。 – Mani

0

您可以使用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); 
} 

也許你也應該總結的「垂直墊襯」(文本行之間的間隔)

1

我使用這個代碼,以查找並設置標籤的準確高度,希望它可以幫助你太

[label setNumberOfLines:0]; 
[label sizeToFit]; 
int heightofbottom = label.frame.size.height; 
NSLog(@"%d",heightofbottom); 
相關問題