プログラムで UITextView を作成する必要がありますが、サイズの設定で問題が発生しました。
複数行の文があるとします。
s = @"Hello World\nHello World\nHello World\n";
次に、表示する必要がある高さを把握し、それに応じて UITextView の高さを設定します (幅を固定する必要があります)。以下は私のコードです:
CGSize size = [text sizeWithFont:_FONT_MYRIADPRO(20.0f) constrainedToSize:CGSizeMake(300, 1000)];
_textBox.text = text;
_textBox.frame = CGRectMake(10, 10, 300, size.height);
_textBox.font = _FONT_MYRIADPRO(20.0f);
_textBox.textColor = _COLOR_BLACK;
_textBox.backgroundColor = _COLOR_BLUE_DARK;
self.frame = CGRectMake(0, 0, 320, _textBox.frame.size.height + 2 * 10);
[self addSubview:_textBox];
ただし、結果は次のようになります。
call のような他の多くの方法を試しました
[_textBox sizeToFit]
幅を固定したいので、これも失敗します。
私も試しました
NSAttributedString *titleText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: _FONT_MYRIADPRO(20.0f)}];
CGRect bound = [titleText boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
_textBox.text = text;
_textBox.frame = CGRectMake(10, 10, 300, bound.size.height);
それも失敗し、結果は上の画像と同じです。
誰でも私を助けることができますか?