YYTextAttribute.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // YYTextAttribute.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/10/26.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <UIKit/UIKit.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. #pragma mark - Enum Define
  14. /// The attribute type
  15. typedef NS_OPTIONS(NSInteger, YYTextAttributeType) {
  16. YYTextAttributeTypeNone = 0,
  17. YYTextAttributeTypeUIKit = 1 << 0, ///< UIKit attributes, such as UILabel/UITextField/drawInRect.
  18. YYTextAttributeTypeCoreText = 1 << 1, ///< CoreText attributes, used by CoreText.
  19. YYTextAttributeTypeYYText = 1 << 2, ///< YYText attributes, used by YYText.
  20. };
  21. /// Get the attribute type from an attribute name.
  22. extern YYTextAttributeType YYTextAttributeGetType(NSString *attributeName);
  23. /**
  24. Line style in YYText (similar to NSUnderlineStyle).
  25. */
  26. typedef NS_OPTIONS (NSInteger, YYTextLineStyle) {
  27. // basic style (bitmask:0xFF)
  28. YYTextLineStyleNone = 0x00, ///< ( ) Do not draw a line (Default).
  29. YYTextLineStyleSingle = 0x01, ///< (──────) Draw a single line.
  30. YYTextLineStyleThick = 0x02, ///< (━━━━━━━) Draw a thick line.
  31. YYTextLineStyleDouble = 0x09, ///< (══════) Draw a double line.
  32. // style pattern (bitmask:0xF00)
  33. YYTextLineStylePatternSolid = 0x000, ///< (────────) Draw a solid line (Default).
  34. YYTextLineStylePatternDot = 0x100, ///< (‑ ‑ ‑ ‑ ‑ ‑) Draw a line of dots.
  35. YYTextLineStylePatternDash = 0x200, ///< (— — — —) Draw a line of dashes.
  36. YYTextLineStylePatternDashDot = 0x300, ///< (— ‑ — ‑ — ‑) Draw a line of alternating dashes and dots.
  37. YYTextLineStylePatternDashDotDot = 0x400, ///< (— ‑ ‑ — ‑ ‑) Draw a line of alternating dashes and two dots.
  38. YYTextLineStylePatternCircleDot = 0x900, ///< (••••••••••••) Draw a line of small circle dots.
  39. };
  40. /**
  41. Text vertical alignment.
  42. */
  43. typedef NS_ENUM(NSInteger, YYTextVerticalAlignment) {
  44. YYTextVerticalAlignmentTop = 0, ///< Top alignment.
  45. YYTextVerticalAlignmentCenter = 1, ///< Center alignment.
  46. YYTextVerticalAlignmentBottom = 2, ///< Bottom alignment.
  47. };
  48. /**
  49. The direction define in YYText.
  50. */
  51. typedef NS_OPTIONS(NSUInteger, YYTextDirection) {
  52. YYTextDirectionNone = 0,
  53. YYTextDirectionTop = 1 << 0,
  54. YYTextDirectionRight = 1 << 1,
  55. YYTextDirectionBottom = 1 << 2,
  56. YYTextDirectionLeft = 1 << 3,
  57. };
  58. /**
  59. The trunction type, tells the truncation engine which type of truncation is being requested.
  60. */
  61. typedef NS_ENUM (NSUInteger, YYTextTruncationType) {
  62. /// No truncate.
  63. YYTextTruncationTypeNone = 0,
  64. /// Truncate at the beginning of the line, leaving the end portion visible.
  65. YYTextTruncationTypeStart = 1,
  66. /// Truncate at the end of the line, leaving the start portion visible.
  67. YYTextTruncationTypeEnd = 2,
  68. /// Truncate in the middle of the line, leaving both the start and the end portions visible.
  69. YYTextTruncationTypeMiddle = 3,
  70. };
  71. #pragma mark - Attribute Name Defined in YYText
  72. /// The value of this attribute is a `YYTextBackedString` object.
  73. /// Use this attribute to store the original plain text if it is replaced by something else (such as attachment).
  74. UIKIT_EXTERN NSString *const YYTextBackedStringAttributeName;
  75. /// The value of this attribute is a `YYTextBinding` object.
  76. /// Use this attribute to bind a range of text together, as if it was a single charactor.
  77. UIKIT_EXTERN NSString *const YYTextBindingAttributeName;
  78. /// The value of this attribute is a `YYTextShadow` object.
  79. /// Use this attribute to add shadow to a range of text.
  80. /// Shadow will be drawn below text glyphs. Use YYTextShadow.subShadow to add multi-shadow.
  81. UIKIT_EXTERN NSString *const YYTextShadowAttributeName;
  82. /// The value of this attribute is a `YYTextShadow` object.
  83. /// Use this attribute to add inner shadow to a range of text.
  84. /// Inner shadow will be drawn above text glyphs. Use YYTextShadow.subShadow to add multi-shadow.
  85. UIKIT_EXTERN NSString *const YYTextInnerShadowAttributeName;
  86. /// The value of this attribute is a `YYTextDecoration` object.
  87. /// Use this attribute to add underline to a range of text.
  88. /// The underline will be drawn below text glyphs.
  89. UIKIT_EXTERN NSString *const YYTextUnderlineAttributeName;
  90. /// The value of this attribute is a `YYTextDecoration` object.
  91. /// Use this attribute to add strikethrough (delete line) to a range of text.
  92. /// The strikethrough will be drawn above text glyphs.
  93. UIKIT_EXTERN NSString *const YYTextStrikethroughAttributeName;
  94. /// The value of this attribute is a `YYTextBorder` object.
  95. /// Use this attribute to add cover border or cover color to a range of text.
  96. /// The border will be drawn above the text glyphs.
  97. UIKIT_EXTERN NSString *const YYTextBorderAttributeName;
  98. /// The value of this attribute is a `YYTextBorder` object.
  99. /// Use this attribute to add background border or background color to a range of text.
  100. /// The border will be drawn below the text glyphs.
  101. UIKIT_EXTERN NSString *const YYTextBackgroundBorderAttributeName;
  102. /// The value of this attribute is a `YYTextBorder` object.
  103. /// Use this attribute to add a code block border to one or more line of text.
  104. /// The border will be drawn below the text glyphs.
  105. UIKIT_EXTERN NSString *const YYTextBlockBorderAttributeName;
  106. /// The value of this attribute is a `YYTextAttachment` object.
  107. /// Use this attribute to add attachment to text.
  108. /// It should be used in conjunction with a CTRunDelegate.
  109. UIKIT_EXTERN NSString *const YYTextAttachmentAttributeName;
  110. /// The value of this attribute is a `YYTextHighlight` object.
  111. /// Use this attribute to add a touchable highlight state to a range of text.
  112. UIKIT_EXTERN NSString *const YYTextHighlightAttributeName;
  113. /// The value of this attribute is a `NSValue` object stores CGAffineTransform.
  114. /// Use this attribute to add transform to each glyph in a range of text.
  115. UIKIT_EXTERN NSString *const YYTextGlyphTransformAttributeName;
  116. #pragma mark - String Token Define
  117. UIKIT_EXTERN NSString *const YYTextAttachmentToken; ///< Object replacement character (U+FFFC), used for text attachment.
  118. UIKIT_EXTERN NSString *const YYTextTruncationToken; ///< Horizontal ellipsis (U+2026), used for text truncation "…".
  119. #pragma mark - Attribute Value Define
  120. /**
  121. The tap/long press action callback defined in YYText.
  122. @param containerView The text container view (such as YYLabel/YYTextView).
  123. @param text The whole text.
  124. @param range The text range in `text` (if no range, the range.location is NSNotFound).
  125. @param rect The text frame in `containerView` (if no data, the rect is CGRectNull).
  126. */
  127. typedef void(^YYTextAction)(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect);
  128. /**
  129. YYTextBackedString objects are used by the NSAttributedString class cluster
  130. as the values for text backed string attributes (stored in the attributed
  131. string under the key named YYTextBackedStringAttributeName).
  132. It may used for copy/paste plain text from attributed string.
  133. Example: If :) is replace by a custom emoji (such as😊), the backed string can be set to @":)".
  134. */
  135. @interface YYTextBackedString : NSObject <NSCoding, NSCopying>
  136. + (instancetype)stringWithString:(nullable NSString *)string;
  137. @property (nullable, nonatomic, copy) NSString *string; ///< backed string
  138. @end
  139. /**
  140. YYTextBinding objects are used by the NSAttributedString class cluster
  141. as the values for shadow attributes (stored in the attributed string under
  142. the key named YYTextBindingAttributeName).
  143. Add this to a range of text will make the specified characters 'binding together'.
  144. YYTextView will treat the range of text as a single character during text
  145. selection and edit.
  146. */
  147. @interface YYTextBinding : NSObject <NSCoding, NSCopying>
  148. + (instancetype)bindingWithDeleteConfirm:(BOOL)deleteConfirm;
  149. @property (nonatomic) BOOL deleteConfirm; ///< confirm the range when delete in YYTextView
  150. @end
  151. /**
  152. YYTextShadow objects are used by the NSAttributedString class cluster
  153. as the values for shadow attributes (stored in the attributed string under
  154. the key named YYTextShadowAttributeName or YYTextInnerShadowAttributeName).
  155. It's similar to `NSShadow`, but offers more options.
  156. */
  157. @interface YYTextShadow : NSObject <NSCoding, NSCopying>
  158. + (instancetype)shadowWithColor:(nullable UIColor *)color offset:(CGSize)offset radius:(CGFloat)radius;
  159. @property (nullable, nonatomic, strong) UIColor *color; ///< shadow color
  160. @property (nonatomic) CGSize offset; ///< shadow offset
  161. @property (nonatomic) CGFloat radius; ///< shadow blur radius
  162. @property (nonatomic) CGBlendMode blendMode; ///< shadow blend mode
  163. @property (nullable, nonatomic, strong) YYTextShadow *subShadow; ///< a sub shadow which will be added above the parent shadow
  164. + (instancetype)shadowWithNSShadow:(NSShadow *)nsShadow; ///< convert NSShadow to YYTextShadow
  165. - (NSShadow *)nsShadow; ///< convert YYTextShadow to NSShadow
  166. @end
  167. /**
  168. YYTextDecorationLine objects are used by the NSAttributedString class cluster
  169. as the values for decoration line attributes (stored in the attributed string under
  170. the key named YYTextUnderlineAttributeName or YYTextStrikethroughAttributeName).
  171. When it's used as underline, the line is drawn below text glyphs;
  172. when it's used as strikethrough, the line is drawn above text glyphs.
  173. */
  174. @interface YYTextDecoration : NSObject <NSCoding, NSCopying>
  175. + (instancetype)decorationWithStyle:(YYTextLineStyle)style;
  176. + (instancetype)decorationWithStyle:(YYTextLineStyle)style width:(nullable NSNumber *)width color:(nullable UIColor *)color;
  177. @property (nonatomic) YYTextLineStyle style; ///< line style
  178. @property (nullable, nonatomic, strong) NSNumber *width; ///< line width (nil means automatic width)
  179. @property (nullable, nonatomic, strong) UIColor *color; ///< line color (nil means automatic color)
  180. @property (nullable, nonatomic, strong) YYTextShadow *shadow; ///< line shadow
  181. @end
  182. /**
  183. YYTextBorder objects are used by the NSAttributedString class cluster
  184. as the values for border attributes (stored in the attributed string under
  185. the key named YYTextBorderAttributeName or YYTextBackgroundBorderAttributeName).
  186. It can be used to draw a border around a range of text, or draw a background
  187. to a range of text.
  188. Example:
  189. ╭──────╮
  190. │ Text │
  191. ╰──────╯
  192. */
  193. @interface YYTextBorder : NSObject <NSCoding, NSCopying>
  194. + (instancetype)borderWithLineStyle:(YYTextLineStyle)lineStyle lineWidth:(CGFloat)width strokeColor:(nullable UIColor *)color;
  195. + (instancetype)borderWithFillColor:(nullable UIColor *)color cornerRadius:(CGFloat)cornerRadius;
  196. @property (nonatomic) YYTextLineStyle lineStyle; ///< border line style
  197. @property (nonatomic) CGFloat strokeWidth; ///< border line width
  198. @property (nullable, nonatomic, strong) UIColor *strokeColor; ///< border line color
  199. @property (nonatomic) CGLineJoin lineJoin; ///< border line join
  200. @property (nonatomic) UIEdgeInsets insets; ///< border insets for text bounds
  201. @property (nonatomic) CGFloat cornerRadius; ///< border corder radius
  202. @property (nullable, nonatomic, strong) YYTextShadow *shadow; ///< border shadow
  203. @property (nullable, nonatomic, strong) UIColor *fillColor; ///< inner fill color
  204. @end
  205. /**
  206. YYTextAttachment objects are used by the NSAttributedString class cluster
  207. as the values for attachment attributes (stored in the attributed string under
  208. the key named YYTextAttachmentAttributeName).
  209. When display an attributed string which contains `YYTextAttachment` object,
  210. the content will be placed in text metric. If the content is `UIImage`,
  211. then it will be drawn to CGContext; if the content is `UIView` or `CALayer`,
  212. then it will be added to the text container's view or layer.
  213. */
  214. @interface YYTextAttachment : NSObject<NSCoding, NSCopying>
  215. + (instancetype)attachmentWithContent:(nullable id)content;
  216. @property (nullable, nonatomic, strong) id content; ///< Supported type: UIImage, UIView, CALayer
  217. @property (nonatomic) UIViewContentMode contentMode; ///< Content display mode.
  218. @property (nonatomic) UIEdgeInsets contentInsets; ///< The insets when drawing content.
  219. @property (nullable, nonatomic, strong) NSDictionary *userInfo; ///< The user information dictionary.
  220. @end
  221. /**
  222. YYTextHighlight objects are used by the NSAttributedString class cluster
  223. as the values for touchable highlight attributes (stored in the attributed string
  224. under the key named YYTextHighlightAttributeName).
  225. When display an attributed string in `YYLabel` or `YYTextView`, the range of
  226. highlight text can be toucheds down by users. If a range of text is turned into
  227. highlighted state, the `attributes` in `YYTextHighlight` will be used to modify
  228. (set or remove) the original attributes in the range for display.
  229. */
  230. @interface YYTextHighlight : NSObject <NSCoding, NSCopying>
  231. /**
  232. Attributes that you can apply to text in an attributed string when highlight.
  233. Key: Same as CoreText/YYText Attribute Name.
  234. Value: Modify attribute value when highlight (NSNull for remove attribute).
  235. */
  236. @property (nullable, nonatomic, copy) NSDictionary<NSString *, id> *attributes;
  237. /**
  238. Creates a highlight object with specified attributes.
  239. @param attributes The attributes which will replace original attributes when highlight,
  240. If the value is NSNull, it will removed when highlight.
  241. */
  242. + (instancetype)highlightWithAttributes:(nullable NSDictionary<NSString *, id> *)attributes;
  243. /**
  244. Convenience methods to create a default highlight with the specifeid background color.
  245. @param color The background border color.
  246. */
  247. + (instancetype)highlightWithBackgroundColor:(nullable UIColor *)color;
  248. // Convenience methods below to set the `attributes`.
  249. - (void)setFont:(nullable UIFont *)font;
  250. - (void)setColor:(nullable UIColor *)color;
  251. - (void)setStrokeWidth:(nullable NSNumber *)width;
  252. - (void)setStrokeColor:(nullable UIColor *)color;
  253. - (void)setShadow:(nullable YYTextShadow *)shadow;
  254. - (void)setInnerShadow:(nullable YYTextShadow *)shadow;
  255. - (void)setUnderline:(nullable YYTextDecoration *)underline;
  256. - (void)setStrikethrough:(nullable YYTextDecoration *)strikethrough;
  257. - (void)setBackgroundBorder:(nullable YYTextBorder *)border;
  258. - (void)setBorder:(nullable YYTextBorder *)border;
  259. - (void)setAttachment:(nullable YYTextAttachment *)attachment;
  260. /**
  261. The user information dictionary, default is nil.
  262. */
  263. @property (nullable, nonatomic, copy) NSDictionary *userInfo;
  264. /**
  265. Tap action when user tap the highlight, default is nil.
  266. If the value is nil, YYTextView or YYLabel will ask it's delegate to handle the tap action.
  267. */
  268. @property (nullable, nonatomic, copy) YYTextAction tapAction;
  269. /**
  270. Long press action when user long press the highlight, default is nil.
  271. If the value is nil, YYTextView or YYLabel will ask it's delegate to handle the long press action.
  272. */
  273. @property (nullable, nonatomic, copy) YYTextAction longPressAction;
  274. @end
  275. NS_ASSUME_NONNULL_END