YYLabel.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. //
  2. // YYLabel.h
  3. // YYText <https://github.com/ibireme/YYText>
  4. //
  5. // Created by ibireme on 15/2/25.
  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. #if __has_include(<YYText/YYText.h>)
  13. #import <YYText/YYTextParser.h>
  14. #import <YYText/YYTextLayout.h>
  15. #import <YYText/YYTextAttribute.h>
  16. #else
  17. #import "YYTextParser.h"
  18. #import "YYTextLayout.h"
  19. #import "YYTextAttribute.h"
  20. #endif
  21. NS_ASSUME_NONNULL_BEGIN
  22. #if !TARGET_INTERFACE_BUILDER
  23. /**
  24. The YYLabel class implements a read-only text view.
  25. @discussion The API and behavior is similar to UILabel, but provides more features:
  26. * It supports asynchronous layout and rendering (to avoid blocking UI thread).
  27. * It extends the CoreText attributes to support more text effects.
  28. * It allows to add UIImage, UIView and CALayer as text attachments.
  29. * It allows to add 'highlight' link to some range of text to allow user interact with.
  30. * It allows to add container path and exclusion paths to control text container's shape.
  31. * It supports vertical form layout to display CJK text.
  32. See NSAttributedString+YYText.h for more convenience methods to set the attributes.
  33. See YYTextAttribute.h and YYTextLayout.h for more information.
  34. */
  35. @interface YYLabel : UIView <NSCoding>
  36. #pragma mark - Accessing the Text Attributes
  37. ///=============================================================================
  38. /// @name Accessing the Text Attributes
  39. ///=============================================================================
  40. /**
  41. The text displayed by the label. Default is nil.
  42. Set a new value to this property also replaces the text in `attributedText`.
  43. Get the value returns the plain text in `attributedText`.
  44. */
  45. @property (nullable, nonatomic, copy) NSString *text;
  46. /**
  47. The font of the text. Default is 17-point system font.
  48. Set a new value to this property also causes the new font to be applied to the entire `attributedText`.
  49. Get the value returns the font at the head of `attributedText`.
  50. */
  51. @property (null_resettable, nonatomic, strong) UIFont *font;
  52. /**
  53. The color of the text. Default is black.
  54. Set a new value to this property also causes the new color to be applied to the entire `attributedText`.
  55. Get the value returns the color at the head of `attributedText`.
  56. */
  57. @property (null_resettable, nonatomic, strong) UIColor *textColor;
  58. /**
  59. The shadow color of the text. Default is nil.
  60. Set a new value to this property also causes the shadow color to be applied to the entire `attributedText`.
  61. Get the value returns the shadow color at the head of `attributedText`.
  62. */
  63. @property (nullable, nonatomic, strong) UIColor *shadowColor;
  64. /**
  65. The shadow offset of the text. Default is CGSizeZero.
  66. Set a new value to this property also causes the shadow offset to be applied to the entire `attributedText`.
  67. Get the value returns the shadow offset at the head of `attributedText`.
  68. */
  69. @property (nonatomic) CGSize shadowOffset;
  70. /**
  71. The shadow blur of the text. Default is 0.
  72. Set a new value to this property also causes the shadow blur to be applied to the entire `attributedText`.
  73. Get the value returns the shadow blur at the head of `attributedText`.
  74. */
  75. @property (nonatomic) CGFloat shadowBlurRadius;
  76. /**
  77. The technique to use for aligning the text. Default is NSTextAlignmentNatural.
  78. Set a new value to this property also causes the new alignment to be applied to the entire `attributedText`.
  79. Get the value returns the alignment at the head of `attributedText`.
  80. */
  81. @property (nonatomic) NSTextAlignment textAlignment;
  82. /**
  83. The text vertical aligmnent in container. Default is YYTextVerticalAlignmentCenter.
  84. */
  85. @property (nonatomic) YYTextVerticalAlignment textVerticalAlignment;
  86. /**
  87. The styled text displayed by the label.
  88. Set a new value to this property also replaces the value of the `text`, `font`, `textColor`,
  89. `textAlignment` and other properties in label.
  90. @discussion It only support the attributes declared in CoreText and YYTextAttribute.
  91. See `NSAttributedString+YYText` for more convenience methods to set the attributes.
  92. */
  93. @property (nullable, nonatomic, copy) NSAttributedString *attributedText;
  94. /**
  95. The technique to use for wrapping and truncating the label's text.
  96. Default is NSLineBreakByTruncatingTail.
  97. */
  98. @property (nonatomic) NSLineBreakMode lineBreakMode;
  99. /**
  100. The truncation token string used when text is truncated. Default is nil.
  101. When the value is nil, the label use "…" as default truncation token.
  102. */
  103. @property (nullable, nonatomic, copy) NSAttributedString *truncationToken;
  104. /**
  105. The maximum number of lines to use for rendering text. Default value is 1.
  106. 0 means no limit.
  107. */
  108. @property (nonatomic) NSUInteger numberOfLines;
  109. /**
  110. When `text` or `attributedText` is changed, the parser will be called to modify the text.
  111. It can be used to add code highlighting or emoticon replacement to text view.
  112. The default value is nil.
  113. See `YYTextParser` protocol for more information.
  114. */
  115. @property (nullable, nonatomic, strong) id<YYTextParser> textParser;
  116. /**
  117. The current text layout in text view. It can be used to query the text layout information.
  118. Set a new value to this property also replaces most properties in this label, such as `text`,
  119. `color`, `attributedText`, `lineBreakMode`, `textContainerPath`, `exclusionPaths` and so on.
  120. */
  121. @property (nullable, nonatomic, strong) YYTextLayout *textLayout;
  122. #pragma mark - Configuring the Text Container
  123. ///=============================================================================
  124. /// @name Configuring the Text Container
  125. ///=============================================================================
  126. /**
  127. A UIBezierPath object that specifies the shape of the text frame. Default value is nil.
  128. */
  129. @property (nullable, nonatomic, copy) UIBezierPath *textContainerPath;
  130. /**
  131. An array of UIBezierPath objects representing the exclusion paths inside the
  132. receiver's bounding rectangle. Default value is nil.
  133. */
  134. @property (nullable, nonatomic, copy) NSArray<UIBezierPath *> *exclusionPaths;
  135. /**
  136. The inset of the text container's layout area within the text view's content area.
  137. Default value is UIEdgeInsetsZero.
  138. */
  139. @property (nonatomic) UIEdgeInsets textContainerInset;
  140. /**
  141. Whether the receiver's layout orientation is vertical form. Default is NO.
  142. It may used to display CJK text.
  143. */
  144. @property (nonatomic, getter=isVerticalForm) BOOL verticalForm;
  145. /**
  146. The text line position modifier used to modify the lines' position in layout.
  147. Default value is nil.
  148. See `YYTextLinePositionModifier` protocol for more information.
  149. */
  150. @property (nullable, nonatomic, copy) id<YYTextLinePositionModifier> linePositionModifier;
  151. /**
  152. The debug option to display CoreText layout result.
  153. The default value is [YYTextDebugOption sharedDebugOption].
  154. */
  155. @property (nullable, nonatomic, copy) YYTextDebugOption *debugOption;
  156. #pragma mark - Getting the Layout Constraints
  157. ///=============================================================================
  158. /// @name Getting the Layout Constraints
  159. ///=============================================================================
  160. /**
  161. The preferred maximum width (in points) for a multiline label.
  162. @discussion This property affects the size of the label when layout constraints
  163. are applied to it. During layout, if the text extends beyond the width
  164. specified by this property, the additional text is flowed to one or more new
  165. lines, thereby increasing the height of the label. If the text is vertical
  166. form, this value will match to text height.
  167. */
  168. @property (nonatomic) CGFloat preferredMaxLayoutWidth;
  169. #pragma mark - Interacting with Text Data
  170. ///=============================================================================
  171. /// @name Interacting with Text Data
  172. ///=============================================================================
  173. /**
  174. When user tap the label, this action will be called (similar to tap gesture).
  175. The default value is nil.
  176. */
  177. @property (nullable, nonatomic, copy) YYTextAction textTapAction;
  178. /**
  179. When user long press the label, this action will be called (similar to long press gesture).
  180. The default value is nil.
  181. */
  182. @property (nullable, nonatomic, copy) YYTextAction textLongPressAction;
  183. /**
  184. When user tap the highlight range of text, this action will be called.
  185. The default value is nil.
  186. */
  187. @property (nullable, nonatomic, copy) YYTextAction highlightTapAction;
  188. /**
  189. When user long press the highlight range of text, this action will be called.
  190. The default value is nil.
  191. */
  192. @property (nullable, nonatomic, copy) YYTextAction highlightLongPressAction;
  193. #pragma mark - Configuring the Display Mode
  194. ///=============================================================================
  195. /// @name Configuring the Display Mode
  196. ///=============================================================================
  197. /**
  198. A Boolean value indicating whether the layout and rendering codes are running
  199. asynchronously on background threads.
  200. The default value is `NO`.
  201. */
  202. @property (nonatomic) BOOL displaysAsynchronously;
  203. /**
  204. If the value is YES, and the layer is rendered asynchronously, then it will
  205. set label.layer.contents to nil before display.
  206. The default value is `YES`.
  207. @discussion When the asynchronously display is enabled, the layer's content will
  208. be updated after the background render process finished. If the render process
  209. can not finished in a vsync time (1/60 second), the old content will be still kept
  210. for display. You may manually clear the content by set the layer.contents to nil
  211. after you update the label's properties, or you can just set this property to YES.
  212. */
  213. @property (nonatomic) BOOL clearContentsBeforeAsynchronouslyDisplay;
  214. /**
  215. If the value is YES, and the layer is rendered asynchronously, then it will add
  216. a fade animation on layer when the contents of layer changed.
  217. The default value is `YES`.
  218. */
  219. @property (nonatomic) BOOL fadeOnAsynchronouslyDisplay;
  220. /**
  221. If the value is YES, then it will add a fade animation on layer when some range
  222. of text become highlighted.
  223. The default value is `YES`.
  224. */
  225. @property (nonatomic) BOOL fadeOnHighlight;
  226. /**
  227. Ignore common properties (such as text, font, textColor, attributedText...) and
  228. only use "textLayout" to display content.
  229. The default value is `NO`.
  230. @discussion If you control the label content only through "textLayout", then
  231. you may set this value to YES for higher performance.
  232. */
  233. @property (nonatomic) BOOL ignoreCommonProperties;
  234. /*
  235. Tips:
  236. 1. If you only need a UILabel alternative to display rich text and receive link touch event,
  237. you do not need to adjust the display mode properties.
  238. 2. If you have performance issues, you may enable the asynchronous display mode
  239. by setting the `displaysAsynchronously` to YES.
  240. 3. If you want to get the highest performance, you should do text layout with
  241. `YYTextLayout` class in background thread. Here's an example:
  242. YYLabel *label = [YYLabel new];
  243. label.displaysAsynchronously = YES;
  244. label.ignoreCommonProperties = YES;
  245. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  246. // Create attributed string.
  247. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"Some Text"];
  248. text.yy_font = [UIFont systemFontOfSize:16];
  249. text.yy_color = [UIColor grayColor];
  250. [text yy_setColor:[UIColor redColor] range:NSMakeRange(0, 4)];
  251. // Create text container
  252. YYTextContainer *container = [YYTextContainer new];
  253. container.size = CGSizeMake(100, CGFLOAT_MAX);
  254. container.maximumNumberOfRows = 0;
  255. // Generate a text layout.
  256. YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text];
  257. dispatch_async(dispatch_get_main_queue(), ^{
  258. label.size = layout.textBoundingSize;
  259. label.textLayout = layout;
  260. });
  261. });
  262. */
  263. @end
  264. #else // TARGET_INTERFACE_BUILDER
  265. IB_DESIGNABLE
  266. @interface YYLabel : UIView <NSCoding>
  267. @property (nullable, nonatomic, copy) IBInspectable NSString *text;
  268. @property (null_resettable, nonatomic, strong) IBInspectable UIColor *textColor;
  269. @property (nullable, nonatomic, strong) IBInspectable NSString *fontName_;
  270. @property (nonatomic) IBInspectable CGFloat fontSize_;
  271. @property (nonatomic) IBInspectable BOOL fontIsBold_;
  272. @property (nonatomic) IBInspectable NSUInteger numberOfLines;
  273. @property (nonatomic) IBInspectable NSInteger lineBreakMode;
  274. @property (nonatomic) IBInspectable CGFloat preferredMaxLayoutWidth;
  275. @property (nonatomic, getter=isVerticalForm) IBInspectable BOOL verticalForm;
  276. @property (nonatomic) IBInspectable NSInteger textAlignment;
  277. @property (nonatomic) IBInspectable NSInteger textVerticalAlignment;
  278. @property (nullable, nonatomic, strong) IBInspectable UIColor *shadowColor;
  279. @property (nonatomic) IBInspectable CGPoint shadowOffset;
  280. @property (nonatomic) IBInspectable CGFloat shadowBlurRadius;
  281. @property (nullable, nonatomic, copy) IBInspectable NSAttributedString *attributedText;
  282. @property (nonatomic) IBInspectable CGFloat insetTop_;
  283. @property (nonatomic) IBInspectable CGFloat insetBottom_;
  284. @property (nonatomic) IBInspectable CGFloat insetLeft_;
  285. @property (nonatomic) IBInspectable CGFloat insetRight_;
  286. @property (nonatomic) IBInspectable BOOL debugEnabled_;
  287. @property (null_resettable, nonatomic, strong) UIFont *font;
  288. @property (nullable, nonatomic, copy) NSAttributedString *truncationToken;
  289. @property (nullable, nonatomic, strong) id<YYTextParser> textParser;
  290. @property (nullable, nonatomic, strong) YYTextLayout *textLayout;
  291. @property (nullable, nonatomic, copy) UIBezierPath *textContainerPath;
  292. @property (nullable, nonatomic, copy) NSArray<UIBezierPath*> *exclusionPaths;
  293. @property (nonatomic) UIEdgeInsets textContainerInset;
  294. @property (nullable, nonatomic, copy) id<YYTextLinePositionModifier> linePositionModifier;
  295. @property (nonnull, nonatomic, copy) YYTextDebugOption *debugOption;
  296. @property (nullable, nonatomic, copy) YYTextAction textTapAction;
  297. @property (nullable, nonatomic, copy) YYTextAction textLongPressAction;
  298. @property (nullable, nonatomic, copy) YYTextAction highlightTapAction;
  299. @property (nullable, nonatomic, copy) YYTextAction highlightLongPressAction;
  300. @property (nonatomic) BOOL displaysAsynchronously;
  301. @property (nonatomic) BOOL clearContentsBeforeAsynchronouslyDisplay;
  302. @property (nonatomic) BOOL fadeOnAsynchronouslyDisplay;
  303. @property (nonatomic) BOOL fadeOnHighlight;
  304. @property (nonatomic) BOOL ignoreCommonProperties;
  305. @end
  306. #endif // !TARGET_INTERFACE_BUILDER
  307. NS_ASSUME_NONNULL_END