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