Good day! The question is: Suppose I have a label that has a width. It happens that when the label's text is larger than its width, the label looks like “BlaBlab ...”, and I would like it to look like “BlaBla..la”. How can I achieve this? Is there any ready-made solution?
1 answer
In IB select your UILabel , then Attributes inspector -> Line Break -> Truncate Middle
It is possible and through the code. Swift 3 example:
label.lineBreakMode = .byTruncatingMiddle; - Fine! It is almost what you need. And tell me a little more, please. I need two characters to end - Dmitriy Greh
- I think that you need to find first how many letters the label contains and then everything will be just. - Dmitriy Greh
- This function puts a three-dot in the center of the line. If you want exactly two characters at the end, you have to write it manually. As an idea, you can create a dictionary where the key will be the original line and enter a new line in the value. You need to consider how many characters fit into the UILabel, then take the range and replace the 3rd, 4th and 5th letters from the end with three dots, and you will get the desired effect. - Vitali Eller
- Yes, I understand that. I’m trying to find out how to find how many characters fit into my UILabel, but it still doesn’t work. a lot of information on obj-c and almost nothing on swift :( - Dmitriy Greh
- Check out this link, maybe it will help you: gist.github.com/ViccAlexander/0224ab078f76a3af6d79986369d5240b - Vitali Eller
|