MDHTMLLabel.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // MDHTMLLabel.h
  3. // MDHTMLLabel
  4. //
  5. // Copyright (c) 2013 Matt Donnelly
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #import <UIKit/UIKit.h>
  26. #import <CoreText/CoreText.h>
  27. typedef NS_ENUM(NSUInteger, MDHTMLLabelVerticalAlignment) {
  28. MDHTMLLabelVerticalAlignmentCenter = 0,
  29. MDHTMLLabelVerticalAlignmentTop = 1,
  30. MDHTMLLabelVerticalAlignmentBottom = 2,
  31. };
  32. @class MDHTMLLabel;
  33. @protocol MDHTMLLabelDelegate <NSObject>
  34. ///-----------------------------------
  35. /// @name Responding to Link Selection
  36. ///-----------------------------------
  37. @optional
  38. /**
  39. Tells the delegate that the user did select a link
  40. @param label The label that the link was selected in.
  41. @param URL the URL of link the user selected.
  42. */
  43. - (void)HTMLLabel:(MDHTMLLabel *)label didSelectLinkWithURL:(NSURL*)URL;
  44. /**
  45. Tells the delegate that the user did hold a link
  46. @param label The label that the link was held in.
  47. @param URL the URL of link the user held.
  48. */
  49. - (void)HTMLLabel:(MDHTMLLabel *)label didHoldLinkWithURL:(NSURL*)URL;
  50. @end
  51. /**
  52. `MDHTMLLabel` is a lightweight, easy to use replacement for `UILabel` which allows you to fully customize the appearence of the text using HTML.
  53. # Differences Between `MDHTMLLabel` and `UILabel`
  54. For the most part, `MDHTMLLabel` behaves the same as `UILabel`. The following are notable exceptions, in which `MDHTMLLabel` properties may act differently:
  55. - `lineBreakMode` - This property displays only the first line when the value is `NSLineBreakByTruncatingHead`, `NSLineBreakByTruncatingMiddle`, or `NSLineBreakByTruncatingTail`
  56. - `adjustsFontsizeToFitWidth` - This property is effective for any value of `numberOfLines` greater than zero.
  57. @warning Any properties changed on the label after setting the text will not be reflected until a subsequent call to `setHtmlText:`. This is to say, order of operations matters in this case. For example, if the label text color is originally black when the text is set, changing the text color to red will have no effect on the display of the label until the text is set once again.
  58. */
  59. @interface MDHTMLLabel : UILabel
  60. ///-----------------------------
  61. /// @name Accessing the Delegate
  62. ///-----------------------------
  63. /**
  64. The receiver's delegate.
  65. @discussion A `MDHTMLLabelDelegate` delegate responds to messages sent by tapping on links in the label. You can use the delegate to respond to links referencing a URL, address, phone number, date, or date with a specified time zone and duration.
  66. */
  67. @property (nonatomic, weak) IBOutlet NSObject <MDHTMLLabelDelegate> *delegate;
  68. ///----------------------------------
  69. /// @name Setting the Text Attributes
  70. ///----------------------------------
  71. /**
  72. The text that HTML tags will be parsed for and rendered by the label.
  73. */
  74. @property (nonatomic, copy) NSString *htmlText;
  75. ///--------------------------------------------
  76. /// @name Detecting, Accessing, & Styling Links
  77. ///--------------------------------------------
  78. /**
  79. A dictionary containing the `NSAttributedString` attributes to be applied to links detected or manually added to the label text. The default link style is the label's tintColor on iOS 7 and blue below iOS 7.
  80. */
  81. @property (nonatomic, strong) NSDictionary *linkAttributes;
  82. /**
  83. A dictionary containing the `NSAttributedString` attributes to be applied to links when they are in the active state. Supply `nil` or an empty dictionary to opt out of active link styling. The default active link style is red.
  84. */
  85. @property (nonatomic, strong) NSDictionary *activeLinkAttributes;
  86. /**
  87. A dictionary containing the `NSAttributedString` attributes to be applied to links when they are in the inactive state, which is triggered a change in `tintColor` in iOS 7. Supply `nil` or an empty dictionary to opt out of inactive link styling. The default inactive link style is gray.
  88. */
  89. @property (nonatomic, strong) NSDictionary *inactiveLinkAttributes;
  90. /**
  91. The length of time the user most hold a link to trigger the 'HTMLLabel:didHoldLinkWithURL' delegate method. The value is 0.5 by default.
  92. */
  93. @property (nonatomic, assign) NSTimeInterval minimumPressDuration;
  94. ///---------------------------------------
  95. /// @name Acccessing Text Style Attributes
  96. ///---------------------------------------
  97. /**
  98. The shadow blur radius for the label. A value of 0 indicates no blur, while larger values produce correspondingly larger blurring. This value must not be negative. The default value is 0.
  99. */
  100. @property (nonatomic, assign) CGFloat shadowRadius;
  101. /**
  102. The shadow blur radius for the label when the label's `highlighted` property is `YES`. A value of 0 indicates no blur, while larger values produce correspondingly larger blurring. This value must not be negative. The default value is 0.
  103. */
  104. @property (nonatomic, assign) CGFloat highlightedShadowRadius;
  105. /**
  106. The shadow offset for the label when the label's `highlighted` property is `YES`. A size of {0, 0} indicates no offset, with positive values extending down and to the right. The default size is {0, 0}.
  107. */
  108. @property (nonatomic, assign) CGSize highlightedShadowOffset;
  109. /**
  110. The shadow color for the label when the label's `highlighted` property is `YES`. The default value is `nil` (no shadow color).
  111. */
  112. @property (nonatomic, strong) UIColor *highlightedShadowColor;
  113. /**
  114. The font name of the custom font which should be used for bold text. If not set the system bold font will be used.
  115. */
  116. @property(nonatomic, copy) NSString *customBoldFontName;
  117. /**
  118. The font name of the custom font which should be used for italic text. If not set the system italic font will be used.
  119. */
  120. @property(nonatomic, copy) NSString *customItalicFontName;
  121. /**
  122. The font name of the custom font which should be used for bold italic text. If not set the system font with style BoldOblique will be used.
  123. */
  124. @property(nonatomic, copy) NSString *customBoldItalicFontName;
  125. ///--------------------------------------------
  126. /// @name Acccessing Paragraph Style Attributes
  127. ///--------------------------------------------
  128. /**
  129. The distance, in points, from the leading margin of a frame to the beginning of the paragraph's first line. This value is always nonnegative, and is 0.0 by default.
  130. */
  131. @property (nonatomic, assign) CGFloat firstLineIndent;
  132. /**
  133. The space in points added between lines within the paragraph. This value is always nonnegative and is 0.0 by default.
  134. */
  135. @property (nonatomic, assign) CGFloat leading;
  136. /**
  137. The line height multiple. This value is 1.0 by default.
  138. */
  139. @property (nonatomic, assign) CGFloat lineHeightMultiple;
  140. /**
  141. The distance, in points, from the margin to the text container. This value is `UIEdgeInsetsZero` by default.
  142. @discussion The `UIEdgeInset` members correspond to paragraph style properties rather than a particular geometry, and can change depending on the writing direction.
  143. ## `UIEdgeInset` Member Correspondence With `CTParagraphStyleSpecifier` Values:
  144. - `top`: `kCTParagraphStyleSpecifierParagraphSpacingBefore`
  145. - `left`: `kCTParagraphStyleSpecifierHeadIndent`
  146. - `bottom`: `kCTParagraphStyleSpecifierParagraphSpacing`
  147. - `right`: `kCTParagraphStyleSpecifierTailIndent`
  148. */
  149. @property (nonatomic, assign) UIEdgeInsets textInsets;
  150. /**
  151. The vertical text alignment for the label, for when the frame size is greater than the text rect size. The vertical alignment is `MDHTMLLabelVerticalAlignmentCenter` by default.
  152. */
  153. @property (nonatomic, assign) MDHTMLLabelVerticalAlignment verticalAlignment;
  154. ///--------------------------------------------
  155. /// @name Accessing Truncation Token Appearance
  156. ///--------------------------------------------
  157. /**
  158. The truncation token that appears at the end of the truncated line. `nil` by default.
  159. @discussion When truncation is enabled for the label, by setting `lineBreakMode` to either `NSLineBreakByTruncatingHead`, `NSLineBreakByTruncatingTail`, or `NSLineBreakByTruncatingMiddle`, the token used to terminate the truncated line will be `truncationTokenString` if defined, otherwise the Unicode Character 'HORIZONTAL ELLIPSIS' (U+2026).
  160. */
  161. @property (nonatomic, strong) NSString *truncationTokenString;
  162. /**
  163. The attributes to apply to the truncation token at the end of a truncated line. If unspecified, attributes will be inherited from the preceding character.
  164. */
  165. @property (nonatomic, strong) NSDictionary *truncationTokenStringAttributes;
  166. @property (nonatomic) BOOL autoDetectUrls;
  167. ///--------------------------------------------
  168. /// @name Calculating Size of HTML String
  169. ///--------------------------------------------
  170. /**
  171. Calculate and return the size that best fits a HTML string, given the specified constraints on size and number of lines.
  172. @param htmlString - the string to render
  173. @param font - the font to render the string with
  174. @param size - the size to constrain the string to
  175. @param numberOfLines - the number of lines to limit the string to
  176. @param autoDetectUrls - detect URLs
  177. @return The size that fits the HTML string within the specified constraints.
  178. */
  179. + (CGFloat)sizeThatFitsHTMLString:(NSString *)htmlString
  180. withFont:(UIFont *)font
  181. constraints:(CGSize)size
  182. limitedToNumberOfLines:(NSUInteger)numberOfLines
  183. autoDetectUrls:(BOOL)autoDetectUrls;
  184. @end