Highlight multiple keywords

Source: Internet
Author: User

Project address: IOS keyword highlighted

Keyword highlighting is often used for search. The following are keywords used by my project.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/20/wKioL1P296uSzuyXAAKKrHjirkc034.jpg "Title =" key character highlight .png "alt =" wkiol1p296uszuyxaakkrhjirkc034.jpg "/>

1 // keyword array keyword color

[LB setattributedtext: Text withregularpattern: [text regularpattern: keyarr] attributes :@{ nsforegroundcolorattributename: [uicolor redcolor]}];

2

/**

Keyword Regular Expression judgment

*/

-(Nsstring *) regularpattern :( nsarray *) Keys {

Nsmutablestring * pattern = [[nsmutablestring alloc] initwithstring :@"(? I) "];

For (nsstring * key in keys ){

[Pattern appendformat: @ "% @ |", key];

}

Return pattern;

}

3

/**

Keyword highlighting settings

*/

-(Void) setattributedtext :( nsstring *) Text

Withregularexpression :( nsregularexpression *) Expression

Attributes :( nsdictionary *) attributesdict

{

Nsmutableattributedstring * attributedstring = [[nsmutableattributedstring alloc] initwithstring: text];


[Expression enumeratematchesinstring: Text

Options: 0

Range: nsmakerange (0, [text length])

Usingblock: ^ (nstextcheckingresult * result, nsmatchingflags flags, bool * Stop ){

Nsange matchrange = [result range];

If (attributesdict ){

[Attributedstring addattributes: attributesdict range: matchrange];

}


If ([result resulttype] = nstextcheckingtypelink ){

Nsurl * url = [result url];

[Attributedstring addattriename: nslinkattributename value: URL range: matchrange];

}

}];

[Self setattributedtext: attributedstring];

}


This article from the "iOS leisurely bug notes" blog, please be sure to keep this source http://ios2013yoyo.blog.51cto.com/6260005/1543550

Highlight multiple keywords

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.