归因字符串字体颜色不起作用

时间:2017-03-16 20:54:45

标签: xamarin xamarin.ios nsattributedstring nsmutableattributedstring nsattributedescription

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

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

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

1 个答案:

答案 0 :(得分:2)

你应该尝试使用UIKit的UIStringAttributes而不是CoreText的CTStringAttributes。

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