YYTextView.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // YYTextView.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  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(<YYKit/YYKit.h>)
  13. #import <YYKit/YYTextParser.h>
  14. #import <YYKit/YYTextLayout.h>
  15. #import <YYKit/YYTextAttribute.h>
  16. #else
  17. #import "YYTextParser.h"
  18. #import "YYTextLayout.h"
  19. #import "YYTextAttribute.h"
  20. #endif
  21. @class YYTextView;
  22. NS_ASSUME_NONNULL_BEGIN
  23. /**
  24. The YYTextViewDelegate protocol defines a set of optional methods you can use
  25. to receive editing-related messages for YYTextView objects.
  26. @discussion The API and behavior is similar to UITextViewDelegate,
  27. see UITextViewDelegate's documentation for more information.
  28. */
  29. @protocol YYTextViewDelegate <NSObject, UIScrollViewDelegate>
  30. @optional
  31. - (BOOL)textViewShouldBeginEditing:(YYTextView *)textView;
  32. - (BOOL)textViewShouldEndEditing:(YYTextView *)textView;
  33. - (void)textViewDidBeginEditing:(YYTextView *)textView;
  34. - (void)textViewDidEndEditing:(YYTextView *)textView;
  35. - (BOOL)textView:(YYTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
  36. - (void)textViewDidChange:(YYTextView *)textView;
  37. - (void)textViewDidChangeSelection:(YYTextView *)textView;
  38. - (BOOL)textView:(YYTextView *)textView shouldTapHighlight:(YYTextHighlight *)highlight inRange:(NSRange)characterRange;
  39. - (void)textView:(YYTextView *)textView didTapHighlight:(YYTextHighlight *)highlight inRange:(NSRange)characterRange rect:(CGRect)rect;
  40. - (BOOL)textView:(YYTextView *)textView shouldLongPressHighlight:(YYTextHighlight *)highlight inRange:(NSRange)characterRange;
  41. - (void)textView:(YYTextView *)textView didLongPressHighlight:(YYTextHighlight *)highlight inRange:(NSRange)characterRange rect:(CGRect)rect;
  42. @end
  43. #if !TARGET_INTERFACE_BUILDER
  44. /**
  45. The YYTextView class implements the behavior for a scrollable, multiline text region.
  46. @discussion The API and behavior is similar to UITextView, but provides more features:
  47. * It extends the CoreText attributes to support more text effects.
  48. * It allows to add UIImage, UIView and CALayer as text attachments.
  49. * It allows to add 'highlight' link to some range of text to allow user interact with.
  50. * It allows to add exclusion paths to control text container's shape.
  51. * It supports vertical form layout to display and edit CJK text.
  52. * It allows user to copy/paste image and attributed text from/to text view.
  53. * It allows to set an attributed text as placeholder.
  54. See NSAttributedString+YYText.h for more convenience methods to set the attributes.
  55. See YYTextAttribute.h and YYTextLayout.h for more information.
  56. */
  57. @interface YYTextView : UIScrollView <UITextInput>
  58. #pragma mark - Accessing the Delegate
  59. ///=============================================================================
  60. /// @name Accessing the Delegate
  61. ///=============================================================================
  62. @property (nullable, nonatomic, weak) id<YYTextViewDelegate> delegate;
  63. #pragma mark - Configuring the Text Attributes
  64. ///=============================================================================
  65. /// @name Configuring the Text Attributes
  66. ///=============================================================================
  67. /**
  68. The text displayed by the text view.
  69. Set a new value to this property also replaces the text in `attributedText`.
  70. Get the value returns the plain text in `attributedText`.
  71. */
  72. @property (null_resettable, nonatomic, copy) NSString *text;
  73. /**
  74. The font of the text. Default is 12-point system font.
  75. Set a new value to this property also causes the new font to be applied to the entire `attributedText`.
  76. Get the value returns the font at the head of `attributedText`.
  77. */
  78. @property (nullable, nonatomic, strong) UIFont *font;
  79. /**
  80. The color of the text. Default is black.
  81. Set a new value to this property also causes the new color to be applied to the entire `attributedText`.
  82. Get the value returns the color at the head of `attributedText`.
  83. */
  84. @property (nullable, nonatomic, strong) UIColor *textColor;
  85. /**
  86. The technique to use for aligning the text. Default is NSTextAlignmentNatural.
  87. Set a new value to this property also causes the new alignment to be applied to the entire `attributedText`.
  88. Get the value returns the alignment at the head of `attributedText`.
  89. */
  90. @property (nonatomic) NSTextAlignment textAlignment;
  91. /**
  92. The text vertical aligmnent in container. Default is YYTextVerticalAlignmentTop.
  93. */
  94. @property (nonatomic) YYTextVerticalAlignment textVerticalAlignment;
  95. /**
  96. The types of data converted to clickable URLs in the text view. Default is UIDataDetectorTypeNone.
  97. The tap or long press action should be handled by delegate.
  98. */
  99. @property (nonatomic) UIDataDetectorTypes dataDetectorTypes;
  100. /**
  101. The attributes to apply to links at normal state. Default is light blue color.
  102. When a range of text is detected by the `dataDetectorTypes`, this value would be
  103. used to modify the original attributes in the range.
  104. */
  105. @property (nullable, nonatomic, copy) NSDictionary<NSString *, id> *linkTextAttributes;
  106. /**
  107. The attributes to apply to links at highlight state. Default is a gray border.
  108. When a range of text is detected by the `dataDetectorTypes` and the range was touched by user,
  109. this value would be used to modify the original attributes in the range.
  110. */
  111. @property (nullable, nonatomic, copy) NSDictionary<NSString *, id> *highlightTextAttributes;
  112. /**
  113. The attributes to apply to new text being entered by the user.
  114. When the text view's selection changes, this value is reset automatically.
  115. */
  116. @property (nullable, nonatomic, copy) NSDictionary<NSString *, id> *typingAttributes;
  117. /**
  118. The styled text displayed by the text view.
  119. Set a new value to this property also replaces the value of the `text`, `font`, `textColor`,
  120. `textAlignment` and other properties in text view.
  121. @discussion It only support the attributes declared in CoreText and YYTextAttribute.
  122. See `NSAttributedString+YYText` for more convenience methods to set the attributes.
  123. */
  124. @property (nullable, nonatomic, copy) NSAttributedString *attributedText;
  125. /**
  126. When `text` or `attributedText` is changed, the parser will be called to modify the text.
  127. It can be used to add code highlighting or emoticon replacement to text view.
  128. The default value is nil.
  129. See `YYTextParser` protocol for more information.
  130. */
  131. @property (nullable, nonatomic, strong) id<YYTextParser> textParser;
  132. /**
  133. The current text layout in text view (readonly).
  134. It can be used to query the text layout information.
  135. */
  136. @property (nullable, nonatomic, strong, readonly) YYTextLayout *textLayout;
  137. #pragma mark - Configuring the Placeholder
  138. ///=============================================================================
  139. /// @name Configuring the Placeholder
  140. ///=============================================================================
  141. /**
  142. The placeholder text displayed by the text view (when the text view is empty).
  143. Set a new value to this property also replaces the text in `placeholderAttributedText`.
  144. Get the value returns the plain text in `placeholderAttributedText`.
  145. */
  146. @property (nullable, nonatomic, copy) NSString *placeholderText;
  147. /**
  148. The font of the placeholder text. Default is same as `font` property.
  149. Set a new value to this property also causes the new font to be applied to the entire `placeholderAttributedText`.
  150. Get the value returns the font at the head of `placeholderAttributedText`.
  151. */
  152. @property (nullable, nonatomic, strong) UIFont *placeholderFont;
  153. /**
  154. The color of the placeholder text. Default is gray.
  155. Set a new value to this property also causes the new color to be applied to the entire `placeholderAttributedText`.
  156. Get the value returns the color at the head of `placeholderAttributedText`.
  157. */
  158. @property (nullable, nonatomic, strong) UIColor *placeholderTextColor;
  159. /**
  160. The styled placeholder text displayed by the text view (when the text view is empty).
  161. Set a new value to this property also replaces the value of the `placeholderText`,
  162. `placeholderFont`, `placeholderTextColor`.
  163. @discussion It only support the attributes declared in CoreText and YYTextAttribute.
  164. See `NSAttributedString+YYText` for more convenience methods to set the attributes.
  165. */
  166. @property (nullable, nonatomic, copy) NSAttributedString *placeholderAttributedText;
  167. #pragma mark - Configuring the Text Container
  168. ///=============================================================================
  169. /// @name Configuring the Text Container
  170. ///=============================================================================
  171. /**
  172. The inset of the text container's layout area within the text view's content area.
  173. */
  174. @property (nonatomic) UIEdgeInsets textContainerInset;
  175. /**
  176. An array of UIBezierPath objects representing the exclusion paths inside the
  177. receiver's bounding rectangle. Default value is nil.
  178. */
  179. @property (nullable, nonatomic, copy) NSArray<UIBezierPath *> *exclusionPaths;
  180. /**
  181. Whether the receiver's layout orientation is vertical form. Default is NO.
  182. It may used to edit/display CJK text.
  183. */
  184. @property (nonatomic, getter=isVerticalForm) BOOL verticalForm;
  185. /**
  186. The text line position modifier used to modify the lines' position in layout.
  187. See `YYTextLinePositionModifier` protocol for more information.
  188. */
  189. @property (nullable, nonatomic, copy) id<YYTextLinePositionModifier> linePositionModifier;
  190. /**
  191. The debug option to display CoreText layout result.
  192. The default value is [YYTextDebugOption sharedDebugOption].
  193. */
  194. @property (nullable, nonatomic, copy) YYTextDebugOption *debugOption;
  195. #pragma mark - Working with the Selection and Menu
  196. ///=============================================================================
  197. /// @name Working with the Selection and Menu
  198. ///=============================================================================
  199. /**
  200. Scrolls the receiver until the text in the specified range is visible.
  201. */
  202. - (void)scrollRangeToVisible:(NSRange)range;
  203. /**
  204. The current selection range of the receiver.
  205. */
  206. @property (nonatomic) NSRange selectedRange;
  207. /**
  208. A Boolean value indicating whether inserting text replaces the previous contents.
  209. The default value is NO.
  210. */
  211. @property (nonatomic) BOOL clearsOnInsertion;
  212. /**
  213. A Boolean value indicating whether the receiver is selectable. Default is YES.
  214. When the value of this property is NO, user cannot select content or edit text.
  215. */
  216. @property (nonatomic, getter=isSelectable) BOOL selectable;
  217. /**
  218. A Boolean value indicating whether the receiver is highlightable. Default is YES.
  219. When the value of this property is NO, user cannot interact with the highlight range of text.
  220. */
  221. @property (nonatomic, getter=isHighlightable) BOOL highlightable;
  222. /**
  223. A Boolean value indicating whether the receiver is editable. Default is YES.
  224. When the value of this property is NO, user cannot edit text.
  225. */
  226. @property (nonatomic, getter=isEditable) BOOL editable;
  227. /**
  228. A Boolean value indicating whether the receiver can paste image from pasteboard. Default is NO.
  229. When the value of this property is YES, user can paste image from pasteboard via "paste" menu.
  230. */
  231. @property (nonatomic) BOOL allowsPasteImage;
  232. /**
  233. A Boolean value indicating whether the receiver can paste attributed text from pasteboard. Default is NO.
  234. When the value of this property is YES, user can paste attributed text from pasteboard via "paste" menu.
  235. */
  236. @property (nonatomic) BOOL allowsPasteAttributedString;
  237. /**
  238. A Boolean value indicating whether the receiver can copy attributed text to pasteboard. Default is YES.
  239. When the value of this property is YES, user can copy attributed text (with attachment image)
  240. from text view to pasteboard via "copy" menu.
  241. */
  242. @property (nonatomic) BOOL allowsCopyAttributedString;
  243. #pragma mark - Manage the undo and redo
  244. ///=============================================================================
  245. /// @name Manage the undo and redo
  246. ///=============================================================================
  247. /**
  248. A Boolean value indicating whether the receiver can undo and redo typing with
  249. shake gesture. The default value is YES.
  250. */
  251. @property (nonatomic) BOOL allowsUndoAndRedo;
  252. /**
  253. The maximum undo/redo level. The default value is 20.
  254. */
  255. @property (nonatomic) NSUInteger maximumUndoLevel;
  256. #pragma mark - Replacing the System Input Views
  257. ///=============================================================================
  258. /// @name Replacing the System Input Views
  259. ///=============================================================================
  260. /**
  261. The custom input view to display when the text view becomes the first responder.
  262. It can be used to replace system keyboard.
  263. @discussion If set the value while first responder, it will not take effect until
  264. 'reloadInputViews' is called.
  265. */
  266. @property (nullable, nonatomic, readwrite, strong) __kindof UIView *inputView;
  267. /**
  268. The custom accessory view to display when the text view becomes the first responder.
  269. It can be used to add a toolbar at the top of keyboard.
  270. @discussion If set the value while first responder, it will not take effect until
  271. 'reloadInputViews' is called.
  272. */
  273. @property (nullable, nonatomic, readwrite, strong) __kindof UIView *inputAccessoryView;
  274. /**
  275. If you use an custom accessory view without "inputAccessoryView" property,
  276. you may set the accessory view's height. It may used by auto scroll calculation.
  277. */
  278. @property (nonatomic) CGFloat extraAccessoryViewHeight;
  279. @end
  280. #else // TARGET_INTERFACE_BUILDER
  281. IB_DESIGNABLE
  282. @interface YYTextView : UIScrollView <UITextInput>
  283. @property (null_resettable, nonatomic, copy) IBInspectable NSString *text;
  284. @property (nullable, nonatomic, strong) IBInspectable UIColor *textColor;
  285. @property (nullable, nonatomic, strong) IBInspectable NSString *fontName_;
  286. @property (nonatomic) IBInspectable CGFloat fontSize_;
  287. @property (nonatomic) IBInspectable BOOL fontIsBold_;
  288. @property (nonatomic) IBInspectable NSTextAlignment textAlignment;
  289. @property (nonatomic) IBInspectable YYTextVerticalAlignment textVerticalAlignment;
  290. @property (nullable, nonatomic, copy) IBInspectable NSString *placeholderText;
  291. @property (nullable, nonatomic, strong) IBInspectable UIColor *placeholderTextColor;
  292. @property (nullable, nonatomic, strong) IBInspectable NSString *placeholderFontName_;
  293. @property (nonatomic) IBInspectable CGFloat placeholderFontSize_;
  294. @property (nonatomic) IBInspectable BOOL placeholderFontIsBold_;
  295. @property (nonatomic, getter=isVerticalForm) IBInspectable BOOL verticalForm;
  296. @property (nonatomic) IBInspectable BOOL clearsOnInsertion;
  297. @property (nonatomic, getter=isSelectable) IBInspectable BOOL selectable;
  298. @property (nonatomic, getter=isHighlightable) IBInspectable BOOL highlightable;
  299. @property (nonatomic, getter=isEditable) IBInspectable BOOL editable;
  300. @property (nonatomic) IBInspectable BOOL allowsPasteImage;
  301. @property (nonatomic) IBInspectable BOOL allowsPasteAttributedString;
  302. @property (nonatomic) IBInspectable BOOL allowsCopyAttributedString;
  303. @property (nonatomic) IBInspectable BOOL allowsUndoAndRedo;
  304. @property (nonatomic) IBInspectable NSUInteger maximumUndoLevel;
  305. @property (nonatomic) IBInspectable CGFloat insetTop_;
  306. @property (nonatomic) IBInspectable CGFloat insetBottom_;
  307. @property (nonatomic) IBInspectable CGFloat insetLeft_;
  308. @property (nonatomic) IBInspectable CGFloat insetRight_;
  309. @property (nonatomic) IBInspectable BOOL debugEnabled_;
  310. @property (nullable, nonatomic, weak) id<YYTextViewDelegate> delegate;
  311. @property (nullable, nonatomic, strong) UIFont *font;
  312. @property (nonatomic) UIDataDetectorTypes dataDetectorTypes;
  313. @property (nullable, nonatomic, copy) NSDictionary *linkTextAttributes;
  314. @property (nullable, nonatomic, copy) NSDictionary *highlightTextAttributes;
  315. @property (nullable, nonatomic, copy) NSDictionary *typingAttributes;
  316. @property (nullable, nonatomic, copy) NSAttributedString *attributedText;
  317. @property (nullable, nonatomic, strong) id<YYTextParser> textParser;
  318. @property (nullable, nonatomic, strong, readonly) YYTextLayout *textLayout;
  319. @property (nullable, nonatomic, strong) UIFont *placeholderFont;
  320. @property (nullable, nonatomic, copy) NSAttributedString *placeholderAttributedText;
  321. @property (nonatomic) UIEdgeInsets textContainerInset;
  322. @property (nullable, nonatomic, copy) NSArray *exclusionPaths;
  323. @property (nullable, nonatomic, copy) id<YYTextLinePositionModifier> linePositionModifier;
  324. @property (nullable, nonatomic, copy) YYTextDebugOption *debugOption;
  325. - (void)scrollRangeToVisible:(NSRange)range;
  326. @property (nonatomic) NSRange selectedRange;
  327. @property (nullable, nonatomic, readwrite, strong) __kindof UIView *inputView;
  328. @property (nullable, nonatomic, readwrite, strong) __kindof UIView *inputAccessoryView;
  329. @property (nonatomic) CGFloat extraAccessoryViewHeight;
  330. @end
  331. #endif // !TARGET_INTERFACE_BUILDER
  332. // Notifications, see UITextView's documentation for more information.
  333. UIKIT_EXTERN NSString *const YYTextViewTextDidBeginEditingNotification;
  334. UIKIT_EXTERN NSString *const YYTextViewTextDidChangeNotification;
  335. UIKIT_EXTERN NSString *const YYTextViewTextDidEndEditingNotification;
  336. NS_ASSUME_NONNULL_END