KILabel.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /***********************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2013 Matthew Styles
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * 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, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. ***********************************************************************************/
  25. #import <UIKit/UIKit.h>
  26. NS_ASSUME_NONNULL_BEGIN
  27. /**
  28. * Constants for identifying link types we can detect
  29. */
  30. typedef NS_ENUM(NSUInteger, KILinkType)
  31. {
  32. /**
  33. * Usernames starting with "@" token
  34. */
  35. KILinkTypeUserHandle,
  36. /**
  37. * Hashtags starting with "#" token
  38. */
  39. KILinkTypeHashtag,
  40. /**
  41. * URLs, http etc
  42. */
  43. KILinkTypeURL,
  44. };
  45. /**
  46. * Flags for specifying combinations of link types as a bitmask
  47. */
  48. typedef NS_OPTIONS(NSUInteger, KILinkTypeOption)
  49. {
  50. /**
  51. * No links
  52. */
  53. KILinkTypeOptionNone = 0,
  54. /**
  55. * Specifies to include KILinkTypeUserHandle links
  56. */
  57. KILinkTypeOptionUserHandle = 1 << KILinkTypeUserHandle,
  58. /**
  59. * Specifies to include KILinkTypeHashtag links
  60. */
  61. KILinkTypeOptionHashtag = 1 << KILinkTypeHashtag,
  62. /**
  63. * Specifies to include KILinkTypeURL links
  64. */
  65. KILinkTypeOptionURL = 1 << KILinkTypeURL,
  66. /**
  67. * Convenience contstant to include all link types
  68. */
  69. KILinkTypeOptionAll = NSUIntegerMax,
  70. };
  71. @class KILabel;
  72. /**
  73. * Type for block that is called when a link is tapped
  74. *
  75. * @param label The KILabel in which the tap took place
  76. * @param string Content of the link that was tapped, includes @ or # tokens
  77. * @param range The range of the string within the label's text
  78. */
  79. typedef void (^KILinkTapHandler)(KILabel *label, NSString *string, NSRange range);
  80. extern NSString * const KILabelLinkTypeKey;
  81. extern NSString * const KILabelRangeKey;
  82. extern NSString * const KILabelLinkKey;
  83. /**
  84. * A UILabel subclass that highlights links, hashtags and usernames and enables response to user
  85. * interactions with those links.
  86. **/
  87. IB_DESIGNABLE
  88. @interface KILabel : UILabel <NSLayoutManagerDelegate>
  89. /** ****************************************************************************************** **
  90. * @name Setting the link detector
  91. ** ****************************************************************************************** **/
  92. /**
  93. * Enable/disable automatic detection of links, hashtags and usernames.
  94. */
  95. @property (nonatomic, assign, getter = isAutomaticLinkDetectionEnabled) IBInspectable BOOL automaticLinkDetectionEnabled;
  96. /**
  97. * Specifies the combination of link types to detect. Default value is KILinkTypeAll.
  98. */
  99. @property (nonatomic, assign) IBInspectable KILinkTypeOption linkDetectionTypes;
  100. /**
  101. * Set containing words to be ignored as links, hashtags or usernames.
  102. *
  103. * @discussion The comparison between the matches and the ignored words is case insensitive.
  104. */
  105. @property (nullable, nonatomic, strong) NSSet *ignoredKeywords;
  106. /** ****************************************************************************************** **
  107. * @name Format & Appearance
  108. ** ****************************************************************************************** **/
  109. /**
  110. * The color used to highlight selected link background.
  111. *
  112. * @discussion The default value is (0.95, 0.95, 0.95, 1.0).
  113. */
  114. @property (nullable, nonatomic, copy) IBInspectable UIColor *selectedLinkBackgroundColor;
  115. /**
  116. * Flag sets if the sytem appearance for URLs should be used (underlined + blue color). Default value is NO.
  117. */
  118. @property (nonatomic, assign) IBInspectable BOOL systemURLStyle;
  119. /**
  120. * Get the current attributes for the given link type.
  121. *
  122. * @param linkType The link type to get the attributes.
  123. * @return A dictionary of text attributes.
  124. * @discussion Default attributes contain colored font using the tintColor color property.
  125. */
  126. - (nullable NSDictionary*)attributesForLinkType:(KILinkType)linkType;
  127. /**
  128. * Set the text attributes for each link type.
  129. *
  130. * @param attributes The text attributes.
  131. * @param linkType The link type.
  132. * @discussion Default attributes contain colored font using the tintColor color property.
  133. */
  134. - (void)setAttributes:(nullable NSDictionary*)attributes forLinkType:(KILinkType)linkType;
  135. /** ****************************************************************************************** **
  136. * @name Callbacks
  137. ** ****************************************************************************************** **/
  138. /**
  139. * Callback block for KILinkTypeUserHandle link tap.
  140. **/
  141. @property (nullable, nonatomic, copy) KILinkTapHandler userHandleLinkTapHandler;
  142. /**
  143. * Callback block for KILinkTypeHashtag link tap.
  144. */
  145. @property (nullable, nonatomic, copy) KILinkTapHandler hashtagLinkTapHandler;
  146. /**
  147. * Callback block for KILinkTypeURL link tap.
  148. */
  149. @property (nullable, nonatomic, copy) KILinkTapHandler urlLinkTapHandler;
  150. /** ****************************************************************************************** **
  151. * @name Geometry
  152. ** ****************************************************************************************** **/
  153. /**
  154. * Returns a dictionary of data about the link that it at the location. Returns nil if there is no link.
  155. *
  156. * A link dictionary contains the following keys:
  157. *
  158. * - **KILabelLinkTypeKey**, a TDLinkType that identifies the type of link.
  159. * - **KILabelRangeKey**, the range of the link within the label text.
  160. * - **KILabelLinkKey**, the link text. This could be an URL, handle or hashtag depending on the linkType value.
  161. *
  162. * @param point The point in the coordinates of the label view.
  163. * @return A dictionary containing the link.
  164. */
  165. - (nullable NSDictionary*)linkAtPoint:(CGPoint)point;
  166. @end
  167. NS_ASSUME_NONNULL_END