YYTextParser.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // YYTextParser.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 15/3/6.
  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. /**
  14. The YYTextParser protocol declares the required method for YYTextView and YYLabel
  15. to modify the text during editing.
  16. You can implement this protocol to add code highlighting or emoticon replacement for
  17. YYTextView and YYLabel. See `YYTextSimpleMarkdownParser` and `YYTextSimpleEmoticonParser` for example.
  18. */
  19. @protocol YYTextParser <NSObject>
  20. @required
  21. /**
  22. When text is changed in YYTextView or YYLabel, this method will be called.
  23. @param text The original attributed string. This method may parse the text and
  24. change the text attributes or content.
  25. @param selectedRange Current selected range in `text`.
  26. This method should correct the range if the text content is changed. If there's
  27. no selected range (such as YYLabel), this value is NULL.
  28. @return If the 'text' is modified in this method, returns `YES`, otherwise returns `NO`.
  29. */
  30. - (BOOL)parseText:(nullable NSMutableAttributedString *)text selectedRange:(nullable NSRangePointer)selectedRange;
  31. @end
  32. /**
  33. A simple markdown parser.
  34. It'a very simple markdown parser, you can use this parser to highlight some
  35. small piece of markdown text.
  36. This markdown parser use regular expression to parse text, slow and weak.
  37. If you want to write a better parser, try these projests:
  38. https://github.com/NimbusKit/markdown
  39. https://github.com/dreamwieber/AttributedMarkdown
  40. https://github.com/indragiek/CocoaMarkdown
  41. Or you can use lex/yacc to generate your custom parser.
  42. */
  43. @interface YYTextSimpleMarkdownParser : NSObject <YYTextParser>
  44. @property (nonatomic) CGFloat fontSize; ///< default is 14
  45. @property (nonatomic) CGFloat headerFontSize; ///< default is 20
  46. @property (nullable, nonatomic, strong) UIColor *textColor;
  47. @property (nullable, nonatomic, strong) UIColor *controlTextColor;
  48. @property (nullable, nonatomic, strong) UIColor *headerTextColor;
  49. @property (nullable, nonatomic, strong) UIColor *inlineTextColor;
  50. @property (nullable, nonatomic, strong) UIColor *codeTextColor;
  51. @property (nullable, nonatomic, strong) UIColor *linkTextColor;
  52. - (void)setColorWithBrightTheme; ///< reset the color properties to pre-defined value.
  53. - (void)setColorWithDarkTheme; ///< reset the color properties to pre-defined value.
  54. @end
  55. /**
  56. A simple emoticon parser.
  57. Use this parser to map some specified piece of string to image emoticon.
  58. Example: "Hello :smile:" -> "Hello 😀"
  59. It can also be used to extend the "unicode emoticon".
  60. */
  61. @interface YYTextSimpleEmoticonParser : NSObject <YYTextParser>
  62. /**
  63. The custom emoticon mapper.
  64. The key is a specified plain string, such as @":smile:".
  65. The value is a UIImage which will replace the specified plain string in text.
  66. */
  67. @property (nullable, copy) NSDictionary<NSString *, __kindof UIImage *> *emoticonMapper;
  68. @end
  69. NS_ASSUME_NONNULL_END