Bri*_*kel 14 nsattributedstring ios
我正在开发一个UILabel具有大型主文本的文本,后跟较小的文本,告诉您是谁说的:

现在它基本上是NSAttributedString小文本上的字体属性.
我想把事情做好,所以大文字包好,但小文字没有.也就是说,如果文本将与正确的项目中的所有内容相同,则它应该按原样呈现,但是它会像在左侧项目中一样包裹,整个小文本应该出现在下一行:

与我想要实现的HTML相当的是:
Title <nobr>Subtitle</nobr>
- or -
Title <span style="white-space:nowrap">Subtitle</span>
Run Code Online (Sandbox Code Playgroud)
我已经尝试将这两个转换为NSAttributedStrings,NSHTMLTextDocumentType它似乎没有直接翻译.
Bri*_*kel 24
根据rmaddy的建议,我能够通过用不破坏的替代方案替换空格和破折号来获得我想要的效果:
Objective-C的:
NS_INLINE NSString *NOBR(NSString *string) {
return [[string stringByReplacingOccurrencesOfString:@" " withString:@"\u00a0"]
stringByReplacingOccurrencesOfString:@"-" withString:@"\u2011"];
}
NSAttributedString *username = [[NSAttributedString alloc]
initWithString:NOBR(hotQuestion.username) attributes:nil];
...
Run Code Online (Sandbox Code Playgroud)
Swift(注意略有不同的转义码格式):
func nobr(_ string:String) -> String {
return string
.stringByReplacingOccurrencesOfString(" ", withString: "\u{a0}")
.stringByReplacingOccurrencesOfString("-", withString: "\u{2011}")
}
let username = NSAttributedString(string:nobr(hotQuestion.username, attributes:nil))
Run Code Online (Sandbox Code Playgroud)
Pet*_*trV 13
Unicode中还有word-joiner\u2060字符,它可以防止任何一方的换行而且是不可见的.当程度符号是单词的一部分时,我用它强制自动换行,所以在iOS中整个单词将保持在同一行.
Objective-C的:
text = [text stringByReplacingOccurrencesOfString:@"°" withString:@"\u2060°\u2060"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3491 次 |
| 最近记录: |