Objective C - NSAttributedString extend Attributes?
As the user types into my textfiled I want the new characters to inherit the previous attributes.
I currently get the attributes from the previous character and add it as new attribute for the new range? How can I make it so that it extends the previous attributedString instead?
// What I have
"a"{attribute1 (0,1)} "b"{attribute2 (1,1)} "c"{attribute3(2,1)}
// What I'm trying to do
"abc" {attribute1 (0,3)}
Note: This has to happen for every character that the user types, so as the user type the new character should inherit the previous attributes.
EDIT: More info
// So I create a new AttributedString, added it to my mutable string
NSAttributedString *newString = [[NSAttributedString alloc] initWithString:USER_INPUT attributes:self.defaultAttributes];
[_mutableAttributedString insertAttributedString:newString atIndex:selectedNSRange.location];
// Is it possible to add the USER_INPUT to the end of my mutable 开发者_开发百科attributedString and extends the range of the previous attribute?
You can use the replaceCharactersInRange:withString:
method to do this. If the length of the specified range is 0, no characters will be replaced. The string will be automatically given the attributes of the character before it, unless it is at the beginning, in which case it will be given the attributes of the character after it.
NSRange range = NSRangeMake([_mutableAttributedString length],0);
[_mutableAttributedString replaceCharactersInRange:range withString:USER_INPUT];
精彩评论