iOS常用

iOS-AttributedString富文本集合

2020-11-03  本文已影响0人  JQWONG

本文用于记录工作中遇到富文本处理的方法

文本中穿插带link的链接,link部分自定义颜色(如图)

struct DisclaimerMaker {
      let text = "By clicking “Post”, I agree to The Knot’s Privacy Policy and Terms of Use"
      let policyLink = "your policy link"
      let termsOfUseLink = "terms of use link"
      let policyText = "Privacy Policy"
      let termsOfUseText = "Terms of Use"

      var attributedText: NSAttributedString {
          let paragraph = NSMutableParagraphStyle()
          paragraph.alignment = .center
          let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.subhead,
                                                           .foregroundColor: UIColor.textSubtle,
                                                           .paragraphStyle: paragraph]
          let attributedString = NSMutableAttributedString(string: text,
                                                           attributes: attributes)
          let policyRange = (text as NSString).range(of: policyText)
          let policyAttributes: [NSAttributedString.Key: Any] = [.link: policyLink,
                                                                 .font: UIFont.subhead]
          attributedString.setAttributes(policyAttributes,
                                         range: policyRange)
          let termsOfUseRange = (text as NSString).range(of: termsOfUseText)
          let termsOfUseAttributes: [NSAttributedString.Key: Any] = [.link: termsOfUseLink,
                                                                     .font: UIFont.subhead]
          attributedString.setAttributes(termsOfUseAttributes,
                                         range: termsOfUseRange)
          return attributedString
      }
  }

private(set) lazy var legalDisclaimerTextView: UITextView = {
      let textView = UITextView()
      textView.textContainerInset = .zero
      textView.isEditable = false
      textView.isScrollEnabled = false
      textView.textAlignment = .center
      textView.linkTextAttributes = [.foregroundColor: UIColor.linkOnLight,
                                     .underlineColor: UIColor.clear]
      textView.attributedText = DisclaimerMaker().attributedText
      return textView
  }()

文本中间穿插图片(如图)

优化富文本固有代码

在日常项目中,富文本最主要就是设置行高等style,无论要设置什么其他样式,文本、字体都是必要的
所以可以扩展NSMutableAttributedString把固有的代码进行一次封装,使整体看起来更加简、美观

extension NSMutableAttributedString {
      class func initWithString(_ string: String,
                                    font: UIFont,
                                    foregroundColor: UIColor,
                                    minimumLineHeight: CGFloat,
                                    maximumLineHeight: CGFloat)
        -> NSMutableAttributedString {
            let style = NSMutableParagraphStyle()
            style.maximumLineHeight = maximumLineHeight
            style.minimumLineHeight = minimumLineHeight
            return initWithString(string,
                                  font: font,
                                  foregroundColor: foregroundColor,
                                  style: style)
    }
}

这个方法返回一个设置了字体、颜色、style的NSMutableAttributedString,将UILabel或UITextView的attributedText直接赋值就可以得到原有的效果
实际使用方法

groupNameLabel.attributedText = NSMutableAttributedString.initWithString(group.name.uppercased(),
                                                                                 font: .tb_font(larsseitMedium, andFontSize: 16),
                                                                                 foregroundColor: .tb_primaryCopy(),
                                                                                 minimumLineHeight: 24,
                                                                                 maximumLineHeight: 24)

未完待续
未经授权,请勿转载
谢谢!
上一篇 下一篇

猜你喜欢

热点阅读