ios – 动态UILabel截断文本

ios – 动态UILabel截断文本,第1张

概述我正在使用 this answer中提供的代码来创建动态标签,它在大多数情况下都有效.但是每当标签文本的长度超过94个字符时,它就会被截断并添加省略号. 还有一个奇怪的事情是,如果我在字符串中添加更多字符,它们会被显示,但最后2行仍然被截断. 例如. 字符串: this is a very very long string with lots of words to test the dyna 我正在使用 this answer中提供的代码来创建动态标签,它在大多数情况下都有效.但是每当标签文本的长度超过94个字符时,它就会被截断并添加省略号.

还有一个奇怪的事情是,如果我在字符串中添加更多字符,它们会被显示,但最后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 labelCGSize 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,如果他/她选择创建答案,我会将其标记为正确.

解决方法
Nsstring * test=@"this is test this is test inthis is test ininthis is test inthis is test inthis is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel END";        testLabel.text = test;        testLabel.numberOflines = 0; //will wrap text in new line        [testLabel sizetoFit];        [self.vIEw addSubvIEw:testLabel];

这肯定会对你有所帮助.

谢谢

总结

以上是内存溢出为你收集整理的ios – 动态UILabel截断文本全部内容,希望文章能够帮你解决ios – 动态UILabel截断文本所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1038286.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存