2

I am trying to set the font color and its not working for some reason

public void ConvertToLinkButton(UIButton btn, String hyperlink)
{
    CTStringAttributes attributesHyperLink = new CTStringAttributes();
    attributesHyperLink.UnderlineStyle = CTUnderlineStyle.Single;
    attributesHyperLink.ForegroundColor = UIColor.Purple.CGColor;

    NSMutableAttributedString attrString = new NSMutableAttributedString(btn.TitleLabel.Text);
    attrString.AddAttributes(attributesHyperLink, new NSRange(btn.TitleLabel.Text.IndexOf(hyperlink), hyperlink.Length));
    btn.TitleLabel.AttributedText = attrString;
}

It makes me wonder why is it happening ?

valdetero
  • 4,624
  • 1
  • 31
  • 46
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241

1 Answers1

2

You should try UIKit's UIStringAttributes instead of CoreText's CTStringAttributes.

UIStringAttributes attributesHyperLink = new UIStringAttributes();
attributesHyperLink.UnderlineStyle = NSUnderlineStyle.Single;
attributesHyperLink.ForegroundColor = UIColor.Purple.CGColor;
valdetero
  • 4,624
  • 1
  • 31
  • 46
Timo Salomäki
  • 7,099
  • 3
  • 25
  • 40
  • 1
    @DuraiAmuthan.H Honestly, I'm not sure. However, CoreText is a low-level technology that is more suitable for cases where you really need low-level text handling features. UIKit's UIStringAttributes provides everything you need here and that's what I've used before. – Timo Salomäki Mar 16 '17 at 21:56