简体   繁体   中英

Word Wrap Line Break not working in UILabel only for specific text Strings

I have a UITableViewController with a custom prototype cell with three labels and one button. The middle label (called descriptionLabel ) is a multi-line label with Word Wrap as the "Line Breaks" option and 0 lines set in the Attributes inspector.

If I set the descriptionLabel.text property assigning a literal String , the Word Wrap option works fine, as shown in the following image:

Cell with Word Wrap working

The problem is that the descriptionLabel 's text comes from an object. This object is downloaded from a web service and is stored locally, and it's also used to fill all the others labels of the cell.

When I assign the object-property String to the descriptionLabel 's text ( cell.descriptionLabel.text = monthlyPaymentType.detail ), however, the Word Wrap is not working, as shown in the image below:

Cell with Word Wrap NOT working

In this case, "cobrado" is one word, but it is being character-wrapped instead of word-wrapped.

It's strange because If I copy the result of print(monthlyPaymentType.detail) , paste as a literal String and assign it to cell.descriptionLabel.text , the Word Wrap option also does not work.

But if I manually type a literal String with exactly the same content of print(monthlyPaymentType.detail) and assign it to cell.descriptionLabel.text , the Word Wrap option does work.

Am I doing anything wrong when configuring the Label? Is it any problem with the stored String in my object?

The problem is that the whitespaces in the Strings that come from the server and are stored in the objects are Unicode Non-Breaking Spaces (U+00A0). When these Strings are assigned to the UILabel text property, Non-Breaking Spaces are not considered as word separators.

To solve this problem, I replace the occurrences of U+00A0 characters with common Unicode whitespace (U+0020), using String 's method stringByReplacingOccurrencesOfString("\\u{00A0}", withString: "\\u{0020}") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM