NSMutableAttributedString 不追加?

NSMutableAttributedString not appending?

我正在尝试使用以下值附加两个 NSAttributedString,我得到第一个 NSAttributedString 而不是第二个。标签在 UITableView 内。我不知道是什么导致了这个问题。

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;

您的第二个 NSAttributedString 附加在最后一个 NSMutableAttributedString 中,但您可能看不到它,因为它是白色的。正如@Larme 在评论中指出的那样,您的问题与您创建 UIColor 的方式有关。

UIColor 接受 0 到 1 之间的 nfloat 值。当您说 new UIColor(33f, 201f, 130f, 1f) 时,结果颜色将为白色,因为它会将任何超过 1.0 的值视为 1.0,因此

new UIColor(33f, 201f, 130f, 1f)new UIColor(1f, 1f, 1f, 1f).

的结果相同

要修复您的代码,您只需将 UIColor 初始化更改为

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

如您所见,将颜色 (RGB) 的 3 个值除以 255。最后一个值表示 alpha。

希望对您有所帮助。-