Setting text color on links doesn't work in 3.5.3
rhfung opened this issue · comments
Setting the text color on links in 3.4.3 works as expected, so this must be a new bug.
Here's the code I am using.
NSMutableAttributedString* addressStr = /*...*/;
NSRange someRange = /* range */;
[addressStr setLink:[NSURL URLWithString:@"location://store"] range:someRange];
[addressStr setTextColor:[UIColor clearColor] range:someRange];
addressStr.userInteractionEnabled = NO;
Hi @rhfung
I don't recall changing anything regarding text color between 3.4.3 and 3.5.3 (see diff here, especially this line).
Are you sure this change in behavior you are talking about is not because some changes between your current iOS versions and an older one?
Anyway, setLink:range:
only adds an attribute (containing the URL) to the NSUAttributedString
, and never changed the color of the text at all.
If the links are colored in blue in OHAttributedLabel
that's not because they are blue in the original NSAttributedString
, but because OHAttributedLabel
itself adds the link color when drawing the text.
If you want to override the color of the link, you can either:
- Change the
linkColor
property of yourOHAttributedLabel
— that will change the color for ALL links but only for the givenOHAttributedLabel
- or change
[[OHAttributedLabel appearance] setLinkColor:]
(as this propery is aUI_APPEARANCE_SELECTOR
so can be used with the appearance proxy) — that will change the color for ALL links for ALLOHAttributedLabels
in your app (that's what Apple's UIAppearance proxy are for after all) - or affect a
delegate
to yourOHAttributedLabel
and implementOHAttributedLabelDelegate
's-attributedLabel:colorForLink:underlineStyle:
method — this will allow you to give a different color to each of your links, allowing you to treat each link separately, for example treatNSURL
whosescheme
is@"location"
differently from the others.
PS : Your call userInteractionEnabled = NO
on the NSMutableAttributedString
object makes no sense