[英]Have multiple ranges with different attributes on NSAttributedString
[英]Do NSAttributedString attributes stack when their ranges nest/overlap?
我正在將簡單的HTML解析為一個屬性字符串(對於iOS 6),並且似乎無法使嵌套屬性起作用。 例如:
This <font color="red">is <i>what</i> I mean</font> to achieve.
我正確地應用了屬性,但是除非在不影響字體顏色的范圍內進行斜體化,否則它不會生效。 我的谷歌搜索沒有引發任何相關問題,我想知道NSAttributedString是否甚至完全支持此功能,或者屬性范圍必須不重疊。
編輯:
我將這個問題改寫得更清楚了。 問題的答案是“否”,您必須在單個屬性字典中指定給定范圍的所有屬性,但它們不能合並。 我已經接受了對原始問題的合理回答,認為是正確的。
您可以使用NSMutableAttributedString
獲得所需的結果。 您只需要分別設置每個屬性。 例如
NSString *string = @"This is a test";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string attributes:nil];
NSMutableDictionary *attributes = [@{NSForegroundColorAttributeName: [UIColor redColor]} mutableCopy];
[string setAttributes:attributes range:[string rangeOfString:@"is a test"];
[attributes setObject:font forKey:NSFontAttributeName];
[string setAttributes:attributes range:[string rangeOfString:@"test"];
是的,您可以具有特定范圍的特定屬性。 隨你想要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.