How to change Title Textcolor of 1 character in NavigationController in iOS using Xamarin

ADeCraemerADeCraemer USMember ✭✭
edited September 2015 in Xamarin.iOS

Hey all,

I am trying to change the title text color from the navigationbar in iOS using Xamarin.

I know how to change the whole color of the title of the navigationbar.
See my current code below.

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

But is it possible to put the "2" in my title in Orange and the rest of the title in white?
How could i solve this problem?

This is what i would like to achieve:

Thnx for your help!

Best Answers

  • hobeauhobeau US
    Accepted Answer

    Hi, what you are looking for is a NSMutableAttributedString which allows you to create a string and apply styling to the string. You can then assign that object to the labels 'AttributedText' property. Here is an article to give you more information but specifically what you want to do is the following:

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

Answers

  • hobeauhobeau USMember
    Accepted Answer

    Hi, what you are looking for is a NSMutableAttributedString which allows you to create a string and apply styling to the string. You can then assign that object to the labels 'AttributedText' property. Here is an article to give you more information but specifically what you want to do is the following:

    `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;`
    
  • ADeCraemerADeCraemer USMember ✭✭

    @hobeau

    I see.
    But how can i put that string to the title of my navigationbar?
    Because you can't add an attributedText to the title of a navigationbar. Or is it possible?

    Thnx for your time!

  • ADeCraemerADeCraemer USMember ✭✭

    @hobeau
    Thnx a lot!
    Just found the same solution myself when you posted your answer.
    I really appreciate your help!

  • hobeauhobeau USMember

    Np :smile:

Sign In or Register to comment.