CustomLabel.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //
  2. // CustomLabel.m
  3. // JasonDevelop
  4. //
  5. // Created by Jason Lee on 2013. 12. 10..
  6. // Copyright (c) jasondevelop. All rights reserved.
  7. //
  8. #import "NSString-Addtions.h"
  9. #import "UIView+AutoLayoutDebugging.h"
  10. #import "CustomLabel.h"
  11. #import "CommonUtil.h"
  12. #define kfBoxCap 4.0f
  13. #define ksEmptyString @""
  14. #define ksSpaceString @""
  15. @interface CustomLabel () {
  16. id _userInteractionTarget;
  17. SEL _userInteractionSelector;
  18. UIEdgeInsets _bgInsets;
  19. }
  20. @end
  21. @implementation CustomLabel
  22. //5.1 이전
  23. //애플고딕
  24. //5.1 이상
  25. //애플 산돌 고딕 네오체 (AppleSDGothicNeo)
  26. //Font Family: AppleGothic
  27. //Font: AppleGothic
  28. //iOS 7
  29. //Family name: Apple SD Gothic Neo
  30. //Font name: AppleSDGothicNeo-Thin
  31. //Font name: AppleSDGothicNeo-SemiBold
  32. //Font name: AppleSDGothicNeo-Medium
  33. //Font name: AppleSDGothicNeo-Regular
  34. //Font name: AppleSDGothicNeo-Bold
  35. //Font name: AppleSDGothicNeo-Light
  36. //iOS 6
  37. //Family name: Apple SD Gothic Neo
  38. //Font name: AppleSDGothicNeo-Bold
  39. //Font name: AppleSDGothicNeo-Medium
  40. - (id)initWithFrame:(CGRect)frame
  41. {
  42. self = [super initWithFrame:frame];
  43. if (self) {
  44. // Initialization code
  45. CGFloat fontSize = self.font.pointSize;
  46. UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
  47. UIFont *boldFont = [UIFont fontWithName:@"AppleSDGothicNeo-Bold" size:fontSize];
  48. if (IOS_VERSION < 6.0f) {
  49. normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
  50. boldFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
  51. }
  52. // self.lineBreakMode = NSLineBreakByCharWrapping;
  53. self.backgroundColor = [UIColor clearColor];
  54. }
  55. return self;
  56. }
  57. //NSString *const NSFontAttributeName;
  58. //NSString *const NSParagraphStyleAttributeName;
  59. //NSString *const NSForegroundColorAttributeName;
  60. //NSString *const NSBackgroundColorAttributeName;
  61. //NSString *const NSLigatureAttributeName;
  62. //NSString *const NSBaselineOffsetAttributeName;
  63. //NSString *const NSStrikethroughStyleAttributeName;
  64. //NSString *const NSStrokeColorAttributeName;
  65. //NSString *const NSStrokeWidthAttributeName;
  66. //NSString *const NSShadowAttributeName;
  67. - (void)awakeFromNib {
  68. CGFloat fontSize = self.font.pointSize;
  69. UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
  70. UIFont *boldFont = [UIFont fontWithName:@"AppleSDGothicNeo-Bold" size:fontSize];
  71. if (IOS_VERSION <= 6.0f) {
  72. normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
  73. boldFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
  74. }
  75. NSDictionary *attribute = @{NSForegroundColorAttributeName : self.textColor,
  76. NSFontAttributeName : normalFont,
  77. };
  78. NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:self.text attributes:attribute];
  79. self.attributedText = attributedText;
  80. // self.lineBreakMode = NSLineBreakByCharWrapping;
  81. if (_bgImageName && ![_bgImageName isEmptyString]) {
  82. if (!CGRectEqualToRect(_rectForCapBackground, CGRectZero)) {//rectForCap이 설정된 경우, 이미지를 리사이즈함.
  83. CGFloat top, left, bottom, right;
  84. top = CGRectGetMinY(_rectForCapBackground);
  85. left = CGRectGetMinX(_rectForCapBackground);
  86. bottom = CGRectGetHeight(_rectForCapBackground);
  87. right = CGRectGetWidth(_rectForCapBackground);
  88. _bgInsets = UIEdgeInsetsMake(top, left, bottom, right);
  89. } else {
  90. _bgInsets = UIEdgeInsetsMake(kfBoxCap, kfBoxCap, kfBoxCap, kfBoxCap);
  91. }
  92. UIImage *bgImage = [CommonUtil resizableImageWithCapInsets:_bgInsets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:_bgImageName]];
  93. self.backgroundColor = [UIColor colorWithPatternImage:bgImage];
  94. } else {
  95. self.backgroundColor = [UIColor clearColor];
  96. }
  97. #ifdef DEBUG
  98. [self setValue:self.text forKey:@"layoutDebuggingIdentifier"];
  99. #endif
  100. }
  101. - (void)setText:(NSString *)text {
  102. if (!text || [text isEqualToString:ksEmptyString]) {
  103. text = ksSpaceString;
  104. }
  105. text = [text stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"];
  106. #ifdef DEBUG_MODE
  107. // text = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@", text, text, text, text, text, text, text, text, text];
  108. #endif
  109. CGFloat fontSize = self.font.pointSize;
  110. UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
  111. UIFont *boldFont = [UIFont fontWithName:@"AppleSDGothicNeo-Bold" size:fontSize];
  112. if (IOS_VERSION <= 5.1f) {
  113. normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
  114. boldFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
  115. }
  116. NSDictionary *attribute = @{NSFontAttributeName : normalFont};
  117. NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attribute];
  118. self.attributedText = attributedText;
  119. }
  120. - (void)setBold:(NSString *)text {
  121. if (!text || [text isEmptyString])
  122. return;
  123. CGFloat fontSize = self.font.pointSize;
  124. UIFont *boldFont = [UIFont fontWithName:@"AppleSDGothicNeo-Bold" size:fontSize];
  125. if (IOS_VERSION <= 6.0f) {
  126. boldFont = [UIFont boldSystemFontOfSize:fontSize];
  127. }
  128. NSDictionary *attribute = @{NSFontAttributeName : boldFont};
  129. NSString *lblText = [self.attributedText string];
  130. NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
  131. //변경될 텍스트의 위치를 찾음
  132. NSUInteger index = [lblText rangeOfString:text].location;
  133. NSRange range = NSMakeRange(index, text.length);
  134. if (range.location != NSNotFound) {
  135. [attrText addAttributes:attribute range:range];
  136. self.attributedText = attrText;
  137. }
  138. }
  139. - (void)setFontSize:(NSString *)text fontSize:(CGFloat)fontSize {
  140. if (!text || [text isEmptyString])
  141. return;
  142. UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
  143. if (IOS_VERSION <= 6.0f) {
  144. normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
  145. }
  146. NSDictionary *attribute = @{NSFontAttributeName : normalFont};
  147. NSString *lblText = [self.attributedText string];
  148. NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
  149. //변경될 텍스트의 위치를 찾음
  150. NSUInteger index = [lblText rangeOfString:text].location;
  151. NSRange range = NSMakeRange(index, text.length);
  152. if (range.location != NSNotFound) {
  153. [attrText addAttributes:attribute range:range];
  154. self.attributedText = attrText;
  155. }
  156. }
  157. - (void)setRedColor:(NSString *)text {
  158. [self setColor:[UIColor redColor] text:text];
  159. }
  160. - (void)setBlueColor:(NSString *)text {
  161. [self setColor:[UIColor blueColor] text:text];
  162. }
  163. - (void)setColor:(UIColor *)color text:(NSString *)text {
  164. CGFloat fontSize = self.font.pointSize;
  165. UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
  166. if (IOS_VERSION <= 6.0f) {
  167. normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
  168. }
  169. NSDictionary *colorAttribute = @{NSForegroundColorAttributeName : color};
  170. NSString *lblText = [self.attributedText string];
  171. NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
  172. //변경될 텍스트의 위치를 찾음
  173. NSInteger index = [lblText rangeOfString:text].location;
  174. NSRange range = NSMakeRange(index, text.length);
  175. if (range.location != NSNotFound) {
  176. [attrText addAttributes:colorAttribute range:range];
  177. self.attributedText = attrText;
  178. }
  179. }
  180. - (void)setUnderLine:(NSString *)text {
  181. CGFloat fontSize = self.font.pointSize;
  182. UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
  183. if (IOS_VERSION <= 6.0f) {
  184. normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
  185. }
  186. NSDictionary *underLineAttribute = @{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
  187. NSString *lblText = [self.attributedText string];
  188. NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
  189. // [attrText addAttributes:@{NSUnderlineStyleAttributeName: } range:NSMakeRange (0, mat.length)];
  190. //변경될 텍스트의 위치를 찾음
  191. NSInteger index = [lblText rangeOfString:text].location;
  192. NSRange range = NSMakeRange(index, text.length);
  193. if (range.location != NSNotFound) {
  194. [attrText addAttributes:underLineAttribute range:range];
  195. self.attributedText = attrText;
  196. }
  197. }
  198. - (void)setLink:(NSString *)text{
  199. [self setColor:[UIColor blueColor] text:text];
  200. [self setUnderLine:text];
  201. }
  202. - (void)addTarget:(id)target action:(SEL)action {
  203. _userInteractionTarget = target;
  204. _userInteractionSelector = action;
  205. UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didLabelTouched:)];
  206. // if labelView is not set userInteractionEnabled, you must do so
  207. [self setUserInteractionEnabled:YES];
  208. [self addGestureRecognizer:gesture];
  209. }
  210. - (void)didLabelTouched:(UIGestureRecognizer*)gestureRecognizer {
  211. //api contidtion controls
  212. if (_userInteractionTarget && _userInteractionSelector) {
  213. NSMethodSignature *sig = [_userInteractionTarget methodSignatureForSelector:_userInteractionSelector];
  214. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
  215. [invocation setTarget:_userInteractionTarget]; // index 0 (hidden)
  216. [invocation setSelector:_userInteractionSelector]; //index 1 (hidden)
  217. [invocation setArgument:(__bridge void *)(self) atIndex:2];
  218. [invocation invoke];
  219. }
  220. }
  221. - (void)addTouchEventHandler:(CustomLabelTouchHandler)handler {
  222. _touchHandler = handler;
  223. UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didLabelTouchedWithHandler:)];
  224. // if labelView is not set userInteractionEnabled, you must do so
  225. [self setUserInteractionEnabled:YES];
  226. [self addGestureRecognizer:gesture];
  227. }
  228. - (void)didLabelTouchedWithHandler:(UIGestureRecognizer *)gestureRecognizer {
  229. UIColor *_tmpColor = self.textColor;
  230. self.textColor = self.highlightedTextColor;
  231. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  232. self.textColor = _tmpColor;
  233. _touchHandler();
  234. });
  235. }
  236. @end
  237. @implementation CustomTopLabel
  238. - (void)drawTextInRect:(CGRect)rect {
  239. if (self.text) {
  240. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  241. paragraphStyle.lineBreakMode = self.lineBreakMode;
  242. CGSize labelStringSize = [self.attributedText boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame), CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
  243. [super drawTextInRect:CGRectMake(0, 0, CGRectGetWidth(self.frame),ceilf(labelStringSize.height))];
  244. } else {
  245. [super drawTextInRect:rect];
  246. }
  247. }
  248. @end