在 UILabel 中设置自定义单词边界
Set custom word boundaries in UILabel
我在 UILabel 上显示多行 NSAttributedString,我遇到换行问题。换行以加号 ('+') 结尾的单词时,UILabel 会在 '+' 号之前换行。
我尝试了所有可用的 lineBreakMode,但无论我做什么,如果该行的最后一个单词以“+”结尾,它会在它之前中断。
例如,使用文本 "My name is Fred and C++ is my language"
UILabel 将呈现为两行,如下所示:
"My name is Fred and C"
"++ is my language"
这篇关于 Apple 文档的文章 (link) 说:
The text system determines word boundaries in a language-specific manner according to Unicode Standard Annex #29 with additional customization for locale as described in that document. On OS X, Cocoa presents APIs related to word boundaries, such as the NSAttributedString methods doubleClickAtIndex: and nextWordFromIndex:forward:, but you cannot modify the way the word-boundary algorithms themselves work.
有什么想法吗?
在 C++
中的每个可见字符之间放置一个 Unicode U+2060 WORD JOINER。您可以在字符串文字中使用 \u2060
,也可以使用 Unicode 十六进制输入键盘将其键入 ⌥2060.
我在 UILabel 上显示多行 NSAttributedString,我遇到换行问题。换行以加号 ('+') 结尾的单词时,UILabel 会在 '+' 号之前换行。
我尝试了所有可用的 lineBreakMode,但无论我做什么,如果该行的最后一个单词以“+”结尾,它会在它之前中断。
例如,使用文本 "My name is Fred and C++ is my language"
UILabel 将呈现为两行,如下所示:
"My name is Fred and C"
"++ is my language"
这篇关于 Apple 文档的文章 (link) 说:
The text system determines word boundaries in a language-specific manner according to Unicode Standard Annex #29 with additional customization for locale as described in that document. On OS X, Cocoa presents APIs related to word boundaries, such as the NSAttributedString methods doubleClickAtIndex: and nextWordFromIndex:forward:, but you cannot modify the way the word-boundary algorithms themselves work.
有什么想法吗?
在 C++
中的每个可见字符之间放置一个 Unicode U+2060 WORD JOINER。您可以在字符串文字中使用 \u2060
,也可以使用 Unicode 十六进制输入键盘将其键入 ⌥2060.