CustomLabel.m 13 KB

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