|
@@ -23,6 +23,12 @@
|
|
|
#endif
|
|
|
|
|
|
@protocol YYTextLinePositionModifier;
|
|
|
+
|
|
|
+NS_ASSUME_NONNULL_BEGIN
|
|
|
+
|
|
|
+/**
|
|
|
+ The max text container size in layout.
|
|
|
+ */
|
|
|
extern const CGSize YYTextContainerMaxSize;
|
|
|
|
|
|
/**
|
|
@@ -59,43 +65,43 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
+ (instancetype)containerWithSize:(CGSize)size insets:(UIEdgeInsets)insets;
|
|
|
|
|
|
/// Creates a container with the specified path. @param size The path.
|
|
|
-+ (instancetype)containerWithPath:(UIBezierPath *)path;
|
|
|
++ (instancetype)containerWithPath:(nullable UIBezierPath *)path;
|
|
|
|
|
|
/// The constrained size. (if the size is larger than YYTextContainerMaxSize, it will be clipped)
|
|
|
-@property (assign) CGSize size;
|
|
|
+@property CGSize size;
|
|
|
|
|
|
/// The insets for constrained size. The inset value should not be negative. Default is UIEdgeInsetsZero.
|
|
|
-@property (assign) UIEdgeInsets insets;
|
|
|
+@property UIEdgeInsets insets;
|
|
|
|
|
|
/// Custom constrained path. Set this property to ignore `size` and `insets`. Default is nil.
|
|
|
-@property (copy) UIBezierPath *path;
|
|
|
+@property (nullable, copy) UIBezierPath *path;
|
|
|
|
|
|
/// An array of `UIBezierPath` for path exclusion. Default is nil.
|
|
|
-@property (copy) NSArray *exclusionPaths;
|
|
|
+@property (nullable, copy) NSArray<UIBezierPath *> *exclusionPaths;
|
|
|
|
|
|
/// Path line width. Default is 0;
|
|
|
-@property (assign) CGFloat pathLineWidth;
|
|
|
+@property CGFloat pathLineWidth;
|
|
|
|
|
|
/// YES:(PathFillEvenOdd) Text is filled in the area that would be painted if the path were given to CGContextEOFillPath.
|
|
|
/// NO: (PathFillWindingNumber) Text is fill in the area that would be painted if the path were given to CGContextFillPath.
|
|
|
/// Default is YES;
|
|
|
-@property (assign, getter=isPathFillEvenOdd) BOOL pathFillEvenOdd;
|
|
|
+@property (getter=isPathFillEvenOdd) BOOL pathFillEvenOdd;
|
|
|
|
|
|
/// Whether the text is vertical form (may used for CJK text layout). Default is NO.
|
|
|
-@property (assign, getter=isVerticalForm) BOOL verticalForm;
|
|
|
+@property (getter=isVerticalForm) BOOL verticalForm;
|
|
|
|
|
|
/// Maximum number of rows, 0 means no limit. Default is 0.
|
|
|
-@property (assign) NSUInteger maximumNumberOfRows;
|
|
|
+@property NSUInteger maximumNumberOfRows;
|
|
|
|
|
|
/// The line truncation type, default is none.
|
|
|
-@property (assign) YYTextTruncationType truncationType;
|
|
|
+@property YYTextTruncationType truncationType;
|
|
|
|
|
|
/// The truncation token. If nil, the layout will use "…" instead. Default is nil.
|
|
|
-@property (copy) NSAttributedString *truncationToken;
|
|
|
+@property (nullable, copy) NSAttributedString *truncationToken;
|
|
|
|
|
|
/// This modifier is applied to the lines before the layout is completed,
|
|
|
/// give you a chance to modify the line position. Default is nil.
|
|
|
-@property (copy) id<YYTextLinePositionModifier> linePositionModifier;
|
|
|
+@property (nullable, copy) id<YYTextLinePositionModifier> linePositionModifier;
|
|
|
@end
|
|
|
|
|
|
|
|
@@ -111,7 +117,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@param text The full text.
|
|
|
@param container The layout container.
|
|
|
*/
|
|
|
-- (void)modifyLines:(NSArray *)lines fromText:(NSAttributedString *)text inContainer:(YYTextContainer *)container;
|
|
|
+- (void)modifyLines:(NSArray<YYTextLine *> *)lines fromText:(NSAttributedString *)text inContainer:(YYTextContainer *)container;
|
|
|
@end
|
|
|
|
|
|
|
|
@@ -157,7 +163,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@param text The text (if nil, returns nil).
|
|
|
@return A new layout, or nil when an error occurs.
|
|
|
*/
|
|
|
-+ (YYTextLayout *)layoutWithContainerSize:(CGSize)size text:(NSAttributedString *)text;
|
|
|
++ (nullable YYTextLayout *)layoutWithContainerSize:(CGSize)size text:(NSAttributedString *)text;
|
|
|
|
|
|
/**
|
|
|
Generate a layout with the given container and text.
|
|
@@ -166,7 +172,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@param text The text (if nil, returns nil).
|
|
|
@return A new layout, or nil when an error occurs.
|
|
|
*/
|
|
|
-+ (YYTextLayout *)layoutWithContainer:(YYTextContainer *)container text:(NSAttributedString *)text;
|
|
|
++ (nullable YYTextLayout *)layoutWithContainer:(YYTextContainer *)container text:(NSAttributedString *)text;
|
|
|
|
|
|
/**
|
|
|
Generate a layout with the given container and text.
|
|
@@ -177,7 +183,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
length of the range is 0, it means the length is no limit.
|
|
|
@return A new layout, or nil when an error occurs.
|
|
|
*/
|
|
|
-+ (YYTextLayout *)layoutWithContainer:(YYTextContainer *)container text:(NSAttributedString *)text range:(NSRange)range;
|
|
|
++ (nullable YYTextLayout *)layoutWithContainer:(YYTextContainer *)container text:(NSAttributedString *)text range:(NSRange)range;
|
|
|
|
|
|
/**
|
|
|
Generate layouts with the given containers and text.
|
|
@@ -187,7 +193,8 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@return An array of YYTextLayout object (the count is same as containers),
|
|
|
or nil when an error occurs.
|
|
|
*/
|
|
|
-+ (NSArray *)layoutWithContainers:(NSArray *)containers text:(NSAttributedString *)text;
|
|
|
++ (nullable NSArray<YYTextLayout *> *)layoutWithContainers:(NSArray<YYTextContainer *> *)containers
|
|
|
+ text:(NSAttributedString *)text;
|
|
|
|
|
|
/**
|
|
|
Generate layouts with the given containers and text.
|
|
@@ -199,7 +206,9 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@return An array of YYTextLayout object (the count is same as containers),
|
|
|
or nil when an error occurs.
|
|
|
*/
|
|
|
-+ (NSArray *)layoutWithContainers:(NSArray *)containers text:(NSAttributedString *)text range:(NSRange)range;
|
|
|
++ (nullable NSArray<YYTextLayout *> *)layoutWithContainers:(NSArray<YYTextContainer *> *)containers
|
|
|
+ text:(NSAttributedString *)text
|
|
|
+ range:(NSRange)range;
|
|
|
|
|
|
- (instancetype)init UNAVAILABLE_ATTRIBUTE;
|
|
|
+ (instancetype)new UNAVAILABLE_ATTRIBUTE;
|
|
@@ -210,31 +219,56 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
/// @name Text layout attributes
|
|
|
///=============================================================================
|
|
|
|
|
|
-@property (nonatomic, readonly) YYTextContainer *container; ///< The text container
|
|
|
-@property (nonatomic, readonly) NSAttributedString *text; ///< The full text
|
|
|
-@property (nonatomic, readonly) NSRange range; ///< The text range in full text
|
|
|
-@property (nonatomic, readonly) CTFramesetterRef frameSetter; ///< CTFrameSetter
|
|
|
-@property (nonatomic, readonly) CTFrameRef frame; ///< CTFrame
|
|
|
-@property (nonatomic, readonly) NSArray *lines; ///< Array of `YYTextLine`, no truncated
|
|
|
-@property (nonatomic, readonly) YYTextLine *truncatedLine; ///< YYTextLine with truncated token, or nil
|
|
|
-@property (nonatomic, readonly) NSArray *attachments; ///< Array of `YYTextAttachment`
|
|
|
-@property (nonatomic, readonly) NSArray *attachmentRanges; ///< Array of NSRange(wrapped by NSValue) in text
|
|
|
-@property (nonatomic, readonly) NSArray *attachmentRects; ///< Array of CGRect(wrapped by NSValue) in container
|
|
|
-@property (nonatomic, readonly) NSSet *attachmentContentsSet; ///< Set of Attachment (UIImage/UIView/CALayer)
|
|
|
-@property (nonatomic, readonly) NSUInteger rowCount; ///< Number of rows
|
|
|
-@property (nonatomic, readonly) NSRange visibleRange; ///< Visible text range
|
|
|
-@property (nonatomic, readonly) CGRect textBoundingRect; ///< Bounding rect (glyphs)
|
|
|
-@property (nonatomic, readonly) CGSize textBoundingSize; ///< Bounding size (glyphs and insets, ceil to pixel)
|
|
|
-@property (nonatomic, readonly) BOOL containsHighlight; ///< Has highlight attribute
|
|
|
-@property (nonatomic, readonly) BOOL needDrawBlockBorder; ///< Has block border attribute
|
|
|
-@property (nonatomic, readonly) BOOL needDrawBackgroundBorder; ///< Has background border attribute
|
|
|
-@property (nonatomic, readonly) BOOL needDrawShadow; ///< Has shadow attribute
|
|
|
-@property (nonatomic, readonly) BOOL needDrawUnderline; ///< Has underline attribute
|
|
|
-@property (nonatomic, readonly) BOOL needDrawText; ///< Has visible text
|
|
|
-@property (nonatomic, readonly) BOOL needDrawAttachment; ///< Has attachment attribute
|
|
|
-@property (nonatomic, readonly) BOOL needDrawInnerShadow; ///< Has inner shadow attribute
|
|
|
-@property (nonatomic, readonly) BOOL needDrawStrikethrough; ///< Has strickthrough attribute
|
|
|
-@property (nonatomic, readonly) BOOL needDrawBorder; ///< Has border attribute
|
|
|
+///< The text container
|
|
|
+@property (nonatomic, strong, readonly) YYTextContainer *container;
|
|
|
+///< The full text
|
|
|
+@property (nonatomic, strong, readonly) NSAttributedString *text;
|
|
|
+///< The text range in full text
|
|
|
+@property (nonatomic, readonly) NSRange range;
|
|
|
+///< CTFrameSetter
|
|
|
+@property (nonatomic, readonly) CTFramesetterRef frameSetter;
|
|
|
+///< CTFrame
|
|
|
+@property (nonatomic, readonly) CTFrameRef frame;
|
|
|
+///< Array of `YYTextLine`, no truncated
|
|
|
+@property (nonatomic, strong, readonly) NSArray<YYTextLine *> *lines;
|
|
|
+///< YYTextLine with truncated token, or nil
|
|
|
+@property (nullable, nonatomic, strong, readonly) YYTextLine *truncatedLine;
|
|
|
+///< Array of `YYTextAttachment`
|
|
|
+@property (nullable, nonatomic, strong, readonly) NSArray<YYTextAttachment *> *attachments;
|
|
|
+///< Array of NSRange(wrapped by NSValue) in text
|
|
|
+@property (nullable, nonatomic, strong, readonly) NSArray<NSValue *> *attachmentRanges;
|
|
|
+///< Array of CGRect(wrapped by NSValue) in container
|
|
|
+@property (nullable, nonatomic, strong, readonly) NSArray<NSValue *> *attachmentRects;
|
|
|
+///< Set of Attachment (UIImage/UIView/CALayer)
|
|
|
+@property (nullable, nonatomic, strong, readonly) NSSet *attachmentContentsSet;
|
|
|
+///< Number of rows
|
|
|
+@property (nonatomic, readonly) NSUInteger rowCount;
|
|
|
+///< Visible text range
|
|
|
+@property (nonatomic, readonly) NSRange visibleRange;
|
|
|
+///< Bounding rect (glyphs)
|
|
|
+@property (nonatomic, readonly) CGRect textBoundingRect;
|
|
|
+///< Bounding size (glyphs and insets, ceil to pixel)
|
|
|
+@property (nonatomic, readonly) CGSize textBoundingSize;
|
|
|
+///< Has highlight attribute
|
|
|
+@property (nonatomic, readonly) BOOL containsHighlight;
|
|
|
+///< Has block border attribute
|
|
|
+@property (nonatomic, readonly) BOOL needDrawBlockBorder;
|
|
|
+///< Has background border attribute
|
|
|
+@property (nonatomic, readonly) BOOL needDrawBackgroundBorder;
|
|
|
+///< Has shadow attribute
|
|
|
+@property (nonatomic, readonly) BOOL needDrawShadow;
|
|
|
+///< Has underline attribute
|
|
|
+@property (nonatomic, readonly) BOOL needDrawUnderline;
|
|
|
+///< Has visible text
|
|
|
+@property (nonatomic, readonly) BOOL needDrawText;
|
|
|
+///< Has attachment attribute
|
|
|
+@property (nonatomic, readonly) BOOL needDrawAttachment;
|
|
|
+///< Has inner shadow attribute
|
|
|
+@property (nonatomic, readonly) BOOL needDrawInnerShadow;
|
|
|
+///< Has strickthrough attribute
|
|
|
+@property (nonatomic, readonly) BOOL needDrawStrikethrough;
|
|
|
+///< Has border attribute
|
|
|
+@property (nonatomic, readonly) BOOL needDrawBorder;
|
|
|
|
|
|
|
|
|
#pragma mark - Query information from text layout
|
|
@@ -318,7 +352,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@param point A point in the container.
|
|
|
@return A text position, or nil if not found.
|
|
|
*/
|
|
|
-- (YYTextPosition *)closestPositionToPoint:(CGPoint)point;
|
|
|
+- (nullable YYTextPosition *)closestPositionToPoint:(CGPoint)point;
|
|
|
|
|
|
/**
|
|
|
Returns the new position when moving selection grabber in text view.
|
|
@@ -332,9 +366,9 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
|
|
|
@return A text position, or nil if not found.
|
|
|
*/
|
|
|
-- (YYTextPosition *)positionForPoint:(CGPoint)point
|
|
|
- oldPosition:(YYTextPosition *)oldPosition
|
|
|
- otherPosition:(YYTextPosition *)otherPosition;
|
|
|
+- (nullable YYTextPosition *)positionForPoint:(CGPoint)point
|
|
|
+ oldPosition:(YYTextPosition *)oldPosition
|
|
|
+ otherPosition:(YYTextPosition *)otherPosition;
|
|
|
|
|
|
/**
|
|
|
Returns the character or range of characters that is at a given point in the container.
|
|
@@ -347,7 +381,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@return An object representing a range that encloses a character (or characters)
|
|
|
at point. Or nil if not found.
|
|
|
*/
|
|
|
-- (YYTextRange *)textRangeAtPoint:(CGPoint)point;
|
|
|
+- (nullable YYTextRange *)textRangeAtPoint:(CGPoint)point;
|
|
|
|
|
|
/**
|
|
|
Returns the closest character or range of characters that is at a given point in
|
|
@@ -360,7 +394,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@return An object representing a range that encloses a character (or characters)
|
|
|
at point. Or nil if not found.
|
|
|
*/
|
|
|
-- (YYTextRange *)closestTextRangeAtPoint:(CGPoint)point;
|
|
|
+- (nullable YYTextRange *)closestTextRangeAtPoint:(CGPoint)point;
|
|
|
|
|
|
/**
|
|
|
If the position is inside an emoji, composed character sequences, line break '\\r\\n'
|
|
@@ -371,7 +405,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
|
|
|
@return A text-range object that extend the position. Or nil if an error occurs
|
|
|
*/
|
|
|
-- (YYTextRange *)textRangeByExtendingPosition:(YYTextPosition *)position;
|
|
|
+- (nullable YYTextRange *)textRangeByExtendingPosition:(YYTextPosition *)position;
|
|
|
|
|
|
/**
|
|
|
Returns a text range at a given offset in a specified direction from another
|
|
@@ -384,9 +418,9 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@return A text-range object that represents the distance from position to the
|
|
|
farthest extent in direction. Or nil if an error occurs.
|
|
|
*/
|
|
|
-- (YYTextRange *)textRangeByExtendingPosition:(YYTextPosition *)position
|
|
|
- inDirection:(UITextLayoutDirection)direction
|
|
|
- offset:(NSInteger)offset;
|
|
|
+- (nullable YYTextRange *)textRangeByExtendingPosition:(YYTextPosition *)position
|
|
|
+ inDirection:(UITextLayoutDirection)direction
|
|
|
+ offset:(NSInteger)offset;
|
|
|
|
|
|
/**
|
|
|
Returns the line index for a given text position.
|
|
@@ -446,7 +480,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@return An array of `YYTextSelectionRect` objects that encompass the selection.
|
|
|
If not found, the array is empty.
|
|
|
*/
|
|
|
-- (NSArray *)selectionRectsForRange:(YYTextRange *)range;
|
|
|
+- (NSArray<YYTextSelectionRect *> *)selectionRectsForRange:(YYTextRange *)range;
|
|
|
|
|
|
/**
|
|
|
Returns an array of selection rects corresponding to the range of text.
|
|
@@ -455,7 +489,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@return An array of `YYTextSelectionRect` objects that encompass the selection.
|
|
|
If not found, the array is empty.
|
|
|
*/
|
|
|
-- (NSArray *)selectionRectsWithoutStartAndEndForRange:(YYTextRange *)range;
|
|
|
+- (NSArray<YYTextSelectionRect *> *)selectionRectsWithoutStartAndEndForRange:(YYTextRange *)range;
|
|
|
|
|
|
/**
|
|
|
Returns the start and end selection rects corresponding to the range of text.
|
|
@@ -465,7 +499,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@return An array of `YYTextSelectionRect` objects contains the start and end to
|
|
|
the selection. If not found, the array is empty.
|
|
|
*/
|
|
|
-- (NSArray *)selectionRectsWithOnlyStartAndEndForRange:(YYTextRange *)range;
|
|
|
+- (NSArray<YYTextSelectionRect *> *)selectionRectsWithOnlyStartAndEndForRange:(YYTextRange *)range;
|
|
|
|
|
|
|
|
|
#pragma mark - Draw text layout
|
|
@@ -494,13 +528,13 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
If it returns YES, the further draw progress will be canceled.
|
|
|
Pass nil to ignore this feature.
|
|
|
*/
|
|
|
-- (void)drawInContext:(CGContextRef)context
|
|
|
+- (void)drawInContext:(nullable CGContextRef)context
|
|
|
size:(CGSize)size
|
|
|
point:(CGPoint)point
|
|
|
- view:(UIView *)view
|
|
|
- layer:(CALayer *)layer
|
|
|
- debug:(YYTextDebugOption *)debug
|
|
|
- cancel:(BOOL (^)(void))cancel;
|
|
|
+ view:(nullable UIView *)view
|
|
|
+ layer:(nullable CALayer *)layer
|
|
|
+ debug:(nullable YYTextDebugOption *)debug
|
|
|
+ cancel:(nullable BOOL (^)(void))cancel;
|
|
|
|
|
|
/**
|
|
|
Draw the layout text and image (without view or layer attachments).
|
|
@@ -511,7 +545,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@param size The context size.
|
|
|
@param debug The debug option. Pass nil to avoid debug drawing.
|
|
|
*/
|
|
|
-- (void)drawInContext:(CGContextRef)context size:(CGSize)size debug:(YYTextDebugOption *)debug;
|
|
|
+- (void)drawInContext:(nullable CGContextRef)context size:(CGSize)size debug:(nullable YYTextDebugOption *)debug;
|
|
|
|
|
|
/**
|
|
|
Show view and layer attachments.
|
|
@@ -521,7 +555,7 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
@param view The attachment views will add to this view.
|
|
|
@param layer The attachment layers will add to this layer.
|
|
|
*/
|
|
|
-- (void)addAttachmentToView:(UIView *)view layer:(CALayer *)layer;
|
|
|
+- (void)addAttachmentToView:(nullable UIView *)view layer:(nullable CALayer *)layer;
|
|
|
|
|
|
/**
|
|
|
Remove attachment views and layers from their super container.
|
|
@@ -531,3 +565,5 @@ extern const CGSize YYTextContainerMaxSize;
|
|
|
- (void)removeAttachmentFromViewAndLayer;
|
|
|
|
|
|
@end
|
|
|
+
|
|
|
+NS_ASSUME_NONNULL_END
|