CustomLabel.m 13 KB

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