• Change the color of a link in an NSMutableAttributedString


    Swift

    Updated for Swift 3

    Use with a textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.green]

    And in context:

    let attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_")

    let linkRange = (attributedString.string as NSString).range(of: "@marcelofabri_")

    attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: linkRange)

    let linkAttributes: [String : Any] = [

        NSForegroundColorAttributeName: UIColor.green,

        NSUnderlineColorAttributeName: UIColor.lightGray,

        NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue]

    // textView is a UITextView

    textView.linkTextAttributes = linkAttributes

    textView.attributedText = attributedString

    textView.delegate = self

    Swift 4:

    let linkAttributes: [String : Any] = [

        NSAttributedStringKey.foregroundColor.rawValue: UIColor.green,

        NSAttributedStringKey.underlineColor.rawValue: UIColor.lightGray,

        NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue]

    Objective-C

    Use with a textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor greenColor]};

    Source: this answer

    And from this post:

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];

    [attributedString addAttribute:NSLinkAttributeName

                             value:@"username://marcelofabri_"

                             range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];

    NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],

                                     NSUnderlineColorAttributeName: [UIColor lightGrayColor],

                                     NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};

    // assume that textView is a UITextView previously created (either by code or Interface Builder)

    textView.linkTextAttributes = linkAttributes; // customizes the appearance of links

    textView.attributedText = attributedString;

    textView.delegate = self;

     https://stackoverflow.com/questions/28361072/change-the-color-of-a-link-in-an-nsmutableattributedstring

  • 相关阅读:
    ASP.NET 判断GRIDVIEW的checkbox是否选中
    分享C#实现XML和实体序列化和反序列化的代码
    设计模式:简单工厂、工厂方法、抽象工厂之小结与区别 (转)
    如何验证已经加载的symbol file与module是否匹配?
    成功运行过的WinDBG Commands–12262010
    间歇性连接数据库失败, 先试试下面两篇文章
    如何使用符号文件?
    为<<Advanced Windows Debugging>>配置符号路径
    TCP中Connection和端口的关系
    SQL Profiler Trace中的列SPID
  • 原文地址:https://www.cnblogs.com/feng9exe/p/9402372.html
Copyright © 2020-2023  润新知