如何限制Uilabel的最大字符
我想限制uilabel.text中的8个中文单词或16个英语单词,如果文本大于此,请使用“ ...”来替换多余的位置。
我试图计算字符串大小,但我不知道如何处理英语单词和中文单词。因为我用utf8来计算字节,所以中文单词比英语单词三重。 (考虑到标签可能具有中文单词和英语单词,这很难计算。)
例如:
1。应该看起来像“蝙蝠侠一样的猫...”
2。
> 3。“蝙蝠侠喜欢猫女和阿福”应该看起来像“蝙蝠侠喜欢猫女和...”
我知道Android有一个财产可以做到这一点,Swift呢?
寻找您的回复,并为我的英语描述不佳而感到抱歉。
I want to limit UILabel.text in 8 Chinese word or 16 English word, and if the text is larger than this, use "..." to replace the redundant place.
I tried to count String size, but I don't know how to deal with both English words and Chinese words. Because is I count bytes by utf8, the Chinese word would be triple than English words. (Considering the label might have both Chinese word and English words, it's hard to calculate.)
for example:
1."Batman like cat Woman and Alfred." should look like "Batman like cat..."
2."蝙蝠侠喜欢猫女和阿福" should look like "蝙蝠侠喜欢猫女和..."
3."Batman喜欢猫女和阿福" should look like "Batman喜欢猫女和..."
I know Android has a property to do this, how about Swift?
Lookingforward for your response, and sorry for my poor English description.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以迭代字符串索引计算字符,如果是中文字符等于16,将子字符串返回到当前索引,并在末尾使用“…”返回。像:
You can iterate your string indices counting the characters, if it is a chinese character add 2 otherwise add 1. If the count is equal to 16 return the substring up to the current index with "…" at the end. Something like: