简体   繁体   中英

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?

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

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

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

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

For Swift:

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

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.

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}

Here's a Swift extension to fix an orphan in a string by replacing the last space with a non-breaking space:

extension String {
    var replacingLastSpace: String {
        if let lastSpace = range(of: " ", options: .backwards, locale: .current) {
            return replacingCharacters(in: lastSpace, with: "\u{00a0}")
        }
        return self
    }
}

Although note as of iOS 11, UILabels solve this problem automatically .

在 Inspector 中,将 Label 的行数设置为 3 或 4 你需要什么然后内容将显示在多行中。

You may need to implement a custom word-wrapping function.

// pseudo-code
instring;

get len(instring)
if len > textedit.width*charFontSize
    for (textedit.width*charFontSize ) // cycle through string
        insert `\n` into inString at shortest whitespace

or something like that.

I don't think there's a simple way to do this with UILabel. Of course one way to achieve the same effect is to manually insert a "\\n" before " text" in your example. Another option is to use a UIWebView with static HTML instead of the UILabel, so you can use an actual &nbsp .

If this is not going to happen often, you can do this:

NSString *string = @"lorem ipsum some \ntext 1";

label.text = string;

You can dynamically generate where you put the \\n using character counts, word counts etc...

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