I have a UILabel with 3 lines set to word wrap. I also set the label to have a width of 180. While this works in almost all circumstances, some languages with very long words are starting to character wrap because their length is greater than 180. Ideally, I would have the label keep the width of 180 in all cases, except if a word is too long to fit. Then, I would like to expand the width to be the minimum size to keep the longest word in tact. Any advice on how to do that?
let myString = "thisisaveryveryveryveryverylongstring"
let myLabel = UILabel()
myLabel.text = myString
mylabel.numberOfLines = 3
myLabel.lineBreakMode = .byWordWrapping
myLabel.widthAnchor.constraint(equalToConstant: 180).isActive = true
I’ve tried setting a preferredWidth as well as setting the width constraint to have a priority of less than 1000, but in both circumstances the longer word still character wraps.
2
Answers
To achieve the desired behavior of having a UILabel with a maximum of three lines, auto-shrinking the text to fit within those lines, and dynamically adjusting the number of lines, you can follow these steps:
swift
let myString = "thisisaveryveryveryveryverylongstring" let myLabel = UILabel() myLabel.text = myString myLabel.numberOfLines = 0 // Allow dynamic number of lines myLabel.lineBreakMode = .byWordWrapping myLabel.translatesAutoresizingMaskIntoConstraints = false // Ensure Auto Layout is enabled
swift
let widthConstraint = myLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 180) widthConstraint.priority = .required widthConstraint.isActive = true
swift
myLabel.minimumScaleFactor = 0.5 // Adjust as needed myLabel.adjustsFontSizeToFitWidth = true
With these settings, the label will automatically adjust its font size to fit within the specified width (180) and ensure the text remains within a maximum of three lines. If the text is too long to fit, it will shrink the font size while keeping it within three lines. Setting numberOfLines to 0 allows the label to dynamically adjust the number of lines based on the content.
You can fine-tune the minimumScaleFactor value to control the extent to which the font size can shrink while auto-fitting the text within the label’s frame. This approach provides a flexible and responsive solution for your UILabel requirements.
It’s not entirely clear what you are trying to do, but I’m going to guess it’s like this…
Consider the different word lengths here:
Using the first example, if the string is: "Some Occupation Irgendeine Beschäftigung" and the label width is constrained to 180-points, it will look like this:
Which, I’m guessing, is the first part of your goal — 180-point label frame width.
However, using the second example, if the string is: "Occupational Therapist Beschäftigungstherapeutin" and the label width is constrained to 180-points, it will look like this:
The single word "Beschäftigungstherapeutin" by itself is too wide, and we get character wrapping.
So, we need to get to here:
The label width is now 206-points.
To accomplish that, we can split the string into individual words and find the widest single word:
Here’s an example you can try:
Please note: this is meant to be a starting point. You would probably want to implement a "max width" so your label doesn’t extend wider than the screen (or into another view). Also, I did very little testing of this… so it should not be considered "Production Ready"