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)时,生成的 Color 将是白色,因为它会将任何超过 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。

希望这有帮助.-

最新更新