xamarin - 如何在 iOS 中为 TTTAttributedLabel LinkAttributes 添加 NSDictionary UIColor

标签 xamarin xamarin.ios uicolor tttattributedlabel

我使用了 Xamarin 绑定(bind) https://github.com/TTTAttributedLabel/TTTAttributedLabel

并且根据 - Make link in UILabel.attributedText *not* blue and *not* underlined

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

最佳答案

以下代码行取自 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/

相关文章:

ios - Xamarin iOS : alignment of fields in a TableView with LayoutConstraint (visual format)

ios - UIRefreshControl 在 Xamarin iOS 上崩溃

xamarin.ios - MonoTouch 和 TestFlight

c# - 为 UIBarButtonItem 添加/删除 EventHandler

c# - Xamarin Google Cloud Messaging GcmClient.CheckDevice(this) 导致运行时错误

android - 如何在 Xamarin 中测试所有内容

windows-phone-7 - Xamarin Monotouch 是否可以在 Microsoft Windows Phone 移动版上运行?

iphone - 如何更改sectionIndexTitlesForTableView 字母的颜色?在iphone上可以吗?

iphone - iOS 5.1 : -[UIColor colorWithPatternImage:] background color draws solid black

iOS 白色是一种颜色