Skip to content

Instantly share code, notes, and snippets.

@heronlyj
Last active May 5, 2018 05:45
Show Gist options
  • Save heronlyj/244b545a3e6ee56b965422c7d22b2e7f to your computer and use it in GitHub Desktop.
Save heronlyj/244b545a3e6ee56b965422c7d22b2e7f to your computer and use it in GitHub Desktop.
NSAttributeString+extension
//
// NSAttributeString+extension.swift
// BMKP
//
// Created by lyj on 13/03/2017.
// Copyright © 2017 bmkp. All rights reserved.
//
import Foundation
import UIKit
extension NSTextAttachment {
/// 便利构造方法,增加图片附件
///
/// - Parameter image: 图片
public convenience init(image: UIImage?) {
self.init()
self.image = image
}
}
extension NSAttributedString {
/// 基于 NSAttributedNames 的封装,请根据使用情况自行添加类型
public enum Attribute {
case font(UIFont)
case foreground(UIColor)
case background(UIColor)
case paragraph(NSParagraphStyle)
case attachment(NSTextAttachment)
case underline(NSUnderlineStyle, UIColor?)
case baselineOffset(Int)
var value: [NSAttributedStringKey:Any] { return fill() }
func fill(in result: [NSAttributedStringKey:Any] = [:]) -> [NSAttributedStringKey:Any] {
var result = result
switch self {
case .font(let font):
result[.font] = font
case .foreground(let color):
result[.foregroundColor] = color
case .background(let color):
result[.backgroundColor] = color
case .paragraph(let style):
result[.paragraphStyle] = style
case .attachment(let attachment):
result[.attachment] = attachment
case .underline(let style, let color):
result[.underlineStyle] = style.rawValue
if let color = color {
result[.underlineColor] = color
}
case .baselineOffset(let value):
result[.baselineOffset] = value
}
return result
}
}
public var range: NSRange { return NSRange(location: 0, length: length) }
}
extension NSAttributedString {
/// 便利构造函数
///
/// - Parameters:
/// - string: 字符串
/// - attribute: 枚举属性
public convenience init(string: String, attribute: Attribute) {
self.init(string: string, attributes: [attribute])
}
/// 便利构造函数
///
/// - Parameters:
/// - string: 字符串
/// - attributes: 枚举属性数组
public convenience init(string: String, attributes: [Attribute]) {
self.init(string: string, attributes: attributes.reduce([:], { $1.fill(in: $0 ?? [:]) }))
}
}
extension NSMutableAttributedString {
@discardableResult
public func set(attribute: Attribute, range: NSRange? = nil) -> Self {
addAttributes(attribute.value, range: range ?? self.range)
return self
}
@discardableResult
public func set(attributes: [Attribute], range: NSRange? = nil) -> Self {
addAttributes(attributes.reduce([:], { $1.fill(in: $0) }), range: range ?? self.range)
return self
}
/// 添加指定范围的字符串属性
///
/// - Parameters:
/// - attribute: 枚举属性
/// - range: 范围
/// - Returns: 对象本身,用于链式语法
@discardableResult
public func addAttribute(attribute: Attribute, range: NSRange? = nil) -> Self {
addAttributes(attribute.value, range: range ?? self.range)
return self
}
/// 添加指定范围的多个属性
///
/// - Parameters:
/// - attributes: 枚举属性数组
/// - range: 范围
/// - Returns: 对象本身,用于链式语法
@discardableResult
public func addAttributes(attributes: [Attribute], range: NSRange? = nil) -> Self {
addAttributes(attributes.reduce([:], { $1.fill(in: $0) }), range: range ?? self.range)
return self
}
/// 添加属性字符串到尾部
///
/// - Parameter attrString: 属性字符串
/// - Returns: 对象本身,用于链式语法
@discardableResult
public func append(attrString: NSAttributedString) -> Self {
append(attrString)
return self
}
/// 添加字符串到尾部,可以指定单个属性
///
/// - Parameters:
/// - string: 字符串
/// - attribute: 枚举属性
/// - Returns: 对象本身,用于链式语法
@discardableResult
public func append(string: String, attribute: Attribute) -> Self {
return append(string: string, attributes: [attribute])
}
/// 添加字符串到尾部,可以指定多个属性
///
/// - Parameters:
/// - string: 字符串
/// - attributes: 枚举属性数组
/// - Returns: 对象本身,用于链式语法
@discardableResult
public func append(string: String, attributes: [Attribute] = []) -> Self {
append(NSMutableAttributedString(string: string, attributes: attributes))
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment