3

UItextview の高さを計算する必要があります。しかし、改行 (\n) 文字を含むテキスト ビュー、正しく計算されないことを意味します。元 :

テスト\n Lorem Ipsum は単に\n 印刷および植字業界の \nダミーテキストです

改行文字で UITextView コンテンツの高さを測定する方法は?

4

3 に答える 3

4

以下のコードを使用するだけで役立つ場合があります。

NSString *strtest =@"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;
于 2013-10-09T12:18:10.750 に答える
1

このコードを使用して、ラベルの正確な高さを見つけて設定しています。あなたにも役立つことを願っています

[label setNumberOfLines:0];
[label sizeToFit];
int heightofbottom = label.frame.size.height;
NSLog(@"%d",heightofbottom);
于 2013-10-09T12:32:54.467 に答える
0

componentsSeparatedBy:メソッドとsizeWithFont:NSString のメソッドを使用できます。

sizeWithFont メソッドですべてのコンポーネントのサイズを取得し、すべての高さを要約します。これが結果の高さになり、最大の幅を選択すると、結果の幅になります。

フェ

- (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);
}

「垂直パディング」(テキスト行間の間隔)の高さも要約する必要があるかもしれません

于 2013-10-09T12:12:50.350 に答える