Search code examples
iosuitableviewxamarin.iosnsattributedstringnsmutableattributedstring

NSMutableAttributedString not appending?


I am trying to append two NSAttributedString's with the following values and I get the first NSAttributedString but not the second. The label is inside a UITableView. I have no idea what is causing this problem.

NSMutableAttributedString ms = new NSMutableAttributedString();
NSAttributedString attributedString = new NSAttributedString("Add New Seller ", new UIStringAttributes(){
                    Font = UIFont.FromName("Quicksand-Regular", 17)});
NSAttributedString attributedString1 = new NSAttributedString("+", new UIStringAttributes(){
                    Font = UIFont.FromName("Quicksand-Regular", 17),
                    ForegroundColor = new UIColor(33f, 201f, 130f, 1f)});
ms.Append(attributedString);
ms.Append(attributedString1);
cell.seller.AttributedText = ms;

Solution

  • Your second NSAttributedString is there appended in the final NSMutableAttributedString but you might not see it because it's white. Your issue as @Larme indicated in the comments is with the way you are creating your UIColor.

    UIColor accepts nfloat values between 0 and 1. When you say new UIColor(33f, 201f, 130f, 1f) the resulting Color will be White because it will treat any value over 1.0 as 1.0, hence

    new UIColor(33f, 201f, 130f, 1f) has the same result as new UIColor(1f, 1f, 1f, 1f).

    To fix your code you just need to change the UIColor initialization as

    new UIColor(33f/255, 201f/255, 130f/255, 1f)

    As you see dividing your 3 values of the color (RGB) by 255. The last value represents the alpha.

    Hope this helps.-