一. range和NSRange互相轉(zhuǎn)換
extension String {
/// range轉(zhuǎn)換為NSRange
func nsRange(from range: Range<String.Index>) -> NSRange {
return NSRange(range, in: self)
}
//使用實例
// let languages = "Java,Swift,Objective-C"
// let one = "Swift"
// let range = languages.range(of: one)
// let nsRange = languages.nsRange(from: range!)
// print(nsRange) // {5, 5}
}
extension String {
/// NSRange轉(zhuǎn)化為range
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
}
}
二.NSMutableAttributedString快速添加屬性
//
// NSMutableAttributedString+Extension.swift
// PXSSwift
//
// Created by 劉劉峰 on 2020/12/21.
// Copyright ? 2020 劉劉峰. All rights reserved.
//
import Foundation
extension NSMutableAttributedString {
// =========================字體的設(shè)置=========================//
/// 設(shè)置字體大小
public func lf_addFont(_ font: UIFont, on range: NSRange) {
if self.length < range.location + range.length { return }
let attrs = [NSAttributedString.Key.font: font]
self.addAttributes(attrs, range: range)
}
/// 設(shè)置字體顏色
public func lf_addForegroundColor(_ color: UIColor, range: NSRange) {
if self.length < range.location + range.length {
return
}
let attrs = [NSAttributedString.Key.foregroundColor: color]
self.addAttributes(attrs, range: range)
}
/// 設(shè)置字體的背景顏色
public func lf_addBackgroundColor(_ color: UIColor, range: NSRange) {
if self.length < range.location + range.length {
return
}
let attrs = [NSAttributedString.Key.backgroundColor: color]
self.addAttributes(attrs, range: range)
}
}
extension NSMutableAttributedString {
//=========================文本的處理==========================//
/// 字符間距的設(shè)置 字符間距 正值間距加寬,負(fù)值間距變窄
public func lf_addKern(_ kern: Int, range: NSRange) {
if self.length < range.location + range.length { return }
let attrs = [NSAttributedString.Key.kern: kern]
self.addAttributes(attrs, range: range)
}
/// 設(shè)置字體傾斜度,正值右傾,負(fù)值左傾
public func lf_addObliqueness(_ obliqueness: CGFloat, range: NSRange) {
if self.length < range.location + range.length { return }
let attrs = [NSAttributedString.Key.obliqueness: obliqueness]
self.addAttributes(attrs, range: range)
}
/// 設(shè)置字體的橫向拉伸,正值拉伸 ,負(fù)值壓縮
public func lf_addExpansion(_ expansion: CGFloat, range: NSRange) {
if self.length < range.location + range.length { return }
let attrs = [NSAttributedString.Key.expansion: expansion]
self.addAttributes(attrs, range: range)
}
/// 文字書寫方向
public func lf_addWritingDirection(_ direction: NSWritingDirection, range: NSRange) {
if self.length < range.location + range.length { return }
var value: (Int) = (0)
if direction == .leftToRight {
if #available(iOS 9.0, *) {
value = (NSWritingDirectionFormatType.override.rawValue | NSWritingDirection.leftToRight.rawValue)
} else {
// Fallback on earlier versions
}
} else {
if #available(iOS 9.0, *) {
value = (NSWritingDirectionFormatType.override.rawValue | NSWritingDirection.rightToLeft.rawValue)
} else {
// Fallback on earlier versions
}
}
/**
NSAttributedString.Key.writingDirection接收的是一個數(shù)組
*/
/**
//@[@(NSWritingDirectionLeftToRight | NSWritingDirectionEmbedding)]
//@[@(NSWritingDirectionLeftToRight | NSWritingDirectionOverride)]
//@[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)]
//@[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)]
// NSWritingDirectionOverride 和 NSWritingDirectionEmbedding 是指定Unicode雙向定義的格式控制算法(具體的沒太搞清楚)
*/
let attrs = [NSAttributedString.Key.writingDirection: [value]]
self.addAttributes(attrs, range: range)
}
}
extension NSMutableAttributedString {
//=========================刪除線==========================//
/// 設(shè)置刪除線 NSUnderlineStyle: none不設(shè)置,single單細(xì)線刪除,thick粗單線, Double雙細(xì)線
public func lf_addStrikethrough(style: NSUnderlineStyle = .single, color: UIColor? = nil, range: NSRange) {
if self.length < range.location + range.length {
return
}
let attr1 = [NSAttributedString.Key.strikethroughStyle: style.rawValue]
self.addAttributes(attr1, range: range)
if let tempColor = color {
let attr2 = [NSAttributedString.Key.strikethroughColor: tempColor]
self.addAttributes(attr2, range: range)
}
}
//=========================下劃線==========================//
/// 設(shè)置下劃線 NSUnderlineStyle: none不設(shè)置,single單細(xì)線刪除,thick粗單線, Double雙細(xì)線
public func lf_addUnderLine(style: NSUnderlineStyle = .single, color: UIColor? = nil, range: NSRange) {
if self.length < range.location + range.length {
return
}
let attrs = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue]
self.addAttributes(attrs, range: range)
if let tempColor = color {
let attr2 = [NSAttributedString.Key.underlineColor: tempColor]
self.addAttributes(attr2, range: range)
}
}
}
extension NSMutableAttributedString {
//=========================文字的效果==========================//
/// 設(shè)置文字的描邊
public func lf_addStroke(color: UIColor, width: CGFloat, range: NSRange) {
/**
設(shè)置文字描邊顏色,需要NSStrokeWidthAttributeName設(shè)置描邊寬度,這樣就能使文字空心.
NSStrokeWidthAttributeName,該值改變筆畫寬度(相對于字體 size 的百分比),負(fù)值填充效果,正值中空效果,默認(rèn)為 0,即不改變。正數(shù)只改變描邊寬度。負(fù)數(shù)同時改變文字的描邊和填充寬度。例如,對于常見的空心字,這個值通常為 3.0。
同時設(shè)置了空心描邊和文字前景兩個屬性,并且NSStrokeWidthAttributeName 屬性設(shè)置為整數(shù),文字前景色就無效果了
*/
if self.length < range.location + range.length { return }
let attrs = [
NSAttributedString.Key.strokeWidth: width,
NSAttributedString.Key.strokeColor: color
] as [NSAttributedString.Key : Any]
self.addAttributes(attrs, range: range)
}
/// 陰影
public func lf_addShadow(_ shadow: NSShadow, range: NSRange) {
if self.length < range.location + range.length { return }
let attrs = [NSAttributedString.Key.shadow: shadow]
self.addAttributes(attrs, range: range)
}
/// 設(shè)置文字的特殊NSAttributedString.Key這一個凸版印刷效果)
public func lf_addTextEffect(effect: NSAttributedString.TextEffectStyle = .letterpressStyle, range: NSRange) {
if self.length < range.location + range.length { return }
let attrs = [NSAttributedString.Key.textEffect: effect]
self.addAttributes(attrs, range: range)
}
}
extension NSMutableAttributedString {
//=========================偏移量==========================//
/// 設(shè)置上下偏移量 正數(shù)上移,負(fù)數(shù)下移
public func lf_addBaselineOffset(_ offset: CGFloat, range: NSRange) {
if self.length < range.location + range.length { return }
let attrs = [NSAttributedString.Key.baselineOffset: offset]
self.addAttributes(attrs, range: range)
}
/// 設(shè)置行間距
public func lf_addLineSpacing(_ style: NSMutableParagraphStyle, range: NSRange) {
/**
具體請看: http://m.itdecent.cn/p/b0afc45bb642
NSAttributedString.Key.paragraphStyle 設(shè)置文本段落排版,為NSParagraphStyle對象
paragraphStyle.lineSpacing = 0.0; //增加行高
paragraphStyle.headIndent = 0; //頭部縮進(jìn),相當(dāng)于左padding
paragraphStyle.tailIndent = 0; //相當(dāng)于右padding
paragraphStyle.lineHeightMultiple = 0; //行間距是多少倍
paragraphStyle.alignment = NSTextAlignmentLeft;//對齊方式
paragraphStyle.firstLineHeadIndent = 0; //首行頭縮進(jìn)
paragraphStyle.paragraphSpacing = 0; //段落后面的間距
paragraphStyle.paragraphSpacingBefore = 0; //段落之前的間距
*/
if self.length < range.location + range.length { return }
let attrs = [NSAttributedString.Key.paragraphStyle: style]
self.addAttributes(attrs, range: range)
}
}
extension NSMutableAttributedString {
/// 插入圖片
public func lf_addTextAttachment(image: UIImage?, imageFrame: CGRect, atIndex index: Int) {
let attachment = NSTextAttachment.init()
attachment.image = image
attachment.bounds = imageFrame
let att = NSAttributedString(attachment: attachment)
self.insert(att, at: index)
}
}
三.項目中使用
let attr = NSMutableAttributedString.init(string: str)
let range = str.range(of: self.model?.view_rent_un_prefix ?? "")
let nsrange = str.nsRange(from: range!)
attr.lf_addForegroundColor(AppColor.themeColor, range: nsrange)
descLab.attributedText = attr