如题
比如
这个UIlabel 本该显示成
顶顶顶顶顶顶顶顶顶顶的大多数是是是是
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
谁谁谁水水水水是是是是是是是是是是撒
大多数是山东省撒打算打算打算打算的收
水电费第三方第三方说的发送到发送到范
水电费第三方士大夫士大夫。
但是 现在业务要求最多显示3行 如果小于3行有多少航显示多少行 超过三行的话显示如下
顶顶顶顶顶顶顶顶顶顶的大多数是是是是
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
谁谁谁水水水水是是是是是是是...展开
其中展开是蓝色 一点击...展开才变成
顶顶顶顶顶顶顶顶顶顶的大多数是是是是
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
谁谁谁水水水水是是是是是是是是是是撒
大多数是山东省撒打算打算打算打算的收
水电费第三方第三方说的发送到发送到范
水电费第三方士大夫士大夫。
我现在虽然到达了效果 但是方法比较复杂 且有时候还有问题 我想想问问大家 有没有什么好办法 好思路 最好有代码可以参考 谢谢了
The limit on the number of characters upstairs is not practical. Chinese characters have the same width, but the width of other characters is inconsistent, such as 1 and 8, one and 1, i and u, W and I...
This question needs to be used CoreText, manually change the typesetting. There is this kind of AttributeLabel on the Internet. First download the code and take a look. It is difficult to write it completely by hand. Let me give you a look at the code that we downloaded online before the project and modified it by ourselves. It is a bit similar to your needs. He This is just added... For reference, the following method is the method of customizing label
This requirement is indeed a bit difficult. If it were me, I might use two alternative methods:
Instead of limiting it to 3 lines, it limits the number of words to no more than 3 lines, such as 50 words. Then the string is truncated, spelled with "...", and then "expanded". Use
TTTAttributedlabel
to add a click event . The number of words can also be determined individually based on several screen widths.Move the "Expand" outside the label range, limit the numberOfLines of the label to 3, and then "Expand" is a separate button, put it outside...
If you have to implement this... The way I can think of is to scan the string from beginning to end in a range that is probably 3 lines, such as from 40 words to 60 words, first subString and then calculate the height of boundingRect , until you find that it becomes 4 lines. The previous text fills exactly 3 lines, cut off a few more words, spell "...", and then spell "Expand". But the method of calculating boundingRect is relatively slow, so it is definitely very inefficient. Not very recommended.
How did you achieve it?
I am also looking for this control