1,最近項目中用到了一個功能,一個很好的功能。就是用戶在搜索的時候,搜索結果出來后對你輸入的關鍵字進行紅色標記。這樣用戶就很請楚的看到自己輸入什么后會出現什么樣子的結果。還有一個功能是,現在有一段文字了,但是要對其中的某些字符串進行着色處理,這個時候NSAttibutedString起到了非常大的作用。以下是我寫好的一段代碼,各位可以拿去用,非常方便的處理好。
#import <Foundation/Foundation.h> @interface NSAttributedString (Color) /** * 對內容中部份關鍵字進行着色處理 * * @param content 所有內容 * @param searchs 關鍵字數組 * * @return */ +(NSAttributedString *)attributeStringWithContent:(NSString *)content keyWords:(NSArray *)keyWords; @end
+(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) { NSMutableString *tmpString=[NSMutableString stringWithString:content]; 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= [tmpString substringWithRange:NSMakeRange(range.location+range.length, content.length-location)]; tmpString=[NSMutableString stringWithString:tmp]; range=[tmp rangeOfString:obj]; } }]; } return attString; }
之前也有看到類似的第三方,他是指寫位置,哪個位置到哪個位置的,這里的話是直接指定字符串,而且,出現多次的,一樣可以全部着色。大家可以根據自身需求進行細微的調整。
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在,非常的方便。
3.這兩天有朋友提到一個需求是,關鍵字和顏色都自定義,最后寫了個方法實現了這個功能。
/** * 對指定內定進行着色,keywords數組與colors數組相對應 * * @param content 全部內容 * @param keyWords 關鍵字數組 * @param color 關鍵字對應顏色,如果傳空,則默認對關鍵字着紅色 * @param repeat 關鍵字出現多次的時候,是否全部進行多次着色,默認否 * * @return */ +(NSAttributedString *)attributeStringWithContent:(NSString *)content keyWords:(NSArray *)keyWords colors:(NSArray *)colors repeat:(BOOL)repeat;
+(NSAttributedString *)attributeStringWithContent:(NSString *)content keyWords:(NSArray *)keyWords colors:(NSArray *)colors repeat:(BOOL)repeat { NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:content]; if (keyWords) { [keyWords enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSMutableString *tmpString=[NSMutableString stringWithString:content]; NSRange range=[content rangeOfString:obj]; NSInteger location=0; while (range.length>0) { UIColor *color=nil; if (!colors[idx]) { color=[UIColor redColor]; }else{ color=colors[idx]; } [attString addAttribute:(NSString*)NSForegroundColorAttributeName value:color range:NSMakeRange(location+range.location, range.length)]; location+=(range.location+range.length); NSString *tmp= [tmpString substringWithRange:NSMakeRange(range.location+range.length, content.length-location)]; tmpString=[NSMutableString stringWithString:tmp]; range=[tmp rangeOfString:obj]; if (!repeat) { break; } } }]; } return attString; }
如果有什么問題可以向我反饋一下,謝謝
感覺大家閱讀。