iPhone UILabel - non breaking space

2020-02-26 03:13发布

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?

9条回答
地球回转人心会变
2楼-- · 2020-02-26 03:43

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-02-26 03:46

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.

查看更多
Juvenile、少年°
4楼-- · 2020-02-26 03:49

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.

查看更多
登录 后发表回答