In the "Vaudicamps" chat function, party a request to send the URL to automatically identify, and click to automatically jump
In the implementation process, all the text is dynamically obtained, when setting rich Text properties, can not follow the normal method
- How to Solve
- If only text, no expression, you can use Uilabel and uitextview to achieve
- If there is expression, it is now easier to learn to use Uitextview to achieve
- Code implementation
- UILabelUilabel Implementation method:
I use the third-party framework: Tyattributedlabel, you can go to git download, as follows, I will be the implementation of their own code, tell me how to use, for reference, attention, just explain their own ideas, can not be copied using:1Import the Tyattributedlabel header file to where you want it: (I am implementing the following code in. h, the reader can also be declared in. m)@protectedTyattributedlabel*_chattext;2The instance just declared in the. m file is _chattext, as follows: _chattext= [[Tyattributedlabel alloc] init];//Create an instance_chattext.backgroundcolor = [Uicolor Clearcolor];//Set Background color_chattext.Delegate= self;//Set up proxy_chattext.textalignment= Nstextalignmentleft;//formatting the display--left_chattext.numberoflines =0;//Unlimited set of lines3. Assigning values to _chattext-This is a key point, please note: nsstring*urlstr =@"http://baidu.com This is a URL, this URL can be clicked to jump to the http://baidu.com page";//assigning values to UrlstrNsarray *textarray = [Yztools substruseurl:urlstr];//Note here: Yztools is my own usual method of accumulation of a tool class, Substruseurl This method is to urlstr according to the URL to separate into an array, the following will give the code inside, please be patient, separate the array as follows @[@ "http:// Baidu.com ", @" This is a URL, this URL can be clicked jump to ", @" http://baidu.com "]
for(NSString *textinchTextarray) { //depending on the delimited array, determine if it is a URL and then call the Tyattributedlabel method to add the URL's color properties and font size if([Text Hasprefix:@"http"]) {[_chattext appendlinkwithtext:text linkfont:[_msg Textfont] Linkcolor:[uicolor Bluecolor] LinkData:te XT]; }Else{[_chattext appendtext:text]; } } 4. In. m to inherit the Tyattributedlabel agent <TYAttributedLabelDelegate>, and implement the agent
#pragmamark-tyattributedlabeldelegate-(void) Attributedlabel: (Tyattributedlabel *) Attributedlabel textstorageclicked: (ID<TYTextStorageProtocol>) Textrun Atpoint: (cgpoint) point{NSLog (@"Textstorageclickedatpoint"); if([Textrun Iskindofclass:[tylinktextstorageclass]]) {NSString*linkstr = ((tylinktextstorage*). Textrun). LinkData; if([Linkstr Hasprefix:@"http:"] {[[uiapplication sharedapplication] openurl:[nsurl Urlwithstring:linkstr]]; }Else{Uialertview*alertview = [[Uialertview alloc]initwithtitle:@"Click Tips"Message:linkstrDelegate: Nil Cancelbuttontitle:@"Determine"Otherbuttontitles:nil]; [Alertview show]; } }}- (void) Attributedlabel: (Tyattributedlabel *) Attributedlabel textstoragelongpressed: (ID<TYTextStorageProtocol>) textstorage onstate: (uigesturerecognizerstate) State atpoint: (cgpoint) point{NSLog (@"textstoragelongpressed");}
In the above code, there is a Substruseurl method, the code is as follows (can be copied directly is used): * * * I use the class method, the reader can also use the object method * * *
#pragmaMark-cut strings by URL array/** cut arrays by URL*/+ (Nsmutablearray *) Substruseurl: (NSString *)string{nserror*error; //Regular expressions that can recognize URLsNSString*regulastr =@"( (HTTP[S]{0,1}|FTP)://[a-za-z0-9\\.\\-]+\\. ( [A-za-z] {2,4}) (: \\d+)? (/[a-za-z0-9\\.\\[email protected]#$%^&*+?:_/=<>]*)?) | (www. [A-za-z0-9\\.\\-]+\\. ([a-za-z]{2,4}) (: \\d+)? (/[a-za-z0-9\\.\\[email protected]#$%^&*+?:_/=<>]*)?)"; Nsregularexpression*regex =[Nsregularexpression Regularexpressionwithpattern:regulastr Options:nsregularexpressioncaseinsensitive Error:&ERROR]; Nsarray*arrayofallmatches = [Regex matchesinstring:stringOptions0Range:nsmakerange (0, [stringlength]); Nsmutablearray*arr=[[Nsmutablearray alloc]init]; Nsmutablearray*rangearr=[[Nsmutablearray alloc]init]; for(Nstextcheckingresult *matchincharrayofallmatches) {NSString*Substringformatch; Substringformatch= [stringSubstringWithRange:match.range]; [Arr Addobject:substringformatch]; [Rangearr Addobject:[nsvalue ValueWithRange:match.range]; } nsstring*substr=string;//For (NSString *str in arr) {//[Rangearr addobject:[self rangesofstring:str instring:substr];// }Uifont*font = [Uifont systemfontofsize: -]; Nsmutableattributedstring*Attributedtext; Attributedtext=[[Nsmutableattributedstring Alloc]initwithstring:substr Attributes:@{nsfontattributename:font}]; Nsmutablearray*urlarray =[Nsmutablearray array]; //cut a string into an array based on a URLNsinteger index =0; Nsmutablearray*strarray =[[Nsmutablearray alloc] init]; for(Nsinteger i =0; i < Rangearr.count; i++) {Nsvalue*value =Rangearr[i]; Nsinteger valueloction=value.rangeValue.location; Nsinteger Valuelength=value.rangeValue.length; if(i = =0&& valueloction! =0) {[Strarray addobject:[stringSubstringwithrange:nsmakerange (0, valueloction)]; Index=valueloction; } if(Index! =valueloction) {[Strarray addobject:[stringSubstringwithrange:nsmakerange (Index, Valueloction-index)]; Index= index +valuelength; } [Strarray addobject:[stringSubstringwithrange:nsmakerange (Valueloction, valuelength)]; Index= index +valuelength; if(i = = Rangearr.count-1&& (valueloction + valuelength! =string. Length)) {[Strarray addobject:[stringSubstringwithrange:nsmakerange (Valuelength + valueloction,string. Length-valuelength-valueloction)]; } } if(Rangearr.count = =0) {[Strarray addobject:string]; } NSLog (@"----Strarray =%@", Strarray); //gets the URL of a string all links for(Nsvalue *valueinchRangearr) {Nsinteger Index=[Rangearr Indexofobject:value]; [Urlarray Addobject:[nsurl Urlwithstring:[arr Objectatindex:index]]; } returnStrarray;}//gets the Nsrange of the lookup string in the parent string+ (Nsvalue *) rangesofstring: (NSString *) searchstring instring: (NSString *) str {nsrange Searchrange= Nsmakerange (0, [str length]); Nsrange range; if(range = [str rangeofstring:searchstring options:0Range:searchrange]). Location! =nsnotfound) {Searchrange= Nsmakerange (Nsmaxrange (range), [str length]-Nsmaxrange (range)); } return[Nsvalue valuewithrange:range];}
Implementation of the IOS auto-identify URL (link) feature