2017-03-06 1 views
0

UITextViewの中に表示するHTML文字列がNSAttributedStringです。今、いくつかの条件に関して、属性付き文字列の中に文字列を挿入/置き換えたいと思います。ios - 複合NSAttributedStringに文字列を挿入する

  • 属性付き文字列自体には複数のセクションがあり、特定のセクションに文字列を挿入する必要があります。
  • 主なことは、文字列内のどこにでもある可能性があるため、Sectionを特定することです。
  • 必要なセクションを、近くにあるセクションに相対的なものとして識別します。

NSAttributedStringの文字列の一部を自分の新しい文字列に置き換えることができます。文字列セクションのアイディアが必要です。ここ

は、以下の3つのセクション継手(R)、中足指節(R)とジョイント(L)が存在する今uが見ることができる属性付きテキスト

enter image description here

のスクリーンショットです。それらは特定の場所ではなく常にランダムです。

これらのデータは順序付けられていますが、サブセクションがさらにある場合があります。

今、私はメタタゴ殻角セクション内のすべての文字列にしたい場合はどうすれば入手できますか?

+0

は、これらの見出しはいつもと同じテキストを持っていますか?彼らは大胆なだけですか? –

+0

はい見出しは同じテキストを持っています – Hassy

+0

内側の文字列を変更したい** Flexion:Angle:0-5、Quality:Crepitus **またはAngle、品質値 –

答えて

1

文字列に任意の属性を設定することができます。たとえば、[string setAttributes:@{ @"my custom attribute name" : @(YES), ...} range:range];

これらの属性を再度使用する場合は、enumerateAttribute:を使用できます。 copyOfStringのデータを上書きしないように、options:NSAttributedStringEnumerationReverseを使用することに注意してください。私はそれはあなたのケースのためのソリューションであることができると思い

[string enumerateAttribute:@"my custom attribute name" inRange:NSMakeRange(0, string.length) options:NSAttributedStringEnumerationReverse usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) { 
    if (value){ // Do something here 
     [copyOfString replaceCharactersInRange:range withString:@"______"]; 
    } 
}]; 
+0

もし私が範囲を知っていたのであれば、最初の場所では問題はありません。セクションの属性を設定しない範囲を特定する必要があります。 – Hassy

+0

あなたの質問はNSAttributedStringに焦点を当てているようです。NSRangeを使用する場合と使用しない場合があります。文字列を自分で作成せず、HTMLからインスタンス化するだけの場合は、 'rangeOfString:'を使ってそれらのヘッダの開始/終了範囲を見つけなければなりません。 – arsenius

0

は、私は、各セクションをカバーするユニークなものとしてstrong HTML属性を使用して、一致した文字列(セクション)を確認し、各セクションに望ましいテキストを追加(Iました知られている私はそのために、より正確なコードを書くことができ、あなたのHTML文字列):

// sample HTML String 
NSString *htmlString = @"<strong>Section (A)</strong><br>...<br><strong>Section (B)</strong><br>...<br><strong>Section (C)</strong><br>...<br>"; 

NSString *regexString = @"<strong>(.*?)</strong>"; // or whatever makes sense for your scenario 
NSRegularExpression *regex = 
[NSRegularExpression regularExpressionWithPattern:regexString 
              options:NSRegularExpressionCaseInsensitive 
              error:nil]; 

NSMutableString* mutableString = [htmlString mutableCopy]; 
NSInteger offset = 0; // keeps track of range changes in the string 
// due to replacements. 
for (NSTextCheckingResult* result in [regex matchesInString:htmlString 
                options:0 
                 range:NSMakeRange(0, [htmlString length])]) { 

    NSRange resultRange = [result range]; 
    resultRange.location += offset; // resultRange.location is updated 
    // based on the offset updated below 

    // implement your own replace functionality using 
    // replacementStringForResult:inString:offset:template: 
    // note that in the template $0 is replaced by the match 
    NSString* match = [regex replacementStringForResult:result 
               inString:mutableString 
               offset:offset 
               template:@"$0"]; 
    NSLog(@"match %@", match); 

    NSString* append; 
    if ([match containsString:@"Section (A)"]) { 
     append = @"AppendForSectionA"; 
    } else if ([match containsString:@"Section (B)"]) { 
     append = @"AppendForSectionB"; 
    } else if ([match containsString:@"Section (C)"]) { 
     append = @"AppendForSectionC"; 
    } else { 
     append = @""; 
    } 

    NSString* replacement = [match stringByAppendingString:append]; 

    // make the replacement 
    [mutableString replaceCharactersInRange:resultRange withString:replacement]; 

    // update the offset based on the replacement 
    offset += ([replacement length] - resultRange.length); 
} 

NSLog(@"resulting string: %@", mutableString); 

、結果は次のようになります。

<strong>Section (A)</strong>AppendForSectionA<br>...<br><strong>Section (B)</strong>AppendForSectionB<br>...<br><strong>Section (C)</strong>AppendForSectionC<br>...<br> 

スクリーンショットちゃんの前にGE:変更後の enter image description here

スクリーンショット: enter image description here

関連する問題