2

ユーザーがテキストファイルに入力するときに、新しい文字に以前の属性を継承させたいと思います。

現在、前の文字から属性を取得し、それを新しい範囲の新しい属性として追加していますか?代わりに以前のattributedStringを拡張するようにするにはどうすればよいですか?

// 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 に答える