Social more to be accepted by the community, in the social products, in the reply or forwarding will @ others display, click @user Jump to the user's introduction page, here first to develop appropriate tags, such as: <at href=\ "Jecky://profile/239\" >ijecky</at>, and then when the client gets the text, it uses Nsregularexpression to split the string, which is illustrated below with a rich text Ohattributedlabel:
Description: The Contentlabel used in this article is the Ohattributedlabel type
Example:
nsstring *atstring = @ " test it, test it. <at href=\ "jecky://profile/239\" >iJecky</at> test it, test it. ";
First split:
nsmutablestring *resultstr = [nsmutablestringstringwithcapacity :0];
nsstring *pattern =@ "<at href=\" (. *?) \ "> (. *?) </at> ";
nsregularexpression *regex = [ nsregularexpression Regularexpressionwithpattern :p attern options : 0 error : nil
nsarray *matches = [regexmatchesinstring:atstring Options:0range:nsmakerange (0,atstring. length )];
nsuinteger lastidx =0;
for (nstextcheckingresult* Match in matches)
{
nsrange range = match. Range ;
if (range. Location > Lastidx)
{
nsstring *temp = [atstringsubstringwithrange : nsmakerange (lastidx, range. Location -Lastidx)];
[resultstr appendString: temp];
}
nsstring *link = [atstringsubstringwithrange: [ Match Rangeatindex:1];
nsstring *text = [atstring substringwithrange :[match rangeatindex : 2 ];
nsstring *atname = [nsstringstringwithformat:@ "@%@", text];
[resultstr appendString: atname];
[markdic setobject: Linkforkey: atname];
lastidx = range. Location + range. length ;
}
if (Lastidx <atstring. length )
{
nsstring *temp = [atstringsubstringfromind Ex: Lastidx];
[resultstr appendString: temp];
}
self. Contentlabel . text = ResultStr;
and show it to Contentlabel.
nsregularexpression* Userregex = [nsregularexpression Regularexpressionwithpattern:@ "\\[email protected]\\w+"options: 0error:nil];
nsmutableattributedstring* mas = [self. Contentlabel . Attributedtext mutablecopy];
[Userregex enumeratematchesinstring:self. Contentlabel . text Options:0
Range:nsmakerange(0,s Elf. Contentlabel . text . length )
usingblock: ^ (nstextcheckingresult *match,nsmatchingflags flags, BOOL *stop) {
nsstring *auser = [self. Contentlabel . text substringwithrange: Match. Range ];
nsstring *atlink = [markdicobjectforkey : Auser];
if (atlink) {
[Mas setlink: [nsurlUrlwithstring: [markdic objectforkey: auser]]range: match. Range ]; //Add it
}
}];
ohparagraphstyle* para = [ohparagraphstyleDefaultparagraphstyle ];
para. firstlineheadindent = 0 ;
para. headindent = 5 ;
para. tailindent = - 5 ;
para. TextAlignment = Kcttextalignmentleft ;
[Mas setparagraphstyle:p ARA];
[ohasbasicmarkupparserprocessmarkupinattributedstring: mas];
self. Contentlabel . Attributedtext = Mas;
iOS-nsregularexpression Regular split string