我使用this answer中提供的代码来创建动态标签,并且它在大多数情况下都能正常工作。但是,只要标签文本长度超过94个字符,它就会被截断并省略“。动态UILabel截断文本
还有一件更奇怪的事情是,如果我向字符串添加更多字符,它们会显示,但最后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...
但是当我通过在标签再次使用同一个句子一倍字符串时,它表现出更多的德文本,但仍将截断它。
例如,
字符串:
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;
希望有人有一个想法,因为这让我挠了挠头。
编辑
使用captionLabel.frame.size.width而不是硬编码的296修好了,感谢@troolee,如果他/她选择创建一个答案,我将迎来它是正确的。
看看这个! HTTP://计算器。com/questions/9059631/autoshrink-on-a-uilabel-with-multiple-lines/9060833#9060833 – 2012-07-19 10:35:23
如果您将使用captionLabel.frame.size.width而不是硬编码的'296',该怎么办?也许标签的宽度不是'296'? – 2012-07-19 10:43:06
captionLabel.frame.size.width修复了它,并将其作为答案粘贴,并将其标记为正确。似乎再次我已经被一些复制面食代码... – triggs 2012-07-19 10:46:43