NSAttributedString
2016-09-22 本文已影响245人
Laughingg
NSAttributedString 对象管理字符和字符串关联的属性(例如,字体和字距调整), 使用单个字符或字符串。 通过属性字符串对字符和字符属性进行关联。
属性字符有两个公共类簇 NSAttributedString 和 NSMutableAttributedString。一个是字符可变的一个是字符不可变的。
@available(iOS 3.2, *)
open class NSAttributedString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
// 获取字符的长度
open var string: String { get }
// 获取属性数组
open func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [String : Any]
}
extension NSAttributedString {
// 获取字符长度
open var length: Int { get }
// 获取属性
open func attribute(_ attrName: String, at location: Int, effectiveRange range: NSRangePointer?) -> Any?
// 获取属性字字符串
open func attributedSubstring(from range: NSRange) -> NSAttributedString
open func attributes(at location: Int, longestEffectiveRange range: NSRangePointer?, in rangeLimit: NSRange) -> [String : Any]
open func attribute(_ attrName: String, at location: Int, longestEffectiveRange range: NSRangePointer?, in rangeLimit: NSRange) -> Any?
// 比较连个字符串是否相等
open func isEqual(to other: NSAttributedString) -> Bool
/******************* 构造方法 *********************/
// 通过一个字符串创建
public init(string str: String)
// 通过字符串 + 属性文本创建
public init(string str: String, attributes attrs: [String : Any]? = nil)
// 通过一个属性字符串创建
public init(attributedString attrStr: NSAttributedString)
@available(iOS 4.0, *)
open func enumerateAttributes(in enumerationRange: NSRange, options opts: NSAttributedString.EnumerationOptions = [], using block: ([String : Any], NSRange, UnsafeMutablePointer<ObjCBool>) -> Swift.Void)
@available(iOS 4.0, *)
open func enumerateAttribute(_ attrName: String, in enumerationRange: NSRange, options opts: NSAttributedString.EnumerationOptions = [], using block: (Any?, NSRange, UnsafeMutablePointer<ObjCBool>) -> Swift.Void)
}
extension NSAttributedString {
public struct EnumerationOptions : OptionSet {
public init(rawValue: UInt)
public static var reverse: NSAttributedString.EnumerationOptions { get }
public static var longestEffectiveRangeNotRequired: NSAttributedString.EnumerationOptions { get }
}
}
@available(iOS 3.2, *)
open class NSMutableAttributedString : NSAttributedString {
open func replaceCharacters(in range: NSRange, with str: String)
open func setAttributes(_ attrs: [String : Any]?, range: NSRange)
}
extension NSMutableAttributedString {
// 获取可变字符串
open var mutableString: NSMutableString { get }
// attribute 的操作
open func addAttribute(_ name: String, value: Any, range: NSRange)
open func addAttributes(_ attrs: [String : Any] = [:], range: NSRange)
open func removeAttribute(_ name: String, range: NSRange)
// 字符串(字符)的操作方法(增删改查)
open func replaceCharacters(in range: NSRange, with attrString: NSAttributedString)
open func insert(_ attrString: NSAttributedString, at loc: Int)
open func append(_ attrString: NSAttributedString)
open func deleteCharacters(in range: NSRange)
open func setAttributedString(_ attrString: NSAttributedString)
open func beginEditing()
open func endEditing()
}