CustomLabel.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. // TODO: 이부분에서 폰트명에 따라서 처리해야 함
  95. // if ([self.font.fontName isEqualToString:[[UIFont systemFontOfSize:fontSize] fontName]])
  96. // {
  97. //
  98. // NSString *fontName = [[UIFont systemFontOfSize:fontSize] fontName];
  99. // NSLog(@"Font Name : %@", fontName);
  100. // fontName = [fontName substringToIndex:[fontName rangeOfString:@"-"].location];
  101. // font = [UIFont fontWithName:[NSString stringWithFormat:@"%@-Regular", fontName] size:fontSize];
  102. // }
  103. _isBold = [[self.font.fontName lowercaseString] rangeOfString:@"bold"].location != NSNotFound;
  104. return _isBold ? [self boldFont] : font;
  105. }
  106. - (UIFont *)boldFont {
  107. UIFont *font = self.font;
  108. CGFloat fontSize = self.font.pointSize;
  109. if ([self.font.fontName isEqualToString:[[UIFont systemFontOfSize:fontSize] fontName]]) {
  110. NSString *fontName = [[UIFont systemFontOfSize:fontSize] fontName];
  111. fontName = [fontName substringToIndex:[fontName rangeOfString:@"-"].location];
  112. font = [UIFont fontWithName:[NSString stringWithFormat:@"%@-Bold", fontName] size:fontSize];
  113. } else {
  114. NSString *fontName = font.fontName;
  115. fontName = [fontName substringToIndex:[fontName rangeOfString:@"-"].location];
  116. font = [UIFont fontWithName:[NSString stringWithFormat:@"%@-Bold", fontName] size:fontSize];
  117. font = font ? font : self.font;
  118. }
  119. return font;
  120. }
  121. - (void)setText:(NSString *)text {
  122. if (!text || [text isEqualToString:ksEmptyString]) {
  123. text = ksSpaceString;
  124. }
  125. text = [text stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"];
  126. #ifdef DEBUG_MODE
  127. // text = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@", text, text, text, text, text, text, text, text, text];
  128. #endif
  129. NSDictionary *attribute = @{NSFontAttributeName : [self getFont]};
  130. NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attribute];
  131. self.attributedText = attributedText;
  132. }
  133. - (void)setBold:(NSString *)text {
  134. if (!text || [text isEmptyString])
  135. return;
  136. NSDictionary *attribute = @{NSFontAttributeName : [self boldFont]};
  137. NSString *lblText = [self.attributedText string];
  138. NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
  139. //변경될 텍스트의 위치를 찾음
  140. NSUInteger index = [lblText rangeOfString:text].location;
  141. NSRange range = NSMakeRange(index, text.length);
  142. if (range.location != NSNotFound) {
  143. [attrText addAttributes:attribute range:range];
  144. self.attributedText = attrText;
  145. }
  146. }
  147. - (void)setFontSize:(CGFloat)fontSize text:(NSString *)text {
  148. if (!text || [text isEmptyString])
  149. return;
  150. UIFont *font = [UIFont systemFontOfSize:fontSize];
  151. NSDictionary *attribute = @{NSFontAttributeName : font};
  152. NSString *lblText = [self.attributedText string];
  153. NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
  154. //변경될 텍스트의 위치를 찾음
  155. NSUInteger index = [lblText rangeOfString:text].location;
  156. NSRange range = NSMakeRange(index, text.length);
  157. if (range.location != NSNotFound) {
  158. [attrText addAttributes:attribute range:range];
  159. self.attributedText = attrText;
  160. }
  161. }
  162. - (void)setRedColor:(NSString *)text {
  163. [self setColor:[UIColor redColor] text:text];
  164. }
  165. - (void)setBlueColor:(NSString *)text {
  166. [self setColor:[UIColor blueColor] text:text];
  167. }
  168. - (void)setColor:(UIColor *)color text:(NSString *)text {
  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. NSDictionary *underLineAttribute = @{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
  182. NSString *lblText = [self.attributedText string];
  183. NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
  184. // [attrText addAttributes:@{NSUnderlineStyleAttributeName: } range:NSMakeRange (0, mat.length)];
  185. //변경될 텍스트의 위치를 찾음
  186. NSInteger index = [lblText rangeOfString:text].location;
  187. NSRange range = NSMakeRange(index, text.length);
  188. if (range.location != NSNotFound) {
  189. [attrText addAttributes:underLineAttribute range:range];
  190. self.attributedText = attrText;
  191. }
  192. }
  193. - (void)setStrikethrough:(NSString *)text {
  194. NSDictionary *attrStrike = @{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle)};
  195. NSString *lblText = [self.attributedText string];
  196. NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
  197. //변경될 텍스트의 위치를 찾음
  198. NSInteger index = [lblText rangeOfString:text].location;
  199. NSRange range = NSMakeRange(index, text.length);
  200. if (range.location != NSNotFound) {
  201. [attrText addAttributes:attrStrike range:range];
  202. self.attributedText = attrText;
  203. }
  204. }
  205. - (void)setKern:(NSInteger)kern text:(NSString *)text {
  206. NSDictionary *underLineAttribute = @{NSKernAttributeName : @(kern)};
  207. NSString *lblText = [self.attributedText string];
  208. NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
  209. //변경될 텍스트의 위치를 찾음
  210. NSInteger index = [lblText rangeOfString:text].location;
  211. NSRange range = NSMakeRange(index, text.length);
  212. if (range.location != NSNotFound) {
  213. [attrText addAttributes:underLineAttribute range:range];
  214. self.attributedText = attrText;
  215. }
  216. }
  217. - (void)setLineSpacing:(CGFloat)lineSpacing {
  218. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  219. paragraphStyle.lineSpacing = lineSpacing;
  220. paragraphStyle.alignment = self.textAlignment;
  221. NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyle};
  222. NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:self.text
  223. attributes:attributes];
  224. self.attributedText = attributedText;
  225. }
  226. - (void)setLink:(NSString *)text{
  227. [self setColor:[UIColor blueColor] text:text];
  228. [self setUnderLine:text];
  229. }
  230. - (void)addTarget:(id)target action:(SEL)action {
  231. _userInteractionTarget = target;
  232. _userInteractionSelector = action;
  233. UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didLabelTouched:)];
  234. // if labelView is not set userInteractionEnabled, you must do so
  235. [self setUserInteractionEnabled:YES];
  236. [self addGestureRecognizer:gesture];
  237. }
  238. - (void)didLabelTouched:(UIGestureRecognizer*)gestureRecognizer {
  239. //api contidtion controls
  240. if (_userInteractionTarget && _userInteractionSelector) {
  241. NSMethodSignature *sig = [_userInteractionTarget methodSignatureForSelector:_userInteractionSelector];
  242. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
  243. [invocation setTarget:_userInteractionTarget]; // index 0 (hidden)
  244. [invocation setSelector:_userInteractionSelector]; //index 1 (hidden)
  245. [invocation setArgument:(__bridge void *)(self) atIndex:2];
  246. [invocation invoke];
  247. }
  248. }
  249. - (void)addTouchEventHandler:(CustomLabelTouchHandler)handler {
  250. _touchHandler = handler;
  251. UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didLabelTouchedWithHandler:)];
  252. // if labelView is not set userInteractionEnabled, you must do so
  253. [self setUserInteractionEnabled:YES];
  254. [self addGestureRecognizer:gesture];
  255. }
  256. - (void)didLabelTouchedWithHandler:(UIGestureRecognizer *)gestureRecognizer {
  257. UIColor *_tmpColor = self.textColor;
  258. self.textColor = self.highlightedTextColor;
  259. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  260. self.textColor = _tmpColor;
  261. if (_touchHandler) {
  262. _touchHandler(self);
  263. }
  264. });
  265. }
  266. @end
  267. @implementation CustomTopLabel
  268. - (void)drawTextInRect:(CGRect)rect {
  269. if (self.text) {
  270. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  271. paragraphStyle.lineBreakMode = self.lineBreakMode;
  272. CGSize labelStringSize = [self.attributedText boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame), CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
  273. [super drawTextInRect:CGRectMake(0, 0, CGRectGetWidth(self.frame),ceilf(labelStringSize.height))];
  274. } else {
  275. [super drawTextInRect:rect];
  276. }
  277. }
  278. @end