Preventing line breaks in part of an NSAttributedString

拟墨画扇 提交于 2020-01-10 10:12:16

问题


I am working on a UILabel which features large main text followed by smaller text that tells you who said it:

Right now it is basically an NSAttributedString with a font attribute on the small text.

I would like to set things up so the big text wraps but the small text doesn't. I.e., If the text will all fit on the same line as it does in the right item, it should render as is, but it it would wrap like in the left item, the whole of the small text should appear on the next line:

The HTML equivalent of what I'm trying to achieve is:

Title <nobr>Subtitle</nobr>
- or -
Title <span style="white-space:nowrap">Subtitle</span>

I've tried converting both of these to NSAttributedStrings with NSHTMLTextDocumentType and it doesn't appear to do a direct translation.


回答1:


Following rmaddy's suggestion, I was able to get the effect I wanted by replacing spaces and dashes with their non-breaking alternatives:

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];
...

Swift (note the slightly different escape code format):

func nobr(_ string:String) -> String {
    return string
        .stringByReplacingOccurrencesOfString(" ", withString: "\u{a0}")
        .stringByReplacingOccurrencesOfString("-", withString: "\u{2011}")
}

let username = NSAttributedString(string:nobr(hotQuestion.username, attributes:nil))



回答2:


There is also word-joiner \u2060 character in Unicode which will prevent line break on its either side and is invisible. I used it to force word wrap when degree sign was part of word, so the whole word will stay on the same line, in iOS.

Objective-C:

text = [text stringByReplacingOccurrencesOfString:@"°" withString:@"\u2060°\u2060"];


来源:https://stackoverflow.com/questions/25393231/preventing-line-breaks-in-part-of-an-nsattributedstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!