ios - Does NSMutableAttributedString automatically concatenate identical adjacent attributes? -
we have simple markup parser in our ios app takes text html-like tags , converts nsattributedstring
iterating on each character in simple nsstring
, producing nsmutableattributedstring
.
in worst case scenario added new set of attributes every single character iterated through string, nsmutableattributedstring
intelligent enough join identical adjacent attribute ranges , optimize? or horribly inefficient , slow render in core text?
i'm not suggesting algorithm in general! i'm looking @ worst case scenario, , wondering whether nsmutableattributedstring
has built in intelligent behaviour?
i tried (a simplified version of real code):
nsmutableattributedstring *text = [[nsmutableattributedstring alloc] initwithstring:@"testfoobarhey"]; uifont *font = [uifont fontwithname:@"georgia-bold" size:12.0]; [text addattribute:nsfontattributename value:font range:nsmakerange(4, 3)]; [text addattribute:nsfontattributename value:font range:nsmakerange(7, 3)];
in other words, added 2 bold attributes right next each other. viewed result:
nslog(@"%@", text);
and output showed 3 chunks of text: "test" in normal font, "foobar" in bold, , "hey" in normal font.
so answer question, yes, looks 2 adjacent formatting ranges joined automatically in nsmutableattributedstring.
p.s. on related note, i've noticed if apply attribute range extends across line break, resulting nsattributedstring have 1 attribute before line break , after, , line break won't have attributes. if you're ever parsing 1 character @ time, might need compensate that.
Comments
Post a Comment