NSTextStorage 定义了 TextKit 最基本的存储机制。这个类是 NSMutableAttributedString 的半具体的子类(我特么也没看明白是啥子意思),并添加一组 NSLayoutManager 的管理行为。一个文本存储对象当字符或属性发生改变的时候会通知布局管理者,让布局管理者在需要的时候重新显示文本。
概览
NSTextSorage 对象能够在任何线程进行存取。但是你必须保证在同一时间只能在一个线程进行存取。
子类化注意事项
NSTextSorage 类实现改变管理(通过 beginEditing() 和 endEditing() ),验证属性,代理回调,布局管理通知。NSTextSorage 类是不完整的,这个类没有实现实际的属性字符串存储,子类通过重载 NSAttributedString 的两个函数进行管理:
string
attributes(at:effectiveRange:)
子类化也必须同时重载 NSMutableAttributedString 的两个函数:
replaceCharacters(in:with:)
setAttributes(_:range:)
这些函数完成改变后,然后就会调用 edited(_:range:changeInLength:)
去通知父类知道 改变已经发生。
// 接收器的内容字符
/*
依附的字符串是不能通过这个属性的值进行移除。
由于性能的考虑,这个属性只会返回当前背后存储的属性字符串对象。如果你想要去维持这个你操作后返回字符串的快照,你需要对子字符串进行拷贝。
这个函数属性必须保证有效的去存储字符到属性字符串,子类必须实现这个属性。
*/
var string: String { get }
给指定索引的字符返回属性
func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [String : Any]
根据给定的返回和给定的字符串替换字符
func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [String : Any]
根据给定的范围和属性数组设置属性
func setAttributes(_ attrs: [String : Any]?, range: NSRange)
变化类型
@available(iOS 7.0, *)
public struct NSTextStorageEditActions : OptionSet {
public init(rawValue: UInt)
// 属性被添加, 移除,改变
public static var editedAttributes: NSTextStorageEditActions { get }
// 字符串被添加,移除,替换
public static var editedCharacters: NSTextStorageEditActions { get }
}
/* Note for subclassing NSTextStorage: NSTextStorage is a semi-abstract subclass of NSMutableAttributedString.
It implements change management (beginEditing/endEditing), verification of attributes, delegate handling, and layout management notification.
The one aspect it does not implement is the actual attributed string storage --- this is left up to the subclassers, which need to override the two NSMutableAttributedString primitives in addition to two NSAttributedString primitives:
- (NSString *)string;
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range;
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
These primitives should perform the change then call edited:range:changeInLength: to get everything else to happen.
*/
@available(iOS 7.0, *)
open class NSTextStorage : NSMutableAttributedString {
// NSLayoutManager objects owned by the receiver.
/**************************** Layout manager ****************************/
// 与存储对象相关连的 布局管理者
open var layoutManagers: [NSLayoutManager] { get }
// Adds aLayoutManager to the receiver. Sends -[NSLayoutManager setTextStorage:] to aLayoutManager with the receiver.
// 添加布局管理者
open func addLayoutManager(_ aLayoutManager: NSLayoutManager)
// Removes aLayoutManager from the receiver if already owned by it. Sends -[NSLayoutManager setTextStorage:] to aLayoutManager with nil.
// 移除布局管理者
open func removeLayoutManager(_ aLayoutManager: NSLayoutManager)
// These methods return information about the editing status. Especially useful when there are outstanding beginEditing calls or during processEditing...
// 这些方法返回的关于编辑状态的信息特别有用(在编辑之前 和 正在编辑的过程中的编辑状态信息)
// The NSTextStorageEditActions mask indicating that there are pending changes for attributes, characters, or both.
/**************************** Pending edit info ****************************/
open var editedMask: NSTextStorageEditActions { get }
// The range for pending changes. {NSNotFound, 0} when there is no pending changes.
open var editedRange: NSRange { get }
// The length delta for the pending changes.
open var changeInLength: Int { get }
/**************************** Delegate ****************************/
unowned(unsafe) open var delegate: NSTextStorageDelegate?
// Notifies and records a recent change. If there are no outstanding -beginEditing calls, this method calls -processEditing to trigger post-editing processes.
// This method has to be called by the primitives after changes are made if subclassed and overridden. editedRange is the range in the original string (before the edit).
/**************************** Edit management ****************************/
open func edited(_ editedMask: NSTextStorageEditActions, range editedRange: NSRange, changeInLength delta: Int)
// Sends out -textStorage:willProcessEditing, fixes the attributes, sends out -textStorage:didProcessEditing, and notifies the layout managers of change with the -processEditingForTextStorage:edited:range:changeInLength:invalidatedRange: method.
// Invoked from -edited:range:changeInLength: or -endEditing.
open func processEditing()
// Indicates if the receiver fixes invalidated attributes lazily. The concrete UIKit subclass fixes attributes lazily by default.
// The abstract class (hence, all custom subclasses) is not lazy.
/**************************** Attribute fixing ****************************/
open var fixesAttributesLazily: Bool { get }
// Notes the range of attributes that requires validation. If the NSTextStorage is not lazy this just calls fixAttributesInRange:.
// If it is lazy this instead just records the range needing fixing in order to do it later.
open func invalidateAttributes(in range: NSRange)
// Ensures all attributes in range are validated and ready to be used. An NSTextStorage that is lazy is required to call the following method before accessing any attributes.
// This gives the attribute fixing a chance to occur if necessary. NSTextStorage subclasses that wish to support laziness must call it from all attribute accessors that they implement.
// The default concrete subclass does call this from its accessors.
open func ensureAttributesAreFixed(in range: NSRange)
}
** 文本存储的代理方法**
/**** NSTextStorage delegate methods ****/
public protocol NSTextStorageDelegate : NSObjectProtocol {
// Sent inside -processEditing right before fixing attributes. Delegates can change the characters or attributes.
@available(iOS 7.0, *)
optional public func textStorage(_ textStorage: NSTextStorage, willProcessEditing editedMask: NSTextStorageEditActions, range editedRange: NSRange, changeInLength delta: Int)
// Sent inside -processEditing right before notifying layout managers. Delegates can change the attributes.
@available(iOS 7.0, *)
optional public func textStorage(_ textStorage: NSTextStorage, didProcessEditing editedMask: NSTextStorageEditActions, range editedRange: NSRange, changeInLength delta: Int)
}
通知
观察着不应该比代理对 textStorage 进行进一步的改变操作。 通知中会包含 编辑的步骤信息,不包含该 userInfo 信息
extension NSNotification.Name {
/**** Notifications ****/
@available(iOS 7.0, *)
// 在调用 processEditing() 之后发送
public static let NSTextStorageWillProcessEditing: NSNotification.Name
@available(iOS 7.0, *)
// 在调用 processEditing() 之前发送
public static let NSTextStorageDidProcessEditing: NSNotification.Name
}