2

我为https://github.com/TTTAttributedLabel/TTTAttributedLabel使用了 Xamarin 绑定

并按照 - 在 UILabel.attributedText *not* 蓝色和 *not* 下划线中创建链接

self.label.linkAttributes = @{NSForegroundColorAttributeName: color, 
                               NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)};

我想设置我的链接属性,我只是不确定语法 -

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/AttributedStrings/Articles/standardAttributes.html

我已经尝试过这些变化 -

 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 ,因为它看不到它的可用类型,它正在做“某事”,因为它用这段代码擦除了我的下划线+蓝色。

4

1 回答 1

2

以下代码行取自您链接的项目的 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 常量,那么这就是要使用的值。

于 2015-12-21T20:49:05.440 回答