2017-03-06 61 views
0

我有一個HTML字符串,我在UITextView裏顯示爲NSAttributedString。現在我想根據某些條件在屬性字符串中插入/替換字符串。ios - 在一個複雜的NSAttributedString中插入字符串

  • 歸屬字符串本身有多個部分,我需要在特定部分插入一個字符串。
  • 最主要的是識別Section,因爲它可能在字符串中的任何位置。
  • 需要確定部分相對於其近鄰部分。

我做的,我可以在NSAttributedString與我的自定義新的字符串替換字符串的一部分工作的其餘部分。我只需要字符串部分標識的想法。

這裏是屬性文本

enter image description here

截圖現在ü可以看到有三個部分聯合(R),跖趾(R)和聯合(L)。它們總是隨機的,而不是在特定的位置。

它們中的數據是有序的,但它可能有更多小節。

現在,如果我想所有的趾甲節內的字符串,我怎麼能得到它?

+0

難道這些標題具有相同的文字一如既往:更改後 enter image description here

屏幕截圖?他們只是粗體嗎? –

+0

是的標題有相同的文字 – Hassy

+0

你想改變裏面的字符串** Flexion:角度:0-5,質量:Crepitus **或角度,質量值 –

回答

1

可以在字符串中設置任意屬性。例如,[string setAttributes:@{ @"my custom attribute name" : @(YES), ...} range:range];

然後,當您想再次使用這些屬性時,可以使用enumerateAttribute:。請注意使用options:NSAttributedStringEnumerationReverse以避免覆蓋copyOfString中的數據。

[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屬性作爲獨特的東西覆蓋每個部分,然後檢查匹配的字符串(部分)和理想的文本附加到每個部分(有我你知道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

相關問題