I have an MSMutableAttributedString displayContent. Content attributes vary in lines, that is, the colors and font sizes may vary depending on the letter.
I want to add a new character to the end of the line and get the attributes of the last character in displayContent for this. I canβt know what these attributes are in advance, since they are under user control.
When I add a new character (tempAttr):
NSAttributedString * tempAttr = [[NSAttributedString alloc] initWithString:appendage];
[displayContent appendAttributedString:tempAttr];
it shows the reset attributes of the entire string to the attributes of the new character (which I did not specify, since I cannot know what they need).
How do I get tempAttr to display the attributes of the last character in displayContent? Thank.
Update Progress in this was awkward, but functional. Copy the attribute dictionary from the last character on the display (displayContent), and then reapply these attributes to the new character to be added:
NSMutableDictionary * lastCharAttrs = [NSMutableDictionary dictionaryWithCapacity:5];
[lastCharAttrs addEntriesFromDictionary: [displayContent attributesAtIndex:0
effectiveRange:NULL]];
NSMutableAttributedString * tempAttr = [[NSMutableAttributedString alloc] initWithString:newCharacter
attributes:lastCharAttrs];
[displayContent appendAttributedString:tempAttr];
I would hope that there is a more elegant way to do this, for example, by setting the NSTextField property.
source
share