Make link in UILabel.attributedText *not* blue and *not* underlined

前端 未结 11 1262
终归单人心
终归单人心 2020-12-24 05:18

I want some words within my OHAttributedLabel to be links, but I want them to be colors other than blue and I don\'t want the underline.

This is giving me a blue lin

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 06:05

    It's better to use UITextView with "Link" feature enabled. In this case you can do it with one line:

    Swift 4:

    // apply link attributes to label.attributedString, then
    textView.tintColor = UIColor.red // any color you want
    

    Full example:

        let attributedString = NSMutableAttributedString(string: "Here is my link")
        let range = NSRange(location: 7, length:4)
        attributedString.addAttribute(.link, value: "http://google.com", range: range)
        attributedString.addAttribute(.underlineStyle, value: 1, range: range)
        attributedString.addAttribute(.underlineColor, value: UIColor.red, range: range)
        textView.tintColor = UIColor.red // any color you want
    

    Or you can apply attributes to links only:

      textView.linkTextAttributes = [
          .foregroundColor: UIColor.red
          .underlineStyle: 1,
          .underlineColor: UIColor.red
       ]
    

提交回复
热议问题