《省钱达人》与《猎豆优选》UI相同版。域名tbk

TTTAttributedLabel.h 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. // TTTAttributedLabel.h
  2. //
  3. // Copyright (c) 2011 Mattt Thompson (http://mattt.me)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import <UIKit/UIKit.h>
  23. #import <CoreText/CoreText.h>
  24. //! Project version number for TTTAttributedLabel.
  25. FOUNDATION_EXPORT double TTTAttributedLabelVersionNumber;
  26. //! Project version string for TTTAttributedLabel.
  27. FOUNDATION_EXPORT const unsigned char TTTAttributedLabelVersionString[];
  28. @class TTTAttributedLabelLink;
  29. /**
  30. Vertical alignment for text in a label whose bounds are larger than its text bounds
  31. */
  32. typedef NS_ENUM(NSInteger, TTTAttributedLabelVerticalAlignment) {
  33. TTTAttributedLabelVerticalAlignmentCenter = 0,
  34. TTTAttributedLabelVerticalAlignmentTop = 1,
  35. TTTAttributedLabelVerticalAlignmentBottom = 2,
  36. };
  37. /**
  38. Determines whether the text to which this attribute applies has a strikeout drawn through itself.
  39. */
  40. extern NSString * const kTTTStrikeOutAttributeName;
  41. /**
  42. The background fill color. Value must be a `CGColorRef`. Default value is `nil` (no fill).
  43. */
  44. extern NSString * const kTTTBackgroundFillColorAttributeName;
  45. /**
  46. The padding for the background fill. Value must be a `UIEdgeInsets`. Default value is `UIEdgeInsetsZero` (no padding).
  47. */
  48. extern NSString * const kTTTBackgroundFillPaddingAttributeName;
  49. /**
  50. The background stroke color. Value must be a `CGColorRef`. Default value is `nil` (no stroke).
  51. */
  52. extern NSString * const kTTTBackgroundStrokeColorAttributeName;
  53. /**
  54. The background stroke line width. Value must be an `NSNumber`. Default value is `1.0f`.
  55. */
  56. extern NSString * const kTTTBackgroundLineWidthAttributeName;
  57. /**
  58. The background corner radius. Value must be an `NSNumber`. Default value is `5.0f`.
  59. */
  60. extern NSString * const kTTTBackgroundCornerRadiusAttributeName;
  61. @protocol TTTAttributedLabelDelegate;
  62. // Override UILabel @property to accept both NSString and NSAttributedString
  63. @protocol TTTAttributedLabel <NSObject>
  64. @property (nonatomic, copy) IBInspectable id text;
  65. @end
  66. IB_DESIGNABLE
  67. /**
  68. `TTTAttributedLabel` is a drop-in replacement for `UILabel` that supports `NSAttributedString`, as well as automatically-detected and manually-added links to URLs, addresses, phone numbers, and dates.
  69. ## Differences Between `TTTAttributedLabel` and `UILabel`
  70. For the most part, `TTTAttributedLabel` behaves just like `UILabel`. The following are notable exceptions, in which `TTTAttributedLabel` may act differently:
  71. - `text` - This property now takes an `id` type argument, which can either be a kind of `NSString` or `NSAttributedString` (mutable or immutable in both cases)
  72. - `attributedText` - Do not set this property directly. Instead, pass an `NSAttributedString` to `text`.
  73. - `lineBreakMode` - This property displays only the first line when the value is `UILineBreakModeHeadTruncation`, `UILineBreakModeTailTruncation`, or `UILineBreakModeMiddleTruncation`
  74. - `adjustsFontsizeToFitWidth` - Supported in iOS 5 and greater, this property is effective for any value of `numberOfLines` greater than zero. In iOS 4, setting `numberOfLines` to a value greater than 1 with `adjustsFontSizeToFitWidth` set to `YES` may cause `sizeToFit` to execute indefinitely.
  75. - `baselineAdjustment` - This property has no affect.
  76. - `textAlignment` - This property does not support justified alignment.
  77. - `NSTextAttachment` - This string attribute is not supported.
  78. Any properties affecting text or paragraph styling, such as `firstLineIndent` will only apply when text is set with an `NSString`. If the text is set with an `NSAttributedString`, these properties will not apply.
  79. ### NSCoding
  80. `TTTAttributedLabel`, like `UILabel`, conforms to `NSCoding`. However, if the build target is set to less than iOS 6.0, `linkAttributes` and `activeLinkAttributes` will not be encoded or decoded. This is due to an runtime exception thrown when attempting to copy non-object CoreText values in dictionaries.
  81. @warning Any properties changed on the label after setting the text will not be reflected until a subsequent call to `setText:` or `setText:afterInheritingLabelAttributesAndConfiguringWithBlock:`. 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.
  82. @bug Setting `attributedText` directly is not recommended, as it may cause a crash when attempting to access any links previously set. Instead, call `setText:`, passing an `NSAttributedString`.
  83. */
  84. @interface TTTAttributedLabel : UILabel <TTTAttributedLabel, UIGestureRecognizerDelegate>
  85. /**
  86. * The designated initializers are @c initWithFrame: and @c initWithCoder:.
  87. * init will not properly initialize many required properties and other configuration.
  88. */
  89. - (instancetype) init NS_UNAVAILABLE;
  90. ///-----------------------------
  91. /// @name Accessing the Delegate
  92. ///-----------------------------
  93. /**
  94. The receiver's delegate.
  95. @discussion A `TTTAttributedLabel` 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.
  96. */
  97. @property (nonatomic, unsafe_unretained) IBOutlet id <TTTAttributedLabelDelegate> delegate;
  98. ///--------------------------------------------
  99. /// @name Detecting, Accessing, & Styling Links
  100. ///--------------------------------------------
  101. /**
  102. @deprecated Use `enabledTextCheckingTypes` property instead.
  103. */
  104. @property (nonatomic, assign) NSTextCheckingTypes dataDetectorTypes DEPRECATED_ATTRIBUTE;
  105. /**
  106. A bitmask of `NSTextCheckingType` which are used to automatically detect links in the label text.
  107. @warning You must specify `enabledTextCheckingTypes` before setting the `text`, with either `setText:` or `setText:afterInheritingLabelAttributesAndConfiguringWithBlock:`.
  108. */
  109. @property (nonatomic, assign) NSTextCheckingTypes enabledTextCheckingTypes;
  110. /**
  111. An array of `NSTextCheckingResult` objects for links detected or manually added to the label text.
  112. */
  113. @property (readonly, nonatomic, strong) NSArray *links;
  114. /**
  115. A dictionary containing the default `NSAttributedString` attributes to be applied to links detected or manually added to the label text. The default link style is blue and underlined.
  116. @warning You must specify `linkAttributes` before setting autodecting or manually-adding links for these attributes to be applied.
  117. */
  118. @property (nonatomic, strong) NSDictionary *linkAttributes;
  119. /**
  120. A dictionary containing the default `NSAttributedString` attributes to be applied to links when they are in the active state. If `nil` or an empty `NSDictionary`, active links will not be styled. The default active link style is red and underlined.
  121. */
  122. @property (nonatomic, strong) NSDictionary *activeLinkAttributes;
  123. /**
  124. A dictionary containing the default `NSAttributedString` attributes to be applied to links when they are in the inactive state, which is triggered by a change in `tintColor` in iOS 7 and later. If `nil` or an empty `NSDictionary`, inactive links will not be styled. The default inactive link style is gray and unadorned.
  125. */
  126. @property (nonatomic, strong) NSDictionary *inactiveLinkAttributes;
  127. /**
  128. The edge inset for the background of a link. The default value is `{0, -1, 0, -1}`.
  129. */
  130. @property (nonatomic, assign) UIEdgeInsets linkBackgroundEdgeInset;
  131. /**
  132. Indicates if links will be detected within an extended area around the touch
  133. to emulate the link detection behaviour of UIWebView.
  134. Default value is YES. Disable to to improve performance on long labels.
  135. */
  136. @property (nonatomic, assign) BOOL extendsLinkTouchArea;
  137. ///---------------------------------------
  138. /// @name Acccessing Text Style Attributes
  139. ///---------------------------------------
  140. /**
  141. 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.
  142. */
  143. @property (nonatomic, assign) IBInspectable CGFloat shadowRadius;
  144. /**
  145. 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.
  146. */
  147. @property (nonatomic, assign) IBInspectable CGFloat highlightedShadowRadius;
  148. /**
  149. 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}.
  150. */
  151. @property (nonatomic, assign) IBInspectable CGSize highlightedShadowOffset;
  152. /**
  153. The shadow color for the label when the label's `highlighted` property is `YES`. The default value is `nil` (no shadow color).
  154. */
  155. @property (nonatomic, strong) IBInspectable UIColor *highlightedShadowColor;
  156. /**
  157. The amount to kern the next character. Default is standard kerning. If this attribute is set to 0.0, no kerning is done at all.
  158. */
  159. @property (nonatomic, assign) IBInspectable CGFloat kern;
  160. ///--------------------------------------------
  161. /// @name Acccessing Paragraph Style Attributes
  162. ///--------------------------------------------
  163. /**
  164. 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.
  165. */
  166. @property (nonatomic, assign) IBInspectable CGFloat firstLineIndent;
  167. /**
  168. @deprecated Use `lineSpacing` instead.
  169. */
  170. @property (nonatomic, assign) IBInspectable CGFloat leading DEPRECATED_ATTRIBUTE;
  171. /**
  172. The space in points added between lines within the paragraph. This value is always nonnegative and is 0.0 by default.
  173. */
  174. @property (nonatomic, assign) IBInspectable CGFloat lineSpacing;
  175. /**
  176. The minimum line height within the paragraph. If the value is 0.0, the minimum line height is set to the line height of the `font`. 0.0 by default.
  177. */
  178. @property (nonatomic, assign) IBInspectable CGFloat minimumLineHeight;
  179. /**
  180. The maximum line height within the paragraph. If the value is 0.0, the maximum line height is set to the line height of the `font`. 0.0 by default.
  181. */
  182. @property (nonatomic, assign) IBInspectable CGFloat maximumLineHeight;
  183. /**
  184. The line height multiple. This value is 1.0 by default.
  185. */
  186. @property (nonatomic, assign) IBInspectable CGFloat lineHeightMultiple;
  187. /**
  188. The distance, in points, from the margin to the text container. This value is `UIEdgeInsetsZero` by default.
  189. @discussion The `UIEdgeInset` members correspond to paragraph style properties rather than a particular geometry, and can change depending on the writing direction.
  190. ## `UIEdgeInset` Member Correspondence With `CTParagraphStyleSpecifier` Values:
  191. - `top`: `kCTParagraphStyleSpecifierParagraphSpacingBefore`
  192. - `left`: `kCTParagraphStyleSpecifierHeadIndent`
  193. - `bottom`: `kCTParagraphStyleSpecifierParagraphSpacing`
  194. - `right`: `kCTParagraphStyleSpecifierTailIndent`
  195. */
  196. @property (nonatomic, assign) IBInspectable UIEdgeInsets textInsets;
  197. /**
  198. The vertical text alignment for the label, for when the frame size is greater than the text rect size. The vertical alignment is `TTTAttributedLabelVerticalAlignmentCenter` by default.
  199. */
  200. @property (nonatomic, assign) TTTAttributedLabelVerticalAlignment verticalAlignment;
  201. ///--------------------------------------------
  202. /// @name Accessing Truncation Token Appearance
  203. ///--------------------------------------------
  204. /**
  205. @deprecated Use `attributedTruncationToken` instead.
  206. */
  207. @property (nonatomic, strong) NSString *truncationTokenString DEPRECATED_ATTRIBUTE;
  208. /**
  209. @deprecated Use `attributedTruncationToken` instead.
  210. */
  211. @property (nonatomic, strong) NSDictionary *truncationTokenStringAttributes DEPRECATED_ATTRIBUTE;
  212. /**
  213. The attributed string to apply to the truncation token at the end of a truncated line. Overrides `truncationTokenStringAttributes` and `truncationTokenString`. If unspecified, attributes will fallback to `truncationTokenStringAttributes` and `truncationTokenString`.
  214. */
  215. @property (nonatomic, strong) IBInspectable NSAttributedString *attributedTruncationToken;
  216. ///--------------------------
  217. /// @name Long press gestures
  218. ///--------------------------
  219. /**
  220. * The long-press gesture recognizer used internally by the label.
  221. */
  222. @property (nonatomic, strong, readonly) UILongPressGestureRecognizer *longPressGestureRecognizer;
  223. ///--------------------------------------------
  224. /// @name Calculating Size of Attributed String
  225. ///--------------------------------------------
  226. /**
  227. Calculate and return the size that best fits an attributed string, given the specified constraints on size and number of lines.
  228. @param attributedString The attributed string.
  229. @param size The maximum dimensions used to calculate size.
  230. @param numberOfLines The maximum number of lines in the text to draw, if the constraining size cannot accomodate the full attributed string.
  231. @return The size that fits the attributed string within the specified constraints.
  232. */
  233. + (CGSize)sizeThatFitsAttributedString:(NSAttributedString *)attributedString
  234. withConstraints:(CGSize)size
  235. limitedToNumberOfLines:(NSUInteger)numberOfLines;
  236. ///----------------------------------
  237. /// @name Setting the Text Attributes
  238. ///----------------------------------
  239. /**
  240. Sets the text displayed by the label.
  241. @param text An `NSString` or `NSAttributedString` object to be displayed by the label. If the specified text is an `NSString`, the label will display the text like a `UILabel`, inheriting the text styles of the label. If the specified text is an `NSAttributedString`, the label text styles will be overridden by the styles specified in the attributed string.
  242. @discussion This method overrides `UILabel -setText:` to accept both `NSString` and `NSAttributedString` objects. This string is `nil` by default.
  243. */
  244. - (void)setText:(id)text;
  245. /**
  246. Sets the text displayed by the label, after configuring an attributed string containing the text attributes inherited from the label in a block.
  247. @param text An `NSString` or `NSAttributedString` object to be displayed by the label.
  248. @param block A block object that returns an `NSMutableAttributedString` object and takes a single argument, which is an `NSMutableAttributedString` object with the text from the first parameter, and the text attributes inherited from the label text styles. For example, if you specified the `font` of the label to be `[UIFont boldSystemFontOfSize:14]` and `textColor` to be `[UIColor redColor]`, the `NSAttributedString` argument of the block would be contain the `NSAttributedString` attribute equivalents of those properties. In this block, you can set further attributes on particular ranges.
  249. @discussion This string is `nil` by default.
  250. */
  251. - (void)setText:(id)text
  252. afterInheritingLabelAttributesAndConfiguringWithBlock:(NSMutableAttributedString *(^)(NSMutableAttributedString *mutableAttributedString))block;
  253. ///------------------------------------
  254. /// @name Accessing the Text Attributes
  255. ///------------------------------------
  256. /**
  257. A copy of the label's current attributedText. This returns `nil` if an attributed string has never been set on the label.
  258. @warning Do not set this property directly. Instead, set @c text to an @c NSAttributedString.
  259. */
  260. @property (readwrite, nonatomic, copy) NSAttributedString *attributedText;
  261. ///-------------------
  262. /// @name Adding Links
  263. ///-------------------
  264. /**
  265. Adds a link. You can customize an individual link's appearance and accessibility value by creating your own @c TTTAttributedLabelLink and passing it to this method. The other methods for adding links will use the label's default attributes.
  266. @warning Modifying the link's attribute dictionaries must be done before calling this method.
  267. @param link A @c TTTAttributedLabelLink object.
  268. */
  269. - (void)addLink:(TTTAttributedLabelLink *)link;
  270. /**
  271. Adds a link to an @c NSTextCheckingResult.
  272. @param result An @c NSTextCheckingResult representing the link's location and type.
  273. @return The newly added link object.
  274. */
  275. - (TTTAttributedLabelLink *)addLinkWithTextCheckingResult:(NSTextCheckingResult *)result;
  276. /**
  277. Adds a link to an @c NSTextCheckingResult.
  278. @param result An @c NSTextCheckingResult representing the link's location and type.
  279. @param attributes The attributes to be added to the text in the range of the specified link. If set, the label's @c activeAttributes and @c inactiveAttributes will be applied to the link. If `nil`, no attributes are added to the link.
  280. @return The newly added link object.
  281. */
  282. - (TTTAttributedLabelLink *)addLinkWithTextCheckingResult:(NSTextCheckingResult *)result
  283. attributes:(NSDictionary *)attributes;
  284. /**
  285. Adds a link to a URL for a specified range in the label text.
  286. @param url The url to be linked to
  287. @param range The range in the label text of the link. The range must not exceed the bounds of the receiver.
  288. @return The newly added link object.
  289. */
  290. - (TTTAttributedLabelLink *)addLinkToURL:(NSURL *)url
  291. withRange:(NSRange)range;
  292. /**
  293. Adds a link to an address for a specified range in the label text.
  294. @param addressComponents A dictionary of address components for the address to be linked to
  295. @param range The range in the label text of the link. The range must not exceed the bounds of the receiver.
  296. @discussion The address component dictionary keys are described in `NSTextCheckingResult`'s "Keys for Address Components."
  297. @return The newly added link object.
  298. */
  299. - (TTTAttributedLabelLink *)addLinkToAddress:(NSDictionary *)addressComponents
  300. withRange:(NSRange)range;
  301. /**
  302. Adds a link to a phone number for a specified range in the label text.
  303. @param phoneNumber The phone number to be linked to.
  304. @param range The range in the label text of the link. The range must not exceed the bounds of the receiver.
  305. @return The newly added link object.
  306. */
  307. - (TTTAttributedLabelLink *)addLinkToPhoneNumber:(NSString *)phoneNumber
  308. withRange:(NSRange)range;
  309. /**
  310. Adds a link to a date for a specified range in the label text.
  311. @param date The date to be linked to.
  312. @param range The range in the label text of the link. The range must not exceed the bounds of the receiver.
  313. @return The newly added link object.
  314. */
  315. - (TTTAttributedLabelLink *)addLinkToDate:(NSDate *)date
  316. withRange:(NSRange)range;
  317. /**
  318. Adds a link to a date with a particular time zone and duration for a specified range in the label text.
  319. @param date The date to be linked to.
  320. @param timeZone The time zone of the specified date.
  321. @param duration The duration, in seconds from the specified date.
  322. @param range The range in the label text of the link. The range must not exceed the bounds of the receiver.
  323. @return The newly added link object.
  324. */
  325. - (TTTAttributedLabelLink *)addLinkToDate:(NSDate *)date
  326. timeZone:(NSTimeZone *)timeZone
  327. duration:(NSTimeInterval)duration
  328. withRange:(NSRange)range;
  329. /**
  330. Adds a link to transit information for a specified range in the label text.
  331. @param components A dictionary containing the transit components. The currently supported keys are `NSTextCheckingAirlineKey` and `NSTextCheckingFlightKey`.
  332. @param range The range in the label text of the link. The range must not exceed the bounds of the receiver.
  333. @return The newly added link object.
  334. */
  335. - (TTTAttributedLabelLink *)addLinkToTransitInformation:(NSDictionary *)components
  336. withRange:(NSRange)range;
  337. /**
  338. Returns whether an @c NSTextCheckingResult is found at the give point.
  339. @discussion This can be used together with @c UITapGestureRecognizer to tap interactions with overlapping views.
  340. @param point The point inside the label.
  341. */
  342. - (BOOL)containslinkAtPoint:(CGPoint)point;
  343. @end
  344. /**
  345. The `TTTAttributedLabelDelegate` protocol defines the messages sent to an attributed label delegate when links are tapped. All of the methods of this protocol are optional.
  346. */
  347. @protocol TTTAttributedLabelDelegate <NSObject>
  348. ///-----------------------------------
  349. /// @name Responding to Link Selection
  350. ///-----------------------------------
  351. @optional
  352. /**
  353. Tells the delegate that the user did select a link to a URL.
  354. @param label The label whose link was selected.
  355. @param url The URL for the selected link.
  356. */
  357. - (void)attributedLabel:(TTTAttributedLabel *)label
  358. didSelectLinkWithURL:(NSURL *)url;
  359. /**
  360. Tells the delegate that the user did select a link to an address.
  361. @param label The label whose link was selected.
  362. @param addressComponents The components of the address for the selected link.
  363. */
  364. - (void)attributedLabel:(TTTAttributedLabel *)label
  365. didSelectLinkWithAddress:(NSDictionary *)addressComponents;
  366. /**
  367. Tells the delegate that the user did select a link to a phone number.
  368. @param label The label whose link was selected.
  369. @param phoneNumber The phone number for the selected link.
  370. */
  371. - (void)attributedLabel:(TTTAttributedLabel *)label
  372. didSelectLinkWithPhoneNumber:(NSString *)phoneNumber;
  373. /**
  374. Tells the delegate that the user did select a link to a date.
  375. @param label The label whose link was selected.
  376. @param date The datefor the selected link.
  377. */
  378. - (void)attributedLabel:(TTTAttributedLabel *)label
  379. didSelectLinkWithDate:(NSDate *)date;
  380. /**
  381. Tells the delegate that the user did select a link to a date with a time zone and duration.
  382. @param label The label whose link was selected.
  383. @param date The date for the selected link.
  384. @param timeZone The time zone of the date for the selected link.
  385. @param duration The duration, in seconds from the date for the selected link.
  386. */
  387. - (void)attributedLabel:(TTTAttributedLabel *)label
  388. didSelectLinkWithDate:(NSDate *)date
  389. timeZone:(NSTimeZone *)timeZone
  390. duration:(NSTimeInterval)duration;
  391. /**
  392. Tells the delegate that the user did select a link to transit information
  393. @param label The label whose link was selected.
  394. @param components A dictionary containing the transit components. The currently supported keys are `NSTextCheckingAirlineKey` and `NSTextCheckingFlightKey`.
  395. */
  396. - (void)attributedLabel:(TTTAttributedLabel *)label
  397. didSelectLinkWithTransitInformation:(NSDictionary *)components;
  398. /**
  399. Tells the delegate that the user did select a link to a text checking result.
  400. @discussion This method is called if no other delegate method was called, which can occur by either now implementing the method in `TTTAttributedLabelDelegate` corresponding to a particular link, or the link was added by passing an instance of a custom `NSTextCheckingResult` subclass into `-addLinkWithTextCheckingResult:`.
  401. @param label The label whose link was selected.
  402. @param result The custom text checking result.
  403. */
  404. - (void)attributedLabel:(TTTAttributedLabel *)label
  405. didSelectLinkWithTextCheckingResult:(NSTextCheckingResult *)result;
  406. ///---------------------------------
  407. /// @name Responding to Long Presses
  408. ///---------------------------------
  409. /**
  410. * Long-press delegate methods include the CGPoint tapped within the label's coordinate space.
  411. * This may be useful on iPad to present a popover from a specific origin point.
  412. */
  413. /**
  414. Tells the delegate that the user long-pressed a link to a URL.
  415. @param label The label whose link was long pressed.
  416. @param url The URL for the link.
  417. @param point the point pressed, in the label's coordinate space
  418. */
  419. - (void)attributedLabel:(TTTAttributedLabel *)label
  420. didLongPressLinkWithURL:(NSURL *)url
  421. atPoint:(CGPoint)point;
  422. /**
  423. Tells the delegate that the user long-pressed a link to an address.
  424. @param label The label whose link was long pressed.
  425. @param addressComponents The components of the address for the link.
  426. @param point the point pressed, in the label's coordinate space
  427. */
  428. - (void)attributedLabel:(TTTAttributedLabel *)label
  429. didLongPressLinkWithAddress:(NSDictionary *)addressComponents
  430. atPoint:(CGPoint)point;
  431. /**
  432. Tells the delegate that the user long-pressed a link to a phone number.
  433. @param label The label whose link was long pressed.
  434. @param phoneNumber The phone number for the link.
  435. @param point the point pressed, in the label's coordinate space
  436. */
  437. - (void)attributedLabel:(TTTAttributedLabel *)label
  438. didLongPressLinkWithPhoneNumber:(NSString *)phoneNumber
  439. atPoint:(CGPoint)point;
  440. /**
  441. Tells the delegate that the user long-pressed a link to a date.
  442. @param label The label whose link was long pressed.
  443. @param date The date for the selected link.
  444. @param point the point pressed, in the label's coordinate space
  445. */
  446. - (void)attributedLabel:(TTTAttributedLabel *)label
  447. didLongPressLinkWithDate:(NSDate *)date
  448. atPoint:(CGPoint)point;
  449. /**
  450. Tells the delegate that the user long-pressed a link to a date with a time zone and duration.
  451. @param label The label whose link was long pressed.
  452. @param date The date for the link.
  453. @param timeZone The time zone of the date for the link.
  454. @param duration The duration, in seconds from the date for the link.
  455. @param point the point pressed, in the label's coordinate space
  456. */
  457. - (void)attributedLabel:(TTTAttributedLabel *)label
  458. didLongPressLinkWithDate:(NSDate *)date
  459. timeZone:(NSTimeZone *)timeZone
  460. duration:(NSTimeInterval)duration
  461. atPoint:(CGPoint)point;
  462. /**
  463. Tells the delegate that the user long-pressed a link to transit information.
  464. @param label The label whose link was long pressed.
  465. @param components A dictionary containing the transit components. The currently supported keys are `NSTextCheckingAirlineKey` and `NSTextCheckingFlightKey`.
  466. @param point the point pressed, in the label's coordinate space
  467. */
  468. - (void)attributedLabel:(TTTAttributedLabel *)label
  469. didLongPressLinkWithTransitInformation:(NSDictionary *)components
  470. atPoint:(CGPoint)point;
  471. /**
  472. Tells the delegate that the user long-pressed a link to a text checking result.
  473. @discussion Similar to `-attributedLabel:didSelectLinkWithTextCheckingResult:`, this method is called if a link is long pressed and the delegate does not implement the method corresponding to this type of link.
  474. @param label The label whose link was long pressed.
  475. @param result The custom text checking result.
  476. @param point the point pressed, in the label's coordinate space
  477. */
  478. - (void)attributedLabel:(TTTAttributedLabel *)label
  479. didLongPressLinkWithTextCheckingResult:(NSTextCheckingResult *)result
  480. atPoint:(CGPoint)point;
  481. @end
  482. @interface TTTAttributedLabelLink : NSObject <NSCoding>
  483. typedef void (^TTTAttributedLabelLinkBlock) (TTTAttributedLabel *, TTTAttributedLabelLink *);
  484. /**
  485. An `NSTextCheckingResult` representing the link's location and type.
  486. */
  487. @property (readonly, nonatomic, strong) NSTextCheckingResult *result;
  488. /**
  489. A dictionary containing the @c NSAttributedString attributes to be applied to the link.
  490. */
  491. @property (readonly, nonatomic, copy) NSDictionary *attributes;
  492. /**
  493. A dictionary containing the @c NSAttributedString attributes to be applied to the link when it is in the active state.
  494. */
  495. @property (readonly, nonatomic, copy) NSDictionary *activeAttributes;
  496. /**
  497. A dictionary containing the @c NSAttributedString attributes to be applied to the link when it is in the inactive state, which is triggered by a change in `tintColor` in iOS 7 and later.
  498. */
  499. @property (readonly, nonatomic, copy) NSDictionary *inactiveAttributes;
  500. /**
  501. Additional information about a link for VoiceOver users. Has default values if the link's @c result is @c NSTextCheckingTypeLink, @c NSTextCheckingTypePhoneNumber, or @c NSTextCheckingTypeDate.
  502. */
  503. @property (nonatomic, copy) NSString *accessibilityValue;
  504. /**
  505. A block called when this link is tapped.
  506. If non-nil, tapping on this link will call this block instead of the
  507. @c TTTAttributedLabelDelegate tap methods, which will not be called for this link.
  508. */
  509. @property (nonatomic, copy) TTTAttributedLabelLinkBlock linkTapBlock;
  510. /**
  511. A block called when this link is long-pressed.
  512. If non-nil, long pressing on this link will call this block instead of the
  513. @c TTTAttributedLabelDelegate long press methods, which will not be called for this link.
  514. */
  515. @property (nonatomic, copy) TTTAttributedLabelLinkBlock linkLongPressBlock;
  516. /**
  517. Initializes a link using the attribute dictionaries specified.
  518. @param attributes The @c attributes property for the link.
  519. @param activeAttributes The @c activeAttributes property for the link.
  520. @param inactiveAttributes The @c inactiveAttributes property for the link.
  521. @param result An @c NSTextCheckingResult representing the link's location and type.
  522. @return The initialized link object.
  523. */
  524. - (instancetype)initWithAttributes:(NSDictionary *)attributes
  525. activeAttributes:(NSDictionary *)activeAttributes
  526. inactiveAttributes:(NSDictionary *)inactiveAttributes
  527. textCheckingResult:(NSTextCheckingResult *)result;
  528. /**
  529. Initializes a link using the attribute dictionaries set on a specified label.
  530. @param label The attributed label from which to inherit attribute dictionaries.
  531. @param result An @c NSTextCheckingResult representing the link's location and type.
  532. @return The initialized link object.
  533. */
  534. - (instancetype)initWithAttributesFromLabel:(TTTAttributedLabel*)label
  535. textCheckingResult:(NSTextCheckingResult *)result;
  536. @end