【问题标题】:How to add NSDictionary UIColor in iOS for TTTAttributedLabel LinkAttributes如何在 iOS 中为 TTTAttributedLabel LinkAttributes 添加 NSDictionary UIColor
【发布时间】:2015-12-21 17:56:42
【问题描述】:

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

【问题讨论】:

    标签: xamarin xamarin.ios uicolor tttattributedlabel


    【解决方案1】:

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

    【讨论】:

    • 谢谢@poupou,不幸的是它仍然不能与 .CGColor 一起使用,我想知道这个替代方案 - label.linkAttributes = @{ (id)kCTForegroundColorAttributeName: [UIColor magentaColor], (id) kCTUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleSingle] };上面的 SO 答案中提到的调用样式与此有关,我只是不确定我是否有正确的键并输入 new NSString("Whatever"), UIColor.Red.CGColor);给我带来了同样的结果。
    • 根据您的评论更新了答案,即它不能是任何 NSString 键,它必须是库要查找的内容 - 从您的评论来看,它看起来像是重用 CoreText 常量。
    • 谢谢您,先生,像魔术一样工作! - label.LinkAttributes = new NSDictionary(CoreText.CTStringAttributeKey.ForegroundColor, UIColor.Red.CGColor);
    • 最后一点,是否有挖掘您发布的CoreText Key Constant 的过程还是只是经验?我查看了代码,看不到要遵循的线索,也看不到在谷歌上搜索常量名称和 Xamarin 等...谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多