Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UILabel - non breaking space

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?

like image 957
Marcin Avatar asked Feb 24 '11 13:02

Marcin


People also ask

How do you insert a nonbreaking space?

Place your cursor where the nonbreaking space should be inserted. Alternately, if a regular space already appears where the nonbreaking space should be inserted, select the regular space (see figure 1). 2. Select Ctrl + Shift + Space on your keyboard to insert the nonbreaking space.

What is non-breaking space character?

Non-breaking spaces, also known as no-break, non-breakable, hard or fixed spaces, are characters that look exactly like regular spaces. You cannot see the difference between a non-breaking space and a regular space either on the page or on most screens.


2 Answers

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

like image 108
Robby Valles Avatar answered Oct 23 '22 23:10

Robby Valles


For Swift:

let sentence = "Barcelona, Real Madryt, Juventus Turyn, Bayern Monachium" let sentencewithnbsp = String(map(sentence.generate()) {     $0 == " " ? "\u{00a0}" : $0 }) 
like image 32
Bartłomiej Semańczyk Avatar answered Oct 23 '22 23:10

Bartłomiej Semańczyk