CustomLabel.m 12 KB

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