2011-07-21 25 views
2

ユーザーが私のテキストファイルに入力するとき、私は新しい文字が以前の属性を継承したい。目的C - NSAttributedString属性を拡張しますか?

私は現在、前の文字から属性を取得し、それを新しい範囲の新しい属性として追加しますか?代わりに以前の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)} 

注:これは、すべての文字のために起こることがある、そのユーザーの種類、ユーザーが前の属性を継承しなければならない新しい文字を入力するようになっています。

EDIT: 詳細情報

// 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? 
+0

-1?かなり不公平に思える – dreamlax

答えて

2

これを行うには、replaceCharactersInRange:withString:メソッドを使用することができます。指定された範囲の長さが0の場合、文字は置き換えられません。文字列の先頭に文字がない場合、文字列の前に文字の属性が自動的に与えられます。その場合、その文字の後に文字の属性が与えられます。

NSRange range = NSRangeMake([_mutableAttributedString length],0); 
[_mutableAttributedString replaceCharactersInRange:range withString:USER_INPUT]; 
関連する問題