属性字符串字体颜色不起作用

Attributed string font color is not working

我正在尝试设置字体颜色,但由于某种原因无法正常工作

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;
}

这让我想知道为什么会这样?

您应该尝试 UIKit 的 UIStringAttributes 而不是 CoreText 的 CTStringAttributes。

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