Use of nsattributedstring in iOS-coloring keywords and working with HTML instances

Source: Internet
Author: User

1, recently the project has used a function, a very good function. is when the user searches, the search results come out after the keyword you entered is marked red. This allows the user to see what happens when they enter something. Another feature is that there is now a piece of text, but to color some of these strings, this time nsattibutedstring played a very big role. The following is a piece of code I have written, you can take it, very convenient to deal with.

#import <Foundation/Foundation.h> @interface nsattributedstring (color)/** *  coloring the middle of a content keyword * *  @param Content All contents *  @param searchs Key Words Group * *  @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 ran            Ge=[content Rangeofstring:obj];            Nsinteger location=0; while (range.length>0) {[attstring AddAttribute: (nsstring*) Nsforegroundcolorattributename Value:color R                Ange: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

Before also saw similar third party, he refers to write position, which position to which position, here the word is the direct designation string, moreover, appears many times, the same can all coloring. You can make minor adjustments according to your own needs.

2, in another case, what if the content returned in the background has HTML-unique tags? Of course, you can choose some third-party to deal with, but compared to the function you want, you can find that this is not what you want, too cumbersome, I just want to put the HTML tag, or in the HTML format display. But do not want to use WebView to display, because WebView cannot edit these text. Here I will simply write a method of turning HTML into 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;    }

The return is the same as in the original HTML format, CSS style in effect. If you just want to go to the HTML tag, instead of showing it as a CSS style, you can call the [Attrstring string] method directly in the returned attrstring and then display it directly in the label or TextView, which is very convenient.

Feel everyone reading.

Use of nsattributedstring in iOS-coloring keywords and working with HTML instances

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.