この回答で提供されているコードを使用して動的ラベルを作成していますが、ほとんどの場合機能します。ただし、ラベルテキストの長さが94文字を超えると、切り捨てられて省略記号が追加されます。
これについてもう1つ奇妙なことは、文字列にさらに文字を追加すると表示されますが、最後の2行はまだ切り捨てられていることです。
例えば。
文字列:
this is a very very long string
with lots of words to test the
dynamic bubble sizing one two three.
このように表示されます:
this is a very very long string
with lots of words to test the
dynamic bubble sizing one tw...
しかし、ラベルで同じ文を再度使用して文字列を2倍にすると、テキストがより多く表示されますが、それでも切り捨てられます。
例えば。
文字列:
this is a very very long string
with lots of words to test the
dynamic bubble sizing one two
three. this is a very very long
string with lots of words to test
the dynamic bubble sizing one
two three.
このようなショー:
this is a very very long string
with lots of words to test the
dynamic bubble sizing one two
three. this is a very very long
string with lots of words to tes...
これが私が使用しているコードです。
NSString *temp = [NSString stringWithFormat:@"this is a very very long string with lots of words to test the dynamic bubble sizing one two three"];
captionLabel.text = temp;
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [temp sizeWithFont:captionLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:captionLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = captionLabel.frame;
newFrame.size.height = expectedLabelSize.height;
captionLabel.frame = newFrame;
これは私が私の頭を掻くので、誰かがアイデアを持っていることを願っています。
編集
ハードコードされた296の代わりにcaptionLabel.frame.size.widthを使用すると、@ trooleeのおかげで修正されました。彼/彼女が回答を作成することを選択した場合、私はそれを正しいとマークします。