ios - Make link in UILabel.attributedText *not* blue and *not* underlined -
i want words within ohattributedlabel links, want them colors other blue , don't want underline.
this giving me blue link underlined text:
-(void)createlinkfromword:(nsstring*)word withcolor:(uicolor*)color atrange:(nsrange)range{ nsmutableattributedstring* mutableattributedtext = [self.label.attributedtext mutablecopy]; [mutableattributedtext beginediting]; [mutableattributedtext addattribute:kohlinkattributename value:[nsurl urlwithstring:@"http://www.somewhere.net"] range:range]; [mutableattributedtext addattribute:(id)kctforegroundcolorattributename value:color range:range]; [mutableattributedtext addattribute:(id)kctunderlinestyleattributename value:[nsnumber numberwithint:kctunderlinestylenone] range:range]; [mutableattributedtext endediting]; self.label.attributedtext = mutableattributedtext; }
since i'm using ohattributedlabel, tried using methods in it's nsattributedstring+attributes.h
category, return blue underlined links well:
-(void)createlinkfromword:(nsstring*)word withcolor:(uicolor*)color atrange:(nsrange)range{ nsmutableattributedstring* mutableattributedtext = [self.label.attributedtext mutablecopy]; [mutableattributedtext setlink:[nsurl urlwithstring:@"http://www.somewhere.net"] range:range]; [mutableattributedtext settextcolor:color range:range]; [mutableattributedtext settextunderlinestyle:kctunderlinestylenone range:range]; self.label.attributedtext = mutableattributedtext; }
if comment out line setting links in each version, text gets colored pass in - works. seems setting link overriding , turning blue.
unfortunately apple docs page found shows how set link text blue , underline it, don't need: https://developer.apple.com/library/content/documentation/cocoa/conceptual/attributedstrings/tasks/changingattrstrings.html
so ended using tttattributedlabel:
-(void)createlinkfromword:(nsstring*)word withcolor:(uicolor*)color atrange:(nsrange)range{ nsmutableattributedstring* newtextwithlinks = [self.label.attributedtext mutablecopy]; nsurl *url = [nsurl urlwithstring:@"http://www.reddit.com"]; self.label.linkattributes = @{nsforegroundcolorattributename: color, nsunderlinestyleattributename: @(nsunderlinestylenone)}; [self.label addlinktourl:url withrange:range]; }
i found ohattributedlabel
have methods set links , declare colors , underline styles links. however, wanted links different colors based on parameter. tttattributedlabel
allows letting set it's linkattributes
property each link create.
Comments
Post a Comment