2

当用户输入我的文本文件时,我希望新字符继承以前的属性。

我目前从前一个字符获取属性并将其添加为新范围的新属性?我怎样才能使它扩展以前的属性字符串呢?

// 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)}

注意:这必须发生在用户键入的每个字符上,因此当用户键入时,新字符应该继承之前的属性。

编辑:更多信息

// 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?
4

1 回答 1

2

您可以使用该replaceCharactersInRange:withString:方法来执行此操作。如果指定范围的长度为 0,则不会替换任何字符。字符串将自动赋予它之前字符的属性,除非它在开头,在这种情况下,它将被赋予它之后字符的属性。

NSRange range = NSRangeMake([_mutableAttributedString length],0);
[_mutableAttributedString replaceCharactersInRange:range withString:USER_INPUT];
于 2011-07-22T00:03:06.727 回答