//
|
// NSAttributedStringExtension.swift
|
// THProject
|
//
|
// Created by alvin_y on 2018/3/27.
|
// Copyright © 2018年 yang-wang. All rights reserved.
|
//
|
|
import UIKit
|
|
public extension String {
|
var attributed:NSAttributedString {
|
return NSAttributedString.init(string: self)
|
}
|
}
|
|
public extension NSAttributedString {
|
|
// 返回所有的字体属性
|
func attributes() -> [NSAttributedString.Key: Any] {
|
return self.attributes(at: 0, longestEffectiveRange: nil, in: self.attributedStringRange(nil))
|
}
|
|
// 修改字体
|
func font(_ font: UIFont, range: NSRange? = nil) -> NSAttributedString {
|
let mutableAttributedString = NSMutableAttributedString(string: self.string, attributes: self.attributes())
|
mutableAttributedString.addAttribute(NSAttributedString.Key.font, value: font, range: attributedStringRange(range))
|
return mutableAttributedString as NSAttributedString
|
}
|
|
// 修改字体颜色
|
func foregroundColor(_ foregroundColor: UIColor, range: NSRange? = nil) -> NSAttributedString {
|
let mutableAttributedString = NSMutableAttributedString(string: self.string, attributes: self.attributes())
|
mutableAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: foregroundColor, range: attributedStringRange(range))
|
return mutableAttributedString as NSAttributedString
|
}
|
|
// 修改文本背景颜色
|
func backgroundColor(_ backgroundColor: UIColor, range: NSRange? = nil) -> NSAttributedString {
|
let mutableAttributedString = NSMutableAttributedString(string: self.string, attributes: self.attributes())
|
mutableAttributedString.addAttribute(NSAttributedString.Key.backgroundColor, value: backgroundColor, range: attributedStringRange(range))
|
return mutableAttributedString as NSAttributedString
|
}
|
|
// 字距调整
|
func kern(_ kern: Float, range: NSRange? = nil) -> NSAttributedString {
|
let mutableAttributedString = NSMutableAttributedString(string: self.string, attributes: self.attributes())
|
mutableAttributedString.addAttribute(NSAttributedString.Key.kern, value: kern, range: attributedStringRange(range))
|
return mutableAttributedString as NSAttributedString
|
}
|
|
// 添加删除线
|
func strikethroughStyle(_ strikethroughStyle: Int, range: NSRange? = nil) -> NSAttributedString {
|
let mutableAttributedString = NSMutableAttributedString(string: self.string, attributes: self.attributes())
|
mutableAttributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: strikethroughStyle, range: attributedStringRange(range))
|
return mutableAttributedString as NSAttributedString
|
}
|
|
// 设置下划线
|
func underlineStyle(_ underlineStyle: NSUnderlineStyle, range: NSRange? = nil) -> NSAttributedString {
|
let mutableAttributedString = NSMutableAttributedString(string: self.string, attributes: self.attributes())
|
mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: underlineStyle, range: attributedStringRange(range))
|
return mutableAttributedString as NSAttributedString
|
}
|
|
//
|
private func attributedStringRange(_ range: NSRange?) -> NSRange {
|
return range ?? NSRange(location: 0, length: self.string.count)
|
}
|
|
}
|