123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- //
- // Extension+NSMutableAttributedString.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2018/10/19.
- // Copyright © 2018 南鑫林. All rights reserved.
- //
- import UIKit
- extension NSMutableAttributedString {
- // MARK: 完全自定义样式
- public func changeAttributeDict(_ dict: [AnyHashable : Any]?, range: NSRange) {
- if let dict = dict as? [NSAttributedString.Key : Any] {
- addAttributes(dict, range: range)
- }
- }
- // MARK: 设置字体颜色,取值为 UIColor,默认为黑色
- public func changeForegroundColor(_ color: UIColor?, range: NSRange) {
- addAttribute(.foregroundColor, value: color!, range: range)
- }
- // MARK: 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
- //#warning NSForegroundColorAttributeName 和 NSBackgroundColorAttributeName 的低位是相等的,跟前面介绍的 textColor 一样,哪个属性最后一次赋值,就会冲掉前面的效果
- public func changeBackgroundColor(_ color: UIColor?, range: NSRange) {
- addAttribute(.backgroundColor, value: color!, range: range)
- }
- // MARK: 改变某位置 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
- public func changeSystemFont(_ font: UIFont?, range: NSRange) {
- addAttribute(.font, value: font!, range: range)
- }
- // MARK: 设置字形倾斜度,取值为 NSNumber(float),正值右倾,负值左倾
- public func changeObliquenessValue(_ value: NSNumber?, range: NSRange) {
- addAttribute(.obliqueness, value: value!, range: range)
- }
- // MARK: NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本 在 iOS 中,总是使用横排文本,0 以外的值都未定义
- public func changeVerticalGlyphFormValue(_ value: NSNumber?, range: NSRange) {
- addAttribute(.verticalGlyphForm, value: value!, range: range)
- }
- // MARK: NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写
- public func changeWritingDirectionStyle(_ style: NSWritingDirection) {
- addAttribute(.writingDirection, value: [NSWritingDirection.rightToLeft], range: NSRange(location: 0, length: length))
- }
- // MARK: 改变某位置的行距
- public func changeLineSpacing(_ spacing: CGFloat, from loc: Int, length: Int) {
- let range = NSRange(location: loc, length: length)
- let style = NSMutableParagraphStyle()
- // 行距
- style.lineSpacing = spacing
- addAttribute(.paragraphStyle, value: style, range: range)
- }
- // MARK: 改变某位置的段落距离
- public func changeParagraphSpacing(_ spacing: CGFloat, from loc: Int, length: Int) {
- let range = NSRange(location: loc, length: length)
- let style = NSMutableParagraphStyle()
- // 段落距离
- style.paragraphSpacing = spacing
- addAttribute(.paragraphStyle, value: style, range: range)
- }
- // MARK: 改变段落的顶部和文本内容的开头之间的距离
- public func change(beforeLparagraphSpacing spacing: CGFloat, from loc: Int, length: Int) {
- let range = NSRange(location: loc, length: length)
- let style = NSMutableParagraphStyle()
- // 段落的顶部和文本内容的开头之间的距离
- style.paragraphSpacingBefore = spacing
- addAttribute(.paragraphStyle, value: style, range: range)
- }
- // MARK: 设置下划线样式:根据枚举选择
- public func change(_ style: NSUnderlineStyle, color: UIColor?, range: NSRange) {
- addAttribute(.underlineStyle, value: style.rawValue, range: range)
- addAttribute(.underlineColor, value: color ?? "", range: range)
- }
- // MARK: 全部加下划线 设置下划线样式:根据枚举选择
- public func changeUnderline(atAllStyle style: NSUnderlineStyle, color: UIColor?) {
- let range = NSRange(location: 0, length: length)
- change(style, color: color, range: range)
- }
- // MARK: 设置删除线样式:根据枚举选择
- public func changeStrikethroughStyle(_ style: NSUnderlineStyle, color: UIColor?, range: NSRange) {
- addAttribute(.strikethroughStyle, value: style.rawValue, range: range)
- addAttribute(.strikethroughColor, value: color ?? "", range: range)
- }
- // MARK: 全部添加删除线 设置删除线样式:根据枚举选择
- public func changeStrikethrough(atAllStyle style: NSUnderlineStyle, color: UIColor?) {
- let range = NSRange(location: 0, length: length)
- changeStrikethroughStyle(style, color: color, range: range)
- }
- // MARK: 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
- public func changeKern(withValue value: NSNumber?, range: NSRange) {
- addAttribute(.kern, value: value ?? "", range: range)
- }
- // MARK: 根据位置设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
- public func changeExpansion(withValue value: NSNumber?, range: NSRange) {
- addAttribute(.expansion, value: value ?? "", range: range)
- }
- // MARK: 根据位置添加阴影效果
- public func changeShadow(with shadow: NSShadow?, range: NSRange) {
- addAttribute(.shadow, value: shadow ?? "", range: range)
- // 排版
- addAttribute(.verticalGlyphForm, value: NSNumber(value: 0), range: range)
- }
- // MARK: 根据位置修改描边颜色 垂直标志符号形式
- public func changeStrokeColor(with strokeColor: UIColor?, strokeWidth: NSNumber?, range: NSRange) {
- addAttribute(.strokeColor, value: strokeColor ?? "", range: range)
- addAttribute(.strokeWidth, value: strokeWidth ?? "", range: range)
- }
- /// 实例化 NSMutableAttributedString 字符串
- ///
- /// - Parameters:
- /// - texts: 用于存储所需设置字符串的数组(根据所需自定义类型分组)
- /// - attrsArray: 富文本属性样式的集合(数组元素是texts的相应文本的属性--字典)
- /// - space: texts文本之间的间距数组 1 代表一个空格的距离
- /// - Returns: NSMutableAttributedString
- public convenience init(strings texts: [String]?, attributesArray attrsArray: [[String : Any?]]?, space: [NSNumber]?) {
- self.init()
- go(texts, attributesArray: attrsArray, space: space)
- }
- /// 实例化 NSMutableAttributedString 字符串
- ///
- /// - Parameters:
- /// - texts: 用于存储所需设置字符串的数组(根据所需自定义类型分组)
- /// - attrsArray: 富文本属性样式的集合(数组元素是texts的相应文本的属性--字典)
- /// - space: texts文本之间的间距数组 1 代表一个空格的距离
- /// - Returns: NSMutableAttributedString
- public func go(_ textArray: [Any]?, attributesArray attrsArray: [Any]?, space: [NSNumber]?) {
- (textArray as NSArray?)?.enumerateObjects({[weak self] obj, idx, stop in
- let attrsCount: Int? = attrsArray?.count
- if idx <= ((attrsCount ?? 0) - 1) {
- self?.append(NSAttributedString(string: obj as? String ?? "", attributes: attrsArray?[(attrsCount ?? 0) - 1] as? [NSAttributedString.Key : Any]))
- } else {
- self?.append(NSAttributedString(string: obj as? String ?? "", attributes: attrsArray?[(attrsCount ?? 0) - 1] as? [NSAttributedString.Key : Any]))
- }
- if idx != (textArray?.count ?? 0) - 1 && idx <= (space?.count ?? 0) - 1 {
- if let space = self?.spaceWidth(withNumberBlackSpace: space?[idx]) {
- self?.append(space)
- }
- } else {
- if let space = self?.spaceWidth(withNumberBlackSpace: nil) {
- self?.append(space)
- }
- }
- })
- }
- /// 字间距
- ///
- /// - Parameter number: 空格
- /// - Returns: NSMutableAttributedString
- public func spaceWidth(withNumberBlackSpace number: NSNumber?) -> NSMutableAttributedString? {
- var string = ""
- let count: Int = number?.intValue ?? 0
- for _ in 0..<count {
- string = string + (" ")
- }
- let attributedString = NSMutableAttributedString(string: string)
- return attributedString
- }
- }
|