RTLabel.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // RTLabel.h
  3. // RTLabelProject
  4. //
  5. /**
  6. * Copyright (c) 2010 Muh Hon Cheng
  7. * Created by honcheng on 1/6/11.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining
  10. * a copy of this software and associated documentation files (the
  11. * "Software"), to deal in the Software without restriction, including
  12. * without limitation the rights to use, copy, modify, merge, publish,
  13. * distribute, sublicense, and/or sell copies of the Software, and to
  14. * permit persons to whom the Software is furnished to do so, subject
  15. * to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be
  18. * included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
  21. * WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  22. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR
  24. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  25. * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  28. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  29. * IN CONNECTION WITH THE SOFTWARE OR
  30. * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. *
  32. * @author Muh Hon Cheng <honcheng@gmail.com>
  33. * @copyright 2011 Muh Hon Cheng
  34. * @version
  35. *
  36. */
  37. #import <UIKit/UIKit.h>
  38. #import <CoreText/CoreText.h>
  39. typedef enum
  40. {
  41. RTTextAlignmentRight = kCTRightTextAlignment,
  42. RTTextAlignmentLeft = kCTLeftTextAlignment,
  43. RTTextAlignmentCenter = kCTCenterTextAlignment,
  44. RTTextAlignmentJustify = kCTJustifiedTextAlignment
  45. } RTTextAlignment;
  46. typedef enum
  47. {
  48. RTTextLineBreakModeWordWrapping = kCTLineBreakByWordWrapping,
  49. RTTextLineBreakModeCharWrapping = kCTLineBreakByCharWrapping,
  50. RTTextLineBreakModeClip = kCTLineBreakByClipping,
  51. }RTTextLineBreakMode;
  52. @protocol RTLabelDelegate <NSObject>
  53. @optional
  54. - (void)rtLabel:(id)rtLabel didSelectLinkWithURL:(NSURL*)url;
  55. @end
  56. @interface RTLabelComponent : NSObject
  57. @property (nonatomic, assign) long componentIndex;
  58. @property (nonatomic, copy) NSString *text;
  59. @property (nonatomic, copy) NSString *tagLabel;
  60. @property (nonatomic) NSMutableDictionary *attributes;
  61. @property (nonatomic, assign) long position;
  62. - (id)initWithString:(NSString*)aText tag:(NSString*)aTagLabel attributes:(NSMutableDictionary*)theAttributes;
  63. + (id)componentWithString:(NSString*)aText tag:(NSString*)aTagLabel attributes:(NSMutableDictionary*)theAttributes;
  64. - (id)initWithTag:(NSString*)aTagLabel position:(int)_position attributes:(NSMutableDictionary*)_attributes;
  65. + (id)componentWithTag:(NSString*)aTagLabel position:(int)aPosition attributes:(NSMutableDictionary*)theAttributes;
  66. @end
  67. @interface RTLabelExtractedComponent : NSObject
  68. @property (nonatomic, strong) NSMutableArray *textComponents;
  69. @property (nonatomic, copy) NSString *plainText;
  70. + (RTLabelExtractedComponent*)rtLabelExtractComponentsWithTextComponent:(NSMutableArray*)textComponents plainText:(NSString*)plainText;
  71. @end
  72. @interface RTLabel : UIView
  73. @property (nonatomic, copy) NSString *text, *plainText, *highlightedText;
  74. @property (nonatomic, strong) UIColor *textColor;
  75. @property (nonatomic, strong) UIFont *font;
  76. @property (nonatomic, strong) NSDictionary *linkAttributes;
  77. @property (nonatomic, strong) NSDictionary *selectedLinkAttributes;
  78. @property (nonatomic, unsafe_unretained) id<RTLabelDelegate> delegate;
  79. @property (nonatomic, copy) NSString *paragraphReplacement;
  80. @property (nonatomic, strong) NSMutableArray *textComponents, *highlightedTextComponents;
  81. @property (nonatomic, assign) RTTextAlignment textAlignment;
  82. @property (nonatomic, assign) CGSize optimumSize;
  83. @property (nonatomic, assign) RTTextLineBreakMode lineBreakMode;
  84. @property (nonatomic, assign) CGFloat lineSpacing;
  85. @property (nonatomic, assign) long currentSelectedButtonComponentIndex;
  86. @property (nonatomic, assign) CFRange visibleRange;
  87. @property (nonatomic, assign) BOOL highlighted;
  88. // set text
  89. - (void)setText:(NSString*)text;
  90. + (RTLabelExtractedComponent*)extractTextStyleFromText:(NSString*)data paragraphReplacement:(NSString*)paragraphReplacement;
  91. // get the visible text
  92. - (NSString*)visibleText;
  93. // alternative
  94. // from susieyy http://github.com/susieyy
  95. // The purpose of this code is to cache pre-create the textComponents, is to improve the performance when drawing the text.
  96. // This improvement is effective if the text is long.
  97. - (void)setText:(NSString *)text extractedTextComponent:(RTLabelExtractedComponent*)extractedComponent;
  98. - (void)setHighlightedText:(NSString *)text extractedTextComponent:(RTLabelExtractedComponent*)extractedComponent;
  99. - (void)setText:(NSString *)text extractedTextStyle:(NSDictionary*)extractTextStyle __attribute__((deprecated));
  100. + (NSDictionary*)preExtractTextStyle:(NSString*)data __attribute__((deprecated));
  101. @end