iPhone UILabel - non breaking space

18,952

Solution 1

Use the no-break space (\u00a0) ex: @"hello**\u00a0**world!"

post.text = [postText stringByAppendingString: @"1\u00a0hour\u00a0ago."];

U+00A0 / no-break space / Common Separator, space

from: http://en.wikipedia.org/wiki/Whitespace_character

Solution 2

For Swift:

let sentence = "Barcelona, Real Madryt, Juventus Turyn, Bayern Monachium"
let sentencewithnbsp = String(map(sentence.generate()) {
    $0 == " " ? "\u{00a0}" : $0
})

Solution 3

In Swift 4 I had to use all caps: \U00A0

Example:

lorem ipsum some\U00A0text 1

Update Feb 2020 from the comments. Thanks to @corban:

In the Localizable.strings file it still needs to be \U00A0 - in code you have to use \u{00a0}

Solution 4

If you want to get this to work in a .strings file, you can type OPTION + SPACE instead of a regular space.

.strings files are usually UTF-8 encoded, so if you open it in Hex view, you will see "C2 A0" in place of the special space character.

Solution 5

In Inspector set number of lines for Label as 3 or 4 What ever you require Then the Content will be displayed in multiple lines.

Share:
18,952
Marcin
Author by

Marcin

Updated on June 23, 2022

Comments

  • Marcin
    Marcin about 2 years

    Is there a way to use non breaking spaces in UILabel text?

    For example, I have label with 2 lines and line breaking mode set to word wrap. The content for this label is read from database, where it's stored as a string. Now sometimes my text in label looks like that:

    lorem ipsum some text
    1
    

    but I want to display it like that:

    lorem ipsum some
    text 1
    

    so basicly, I need to force non breaking space between 'text' and '1'.

    I've found some solution here, but I think it could work when the text is entered in source code file. In my case the text is in database.

    Any suggestions?

  • Marcin
    Marcin over 13 years
    The number of lines is set to 5. That's not a problem. As you can see the text is displayed anyway in 2 lines, in my first and second example. The problem is that I don't want those separated '1' in another line.
  • SNR
    SNR over 13 years
    Then set number of lines as 1 and in "adjust to fit" decrease the font size.
  • Marcin
    Marcin over 13 years
    That's not a good option for me, because I want fixed font sizes (for example, to have the same font size as in other labels, with different texts).
  • Marcin
    Marcin over 13 years
    Finally, I've done this by inserting "\n" in my texts. Not exactly what I've tried to do, but anyway, works.
  • Adam Sharp
    Adam Sharp about 11 years
    To get this to work in my Localizable.strings file, I had to use all caps: \U00A0.
  • zvonicek
    zvonicek about 8 years
    It's also possible to use \u{00a0} directly in string without using the map function – lorem\u{00a0}ipsum
  • Jon Hocking
    Jon Hocking about 7 years
    Thanks for the option+space tip - I didn't know this worked for anything other than new lines!
  • Justin Vallely
    Justin Vallely over 5 years
    If you use Localizable.strings, the Unicode value should be uppercase: hello\U00A0world
  • corban
    corban over 4 years
    In the Localizable.strings file it still needs to be \U00A0 - in code you have to use \u{00a0}.
  • Slaknation
    Slaknation over 4 years
    It should be: lorem ipsum some text\u{00a0}1 for swift. The \u{00a0} needs to be between the words you don't want to break.