ios - Specify multiple/conditional link colors in TTTAttributedLabel -
i've added link detector tttattributedlabel identifies @mentions , #hashtags , creates link @ position in tttattributedlabel instance:
- (void)highlightmentionsinstring:(nsstring *)text withcolor:(uicolor *)color isbold:(bool)bold isunderlined:(bool)underlined { nsregularexpression *mentionexpression = [nsregularexpression regularexpressionwithpattern:@"(?:^|\\s)(@\\w+)" options:no error:nil]; nsarray *matches = [mentionexpression matchesinstring:text options:0 range:nsmakerange(0, [text length])]; (nstextcheckingresult *match in matches) { nsrange matchrange = [match rangeatindex:1]; nsstring *mentionstring = [text substringwithrange:matchrange]; nsrange linkrange = [text rangeofstring:mentionstring]; nsstring* user = [mentionstring substringfromindex:1]; nsstring* linkurlstring = [nsstring stringwithformat:@"user:%@", user]; [self.attributedlabel addlinktourl:[nsurl urlwithstring:linkurlstring] withrange:linkrange]; } } i've discovered can change link colors , attributes:
nsarray *keys = [[nsarray alloc] initwithobjects:(id)kctforegroundcolorattributename,(id)kctunderlinestyleattributename , nil]; nsarray *objects = [[nsarray alloc] initwithobjects:color,[nsnumber numberwithint:kctunderlinestylenone], nil]; nsdictionary *linkattributes = [[nsdictionary alloc] initwithobjects:objects forkeys:keys]; self.attributedlabel.linkattributes = linkattributes; but changes colors every link attribute - including web links, hashtags, , mentions. there way create different link colors using regular expression or range? if wanted @mentions gray, @hashtags red, , web links blue?
i working on similar problem , came across question. didn't know precisely how inject sort of different expressions match other sorts of things in label, first bit of code cleared up.
for question though — what did change tttattributedlabel method 1 adds nstextcheckingresult. so, if make few changes for loop in method , use [self.label addlinkwithtextcheckingresult: attributes: ] , set attributes suggest, loop looks this:
for (nstextcheckingresult *match in matches) { nsrange matchrange = [match rangeatindex:1]; nsstring *mentionstring = [text substringwithrange:matchrange]; nsstring* user = [mentionstring substringfromindex:1]; nsstring* linkurlstring = [nsstring stringwithformat:@"user:%@", user]; nsarray *keys = [[nsarray alloc] initwithobjects:(id)kctforegroundcolorattributename, (id)kctunderlinestyleattributename, nil]; nsarray *objects = [[nsarray alloc] initwithobjects:color,[nsnumber numberwithint:kctunderlinestylenone], nil]; nsdictionary *linkattributes = [[nsdictionary alloc] initwithobjects:objects forkeys:keys]; [self.label addlinkwithtextcheckingresult:match attributes:linkattributes]; } in case, show # , @ in toasted orange.
and have - (void)attributedlabel:(tttattributedlabel *)label didselectlinkwithtextcheckingresult:(nstextcheckingresult *)result tttattributedlabeldelegate method in tttattributedlabeldelegate. gets called nstextcheckingresult when 1 taps on # or @ text.
is looking for?
Comments
Post a Comment