Swift3:NSMutableAttributeString的Range和NSRange问题

本文下方封装了简单的属性字符串方法。封装中只实现了最基础的功能,用来设置一个string中的某些subString特殊显示。调用方法为链式调用。

—> 下面来说说使用NSMutableAttributeString遇到的问题

要特殊显示的subString可以调用

open func addAttributes(_ attrs: [String : Any] = [:], range: NSRange)

来设置,注意这里的range类型是NSRange,我们在获取这个range的时候想当然会这样做:

//其实这时候获取的range类型是Range
let range = attributeStr.string.range(of: "subString")

Range是无法通过 as? 强制转换到NSRange的。

方式1. 比较简单的处理方式就是

//1.先把String转换成 NSStiring
let nsString = NSString(string: attributeStr.string)

//2.NSString中的range方法获取到的是NSRange类型
let nsRange = nsString.range(of: "subString")

方式2. 另外比较复杂的处理就是把Range转换成NSRange了:

//range转换为NSRange  
//扩展的是String类,不可改为NSRange或者Range的扩展,因为samePosition,utf16是String里的
extension String {
    func nsRange(from range: Range<String.Index>) -> NSRange {
        let from = range.lowerBound.samePosition(in: utf16)
        let to = range.upperBound.samePosition(in: utf16)
        return NSRange(location: utf16.distance(from: utf16.startIndex, to: from),
                       length: utf16.distance(from: from, to: to))
    }
}
//NSRange转化为range
//扩展的是String类,不可改为NSRange或者Range的扩展,因为utf16是String里的
extension String {
    func range(from nsRange: NSRange) -> Range<String.Index>? {
        guard
            let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
            let to16 = utf16.index(from16, offsetBy: nsRange.length, limitedBy: utf16.endIndex),
            let from = String.Index(from16, within: self),
            let to = String.Index(to16, within: self)
            else { return nil }
        return from ..< to
    }
}

我的简书上同步更新了本文章,点击查看


最终代码:Github代码地址
import Foundation
import UIKit
class TAttributeString {

    var attributeStr : NSMutableAttributedString =  NSMutableAttributedString()

    //设置整体的字符串
    func setString(string: String,color: UIColor, fontSize: CGFloat) -> Self {

        return setString(string: string, color: color, font: UIFont.systemFont(ofSize: fontSize))
    }

    func setString(string: String, color: UIColor, font: UIFont) -> Self {

        let inceptionAttStr = NSAttributedString(string: string, attributes: [NSFontAttributeName:font,NSForegroundColorAttributeName:color])

        attributeStr.setAttributedString(inceptionAttStr)

        return self
    }
}

//设置属性字符串中的 特殊显示的字符
extension TAttributeString {

    func height(string: String, color: UIColor) -> Self {
        let nsString = NSString(string: attributeStr.string)

        attributeStr.setAttributes([NSForegroundColorAttributeName:color], range: nsString.range(of: string))

        return self
    }

    func height(string: String, color: UIColor, fontSize: CGFloat) -> Self {

        return height(string: string, color: color, font: UIFont.systemFont(ofSize: fontSize))
    }

    func height(string: String, color: UIColor, font: UIFont) -> Self {

        let nsString = NSString(string: attributeStr.string)

        attributeStr.setAttributes([NSFontAttributeName:font, NSForegroundColorAttributeName:color], range: nsString.range(of: string))

        return self
    }

}

调用方式

 label.attributedText = TAttributeString().setString(string: "swift:这里只是封装了最常用的功能,采用简单的链式调用,核心方法很少,都是一些方便调用的衍生方法", color: UIColor.black, fontSize: 14).height(string: "swift:", color: UIColor.red, fontSize: 20).height (string: "链式调用", color: UIColor.blue, font: UIFont.boldSystemFont(ofSize: 10)).attributeStr

效果:

显示效果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值