如何在iOS中使用Xamarin改变NavigationController中1个字符的Title Textcolor



我正在尝试使用Xamarin更改iOS导航栏的标题文本颜色。

我知道如何改变导航栏标题的整体颜色。查看下面我的当前代码。

this.title = "Hello 2 all of you";
this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes()
{               
    ForegroundColor = UIColor.White
};

但是有可能把"2"在我的标题用橙色,标题的其余部分用白色吗?我怎样才能解决这个问题?

这就是我想要实现的:

https://i.stack.imgur.com/hLszW.jpg

谢谢你的帮助!

@Hobeau帮我找到了答案。如果有人需要答案。

var firstAttributes = new UIStringAttributes {
    ForegroundColor = UIColor.White,
    BackgroundColor = UIColor.Black,
    Font = UIFont.FromName("Arial", 24f)
};
    var secondAttributes = new UIStringAttributes {
        ForegroundColor = UIColor.Orange,
        BackgroundColor = UIColor.Black,
        Font = UIFont.FromName("Arial", 24f)
    };

    var prettyString = new NSMutableAttributedString ("Hello 2 all of you");
    prettyString.SetAttributes (firstAttributes.Dictionary, new NSRange (0, 6));
    prettyString.SetAttributes (secondAttributes.Dictionary, new NSRange (6, 1));
    prettyString.SetAttributes (firstAttributes.Dictionary, new NSRange (7, 11));
    testlbl.AttributedText = prettyString;
    this.NavigationItem.TitleView = testlbl;

最新更新