iOS中NSAttributedString的使用--對關鍵字著色,以及處理html執行個體

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   os   使用   ar   for   

      1,最近項目中用到了一個功能,一個很好的功能。就是使用者在搜尋的時候,搜尋結果出來後對你輸入的關鍵字進行紅色標記。這樣使用者就很請楚的看到自己輸入什麼後會出現什麼樣子的結果。還有一個功能是,現在有一段文字了,但是要對其中的某些字串進行著色處理,這個時候NSAttibutedString起到了非常大的作用。以下是我寫好的一段代碼,各位可以拿去用,非常方便的處理好。

#import <Foundation/Foundation.h>@interface NSAttributedString (Color)/** *  對內容中部份關鍵字進行著色處理 * *  @param content 所有內容 *  @param searchs 關鍵字數組 * *  @return */+(NSAttributedString *)attributeStringWithContent:(NSString *)content keyWords:(NSArray *)keyWords;@end

 

#import "NSAttributedString+Color.h"@implementation NSAttributedString (Color)+(NSAttributedString *)attributeStringWithContent:(NSString *)content keyWords:(NSArray *)keyWords{    UIColor *color=[UIColor redColor];    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:content];    if (keyWords) {        [keyWords enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {            NSRange range=[content rangeOfString:obj];            NSInteger location=0;            while (range.length>0) {                [attString addAttribute:(NSString*)NSForegroundColorAttributeName value:color range:NSMakeRange(location+range.location, range.length)];                location+=(range.location+range.length);                NSString *tmp= [content substringWithRange:NSMakeRange(range.location+range.length, content.length-location)];                range=[tmp rangeOfString:obj];            }                    }];    }        return attString;}@end

  之前也有看到類似的第三方,他是指寫位置,哪個位置到哪個位置的,這裡的話是直接指定字串,而且,出現多次的,一樣可以全部著色。大家可以根據自身需求進行細微的調整。

   2,還有一種情況,當後台返回的內容中帶有html專屬的標籤怎麼辦?當然,你可以選擇一些第三方來處理,但是對比你想要的功能後,可以發現,這並不是你想要的,太笨重了,我只是要把html標籤去了,或是就按html裡面的格式顯示 。但是又不想用webview去顯示 ,因 為webview不能對這些文本進行編輯。這裡我就簡單寫個把html轉成NSAttibutedString的方法。

 

+(NSAttributedString *)attributedStringWithHtml:(NSString *)html{    NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};    NSMutableAttributedString *attrString=[[NSMutableAttributedString alloc] initWithData:[html dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES] options:options documentAttributes:nil error:nil];    return attrString;    }

   這裡返回的是按照原來的html格式,CSS樣式一樣生效的。如果你只想把html標籤去了,而不要顯示成有CSS樣式的,你可以直接在返回的attrString中再調用[attrString string]方法就可以,然後直接顯示在label或者textView在,非常的方便。

  感覺大家閱讀。

iOS中NSAttributedString的使用--對關鍵字著色,以及處理html執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.