xamarinxamarin.iosnsattributedstringnsmutableattributedstringnsattributedescription

Attributed string font color is not working


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 ?


Solution

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

    UIStringAttributes attributesHyperLink = new UIStringAttributes();
    attributesHyperLink.UnderlineStyle = NSUnderlineStyle.Single;
    attributesHyperLink.ForegroundColor = UIColor.Purple.CGColor;