NSTextContainer 类定义了一个文本排版的区域。一个 NSLayoutManager 使用 NSTextContainer 去决定 在哪里换行,布局部分文本。等其他的东西。一个 NSTextContainer 通常定义了一个矩形区域。但是,你也可以定义一个排除路径,在文本容器之内去创建区域,文本不能在这个区域流动。你也可以子类化去创建非矩形区域的文本容器,例如一个圆形区域,区域中有一个洞,或者是一个图形的区域。
概览
实例化 NSTextContainer, NSLayoutManager 和 NSTextStorage 的类,
@available(iOS 7.0, *)
open class NSTextContainer : NSObject, NSCoding, NSTextLayoutOrientationProvider {
/**************************** 初始化 ****************************/
@available(iOS 7.0, *)
// 初始化的时候设置容器矩形区域
public init(size: CGSize)
public init(coder: NSCoder)
// Accessor for the NSLayoutManager object owning the receiver.
// 接收器存储的布局管理对象
// Avoid assigning a layout manager directly through this property.
// 避免直接通过这个属性赋值一个布局管理者。
// Adding a text container to a layout manager through -[NSLayoutManager addTextContainer:] will use the property for assigning the new layout manager.
// 通过 -[NSLayoutManager addTextContainer:] 给布局管理者添加文本容器。
/**************************** 布局 ****************************/
// 获取布局管理对象
unowned(unsafe) open var layoutManager: NSLayoutManager?
// This method should be used instead of the primitive -setLayoutManager: if you need to replace a container's layoutManager with a new one leaving the rest of the web intact.
// 这个方法将要被用来替代 -setLayoutManager: 方法。
// All the NSTextContainers on the old NSLayoutManager get transferred to the new one.
// This method deals with all the work of making sure the containers don't get deallocated and removing the old layoutManager from the text storage and replacing it with the new one.
@available(iOS 9.0, *)
// 替换布局管理者
open func replaceLayoutManager(_ newLayoutManager: NSLayoutManager)
// Default value: CGSizeZero Defines the maximum size for the layout area returned from -lineFragmentRectForProposedRect:writingDirection:remainingRect:. 0.0 and less means no limitation.
/************************* 容器形状属性 *************************/
@available(iOS 7.0, *)
// 大小
open var size: CGSize
// Default value : empty array An array of UIBezierPath representing the exclusion paths inside the receiver's bounding rect.
@available(iOS 7.0, *)
// 排除路径
open var exclusionPaths: [UIBezierPath]
// Default value: NSLineBreakByWordWrapping The line break mode defines the behavior of the last line inside the text container.
@available(iOS 7.0, *)
// 换行模式
open var lineBreakMode: NSLineBreakMode
// Default value: 5.0 The layout padding at the beginning and end of the line fragment rects insetting the layout width available for the contents.
// This value is utilized by NSLayoutManager for determining the layout width.
/************************* 布局约束属性 *************************/
open var lineFragmentPadding: CGFloat
// Default value: 0 (no limit) The maximum number of lines that can be stored in the receiver. This value is utilized by NSLayoutManager for determining the maximum number of lines associated with the text container.
@available(iOS 7.0, *)
open var maximumNumberOfLines: Int
// Returns the bounds of a line fragment rect inside the receiver for proposedRect.
// This is the intersection of proposedRect and the receiver's bounding rect defined by -size property.
// The regions defined by -exclusionPaths property are excluded from the return value.
// charIndex is the character location inside the text storage for the line fragment being processed.
// It is possible that proposedRect can be divided into multiple line fragments due to exclusion paths.
// In that case, remainingRect returns the remainder that can be passed in as the proposed rect for the next iteration.
// baseWritingDirection determines the direction of advancement for line fragments inside a visual horizontal line.
// The values passed into the method are either NSWritingDirectionLeftToRight or NSWritingDirectionRightToLeft.
// This method can be overridden by subclasses for further text container region customization.
/**************************** Line fragments (行分段)****************************/
@available(iOS 7.0, *)
open func lineFragmentRect(forProposedRect proposedRect: CGRect, at characterIndex: Int, writingDirection baseWritingDirection: NSWritingDirection, remaining remainingRect: UnsafeMutablePointer<CGRect>?) -> CGRect
// Returns YES if the receiver is a rectangular shape defined simply by -size.
// TextKit utilizes this information for enabling various layout optimizations.
// NSLayoutManager disables non-contiguous layout when this property is NO.
// The default implementation returns NO when -exclusionPaths has 1 or more items, -maximumNumberOfLines is not 0, or -lineFragmentRectForProposedRect:atIndex:writingDirection:remainingRect: is overridden.
// It's recommended to override this property when -lineFragmentRectForProposedRect:atIndex:writingDirection:remainingRect: is overridden.
@available(iOS 9.0, *)
open var isSimpleRectangularTextContainer: Bool { get }
// Default value: NO Define whether the text container view bounds changes can affect the text container size.
/**************************** View synchronization ****************************/
open var widthTracksTextView: Bool
open var heightTracksTextView: Bool
}