如何知道UILabel中显示的文字?
我有一个包含两个 UILabels 的 UIView,用于显示字符串。 第一个 UILabel
有固定的大小,如果字符串太长,无法容纳在这个 UILabel
中,我想在第一个中显示最大字符数UILabel
,并在第二个 UILabel
中显示字符串的其余部分。
但要做到这一点,我必须知道第一个 UILabel
中显示的字符串的确切部分,这并不容易,因为字符串和换行符的随机性。
那么,有没有办法只获取第一个 UILabel
中显示的文本,而无需截断字符串部分?
I have an UIView
containing two UILabels
, in order to display a string.
The first UILabel
has a fixed size, and if the string is too long and can't hold in this UILabel
, I want to display the maximum characters I can in the first UILabel
, and display the rest of the string in the second UILabel
.
But to make this, I must know the exact part of the string displayed in the first UILabel
, which is not easy because of the randomness of the string and the linebreaks.
So, is there a way to get just the text displayed in the first UILabel
, without the truncated part of the string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这是一个迟到的答案,但也许它可以帮助某人。上面的代码大致是我在我的应用程序中使用的解决方案。
_descTop 是我的第一个标签,_descBottom 是第二个标签。 270 是一个常量,相当于略小于我的第一个标签 _descTop 中显示的平均最大字符数。我手动计算了它,尝试了许多不同的字符串,也许有更好的方法可以做到这一点,但这效果还不错。
如果我要显示的字符串 (_infoMedia.description) 大于 270 个字符,在 270 个字符限制的情况下,我将隔离前 270 个字符加上字符串中下一个单词的末尾(通过搜索下一个空格)会在单词中间切断字符串。然后我将字符串的第一部分放入第一个标签中,将第二部分放入第二个标签中。
如果没有,我只将字符串的全局性放在第一个标签中。
我知道这是一个蹩脚的解决方案,但它确实有效,而且我没有找到更好的方法来做到这一点。
Okay that's a late answer but maybe it could help someone. The code above is approximatively the solution I used in my app.
_descTop is my first label and _descBottom is the second label. 270 is a constant equivalent to a little less than the average maximum number of characters displayed in my first label, _descTop. I calculated it by hand, trying with many different strings, maybe there's a better way to do that but this worked not bad.
If the string I want to display (_infoMedia.description) is larger than 270 characters, I isolate the first 270 characters plus the end of the next word in the string (by searching the next space), in the case where the 270 characters limit would cut the string in the middle of a word. Then I put the first part of the string in my first label, and the second part in the second label.
If not, I only put the globality of the string in the first label.
I know that's a crappy solution, but it worked and I didn't found any better way to do that.
以下代码可能会帮助您获得您想要的东西!
Following code might help you in getting what you want!!