【问题标题】:getting NSAttributedText UIKit additions to work让 NSAttributedText UIKit 添加工作
【发布时间】:2014-04-03 20:27:40
【问题描述】:

请参阅下面的编辑#1,它比原始问题更好地隔离了问题

我正在尝试让 linkTextAttributes 为 UITextView 工作并且对此感到非常沮丧 - 这可能是一个直接的 Obj-C 问题。我宁愿不去 NSAttribtedString 但可能不得不去。我只是想删除链接上的下划线。这有效:

 textview.linkTextAttributes = @{NSForegroundColorAttributeName : [UIColor yellowColor]};

要让它变黄,但我需要取出下属。看起来应该是这样的:

 textview.linkTextAttributes = @{NSForegroundColorAttributeName : [UIColor yellowColor],
    NSUnderlineStyleAttributeName: NSUnderlineStyleNone};

但我得到以下有点神秘的错误:

我试过了:

      textview.linkTextAttributes = @{
                                      NSForegroundColorAttributeName : [UIColor yellowColor],
                                      NSUnderlineStyleAttributeName: [NSNumber numberWithInt: NSUnderlineStyleNone]};

删除错误但不删除下划线。关于如何解决这个问题的任何想法?或者上面的语法是否正确?

编辑#1

所以我试图将问题更多地隔离到一个新项目中。这是来自主 UIViewController 的 viewDidLoad;只是试图删除下划线:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITextView *textview= [[UITextView alloc]initWithFrame:CGRectMake(50.0F, 50.0F , 200.0f, 100.0f)];
    [textview setValue:@"here is my thinking <a href='http://www.some.com'>me</a>" forKey:@"contentToHTMLString"];
    textview.layer.borderWidth = 5.0f;
    textview.layer.borderColor = [[UIColor grayColor] CGColor];
    textview.dataDetectorTypes = UIDataDetectorTypeLink;
    textview.linkTextAttributes = @{
                                    NSForegroundColorAttributeName : [UIColor blueColor],
                                    NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)};
    [self.view addSubview:textview];
}

和输出:

【问题讨论】:

    标签: ios ios7 nsstring nsattributedstring


    【解决方案1】:

    改成这样:

    textview.linkTextAttributes = @{
                                    NSForegroundColorAttributeName : [UIColor yellowColor],
                                     NSUnderlineStyleAttributeName : @(NSUnderlineStyleNone)};
    

    【讨论】:

    • thx - 感谢您的回答和帮助,但这对我不起作用。我很紧张我有副作用,所以我将所有代码隔离到 viewDidLoad 并在上面提供了屏幕截图(并更改为蓝色以便更容易查看屏幕截图)。关于如何消除下划线的任何想法?
    • 啊,所以您只是想让神奇的链接不再是链接吗?还是您希望它仍然是一个链接,但显然不是这样?
    • 感谢回复。因此,我们通过textview.selectable = YES; textview.userInteractionEnabled = NO; 关闭了原始链接,并希望删除下划线(如果人为可能,将其加粗):-)
    【解决方案2】:

    希望其他人在 Swift 中尝试同样的方法:

    如果您在 ADC 站点中搜索“NSUnderlineStyleNone”或“NSUnderlineStyleSingle”,您只会找到:

    NSUnderlineStyleNone 不要画下划线。

    在 OS X v10.3 及更高版本中可用。

    :(

    所以 快速补丁 用于 swift:

    var underline : NSNumber = NSNumber(bool: false) // NSUnderlineStyleNone

    underline = NSNumber(bool: true) // NSUnderlineStyleSingle

    然后以这种方式调用它:

        let font = ....fontOfSize(18)!
        let attrString1 = NSAttributedString(
            string: "www.apple.com",
            attributes: [ NSFontAttributeName: font
                , NSForegroundColorAttributeName: UIColor.whiteColor()
                , NSUnderlineStyleAttributeName: underline
            ]
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多