我使用了 Xamarin 绑定(bind) https://github.com/TTTAttributedLabel/TTTAttributedLabel
并且根据 - Make link in UILabel.attributedText *not* blue and *not* underlined
self.label.linkAttributes = @{NSForegroundColorAttributeName: color,
NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)};
我想设置我的链接属性,我只是不确定语法 -
我尝试过这些的变体 -
label.LinkAttributes = new NSDictionary(
new NSString("NSForegroundColorAttributeName"), UIColor.Red,
new NSString("NSUnderlineStyleAttributeName"), new NSUnderlineStyle() == NSUnderlineStyle.Double);
label.LinkAttributes = new NSDictionary(
new NSString("NSForegroundColorAttributeName"), UIColor.Red,
new NSString("NSUnderlineStyleAttributeName"), new NSNumber(2));
但不工作。不确定如何传入 UIColor,因为无法看到其可用的类型,它正在做“某事”,因为它用此代码删除了我的下划线 + 蓝色。
最佳答案
以下代码行取自 github README.md来自您链接的项目。
(id)kCTForegroundColorAttributeName : (id)[UIColor redColor].CGColor,
看起来该库处理的是 CGColor
而不是 UIColor
(或 NSColor
仅适用于 OSX) 。有(太多)多种方法来表示颜色,遗憾的是,大多数 API 只能使用一种方法。在这种情况下,您需要使用:
UIColor.Red.CGColor
而不是:
UIColor.Red
在您为标签的 LinkAttributes
属性指定的字典中。
键kCTForegroundColorAttributeName
(来自您的评论)还必须与Apple的常量值匹配,该值可以与常量的名称不同。在 Xamarin.iOS 中,此常量公开为:
CoreText.CTStringAttributeKey.ForegroundColor
因此,如果库(重新)使用 CoreText 常量,那么这就是要使用的值。
关于xamarin - 如何在 iOS 中为 TTTAttributedLabel LinkAttributes 添加 NSDictionary UIColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34401669/