Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert Non-breaking space to NSString or NSMutableAttributedString

How to insert Non-breaking space (like " " in html) to NSString or NSMutableAttributedString ?

like image 714
Denis S. Avatar asked Oct 02 '13 07:10

Denis S.


People also ask

How do you add a space to an attributed string in Swift?

Use \u{2000} in swift. (Probably \u2000 in Objective-C.) In the answer above, the spaces are wide ones, you can copy and paste them.

What is an NSAttributedString?

A string with associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.


2 Answers

As XCode treats all source code files as UTF-8 encoded text files, it should be possible to directly paste the non-breaking space into your source code:

NSString *text = @"100 feet long";  // non-breaking space between "100" and "feet"

To be on the safe side and make it more obvious, you can insert the Unicode value U+00A0 into the string:

NSString *text = @"100\u00a0feet long";
like image 87
Codo Avatar answered Oct 05 '22 00:10

Codo


Swift code:

let text = "100\u{00A0}feet long"
like image 30
Display Name Avatar answered Oct 05 '22 01:10

Display Name