| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- //
- // CustomLabel.m
- // JasonDevelop
- //
- // Created by Jason Lee on 2013. 12. 10..
- // Copyright (c) jasondevelop. All rights reserved.
- //
- #import "NSString-Addtions.h"
- #import "UIView+AutoLayoutDebugging.h"
- #import "CustomLabel.h"
- #import "CommonUtil.h"
- #import "ImageUtil.h"
- #define kfBoxCap 4.0f
- #define ksEmptyString @""
- #define ksSpaceString @""
- @interface CustomLabel () {
- id _userInteractionTarget;
- SEL _userInteractionSelector;
- UIEdgeInsets _bgInsets;
- }
- @end
- @implementation CustomLabel
- //5.1 이전
- //애플고딕
- //5.1 이상
- //애플 산돌 고딕 네오체 (AppleSDGothicNeo)
- //Font Family: AppleGothic
- //Font: AppleGothic
- //iOS 7
- //Family name: Apple SD Gothic Neo
- //Font name: AppleSDGothicNeo-Thin
- //Font name: AppleSDGothicNeo-SemiBold
- //Font name: AppleSDGothicNeo-Medium
- //Font name: AppleSDGothicNeo-Regular
- //Font name: AppleSDGothicNeo-Bold
- //Font name: AppleSDGothicNeo-Light
- //iOS 6
- //Family name: Apple SD Gothic Neo
- //Font name: AppleSDGothicNeo-Bold
- //Font name: AppleSDGothicNeo-Medium
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- CGFloat fontSize = self.font.pointSize;
- UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
- UIFont *boldFont = [UIFont fontWithName:@"AppleSDGothicNeo-Bold" size:fontSize];
- if (IOS_VERSION < 6.0f) {
- normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
- boldFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
- }
- // self.lineBreakMode = NSLineBreakByCharWrapping;
- self.backgroundColor = [UIColor clearColor];
- }
- return self;
- }
- //NSString *const NSFontAttributeName;
- //NSString *const NSParagraphStyleAttributeName;
- //NSString *const NSForegroundColorAttributeName;
- //NSString *const NSBackgroundColorAttributeName;
- //NSString *const NSLigatureAttributeName;
- //NSString *const NSBaselineOffsetAttributeName;
- //NSString *const NSStrikethroughStyleAttributeName;
- //NSString *const NSStrokeColorAttributeName;
- //NSString *const NSStrokeWidthAttributeName;
- //NSString *const NSShadowAttributeName;
- - (void)awakeFromNib {
- CGFloat fontSize = self.font.pointSize;
- UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
- UIFont *boldFont = [UIFont fontWithName:@"AppleSDGothicNeo-Bold" size:fontSize];
- if (IOS_VERSION <= 6.0f) {
- normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
- boldFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
- }
- NSDictionary *attribute = @{NSForegroundColorAttributeName : self.textColor,
- NSFontAttributeName : normalFont,
- };
- NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:self.text attributes:attribute];
- self.attributedText = attributedText;
- // self.lineBreakMode = NSLineBreakByCharWrapping;
- if (_bgImageName && ![_bgImageName isEmptyString]) {
- if (!CGRectEqualToRect(_rectForCapBackground, CGRectZero)) {//rectForCap이 설정된 경우, 이미지를 리사이즈함.
- CGFloat top, left, bottom, right;
- top = CGRectGetMinY(_rectForCapBackground);
- left = CGRectGetMinX(_rectForCapBackground);
- bottom = CGRectGetHeight(_rectForCapBackground);
- right = CGRectGetWidth(_rectForCapBackground);
- _bgInsets = UIEdgeInsetsMake(top, left, bottom, right);
- } else {
- _bgInsets = UIEdgeInsetsMake(kfBoxCap, kfBoxCap, kfBoxCap, kfBoxCap);
- }
- UIImage *bgImage = [ImageUtil resizableImageWithCapInsets:_bgInsets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:_bgImageName]];
- self.backgroundColor = [UIColor colorWithPatternImage:bgImage];
- } else {
- self.backgroundColor = [UIColor clearColor];
- }
- #ifdef DEBUG
- [self setValue:self.text forKey:@"layoutDebuggingIdentifier"];
- #endif
- }
- - (void)setText:(NSString *)text {
- if (!text || [text isEqualToString:ksEmptyString]) {
- text = ksSpaceString;
- }
- text = [text stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"];
- #ifdef DEBUG_MODE
- // text = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@", text, text, text, text, text, text, text, text, text];
- #endif
- CGFloat fontSize = self.font.pointSize;
- UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
- UIFont *boldFont = [UIFont fontWithName:@"AppleSDGothicNeo-Bold" size:fontSize];
- if (IOS_VERSION <= 5.1f) {
- normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
- boldFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
- }
- NSDictionary *attribute = @{NSFontAttributeName : normalFont};
- NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attribute];
- self.attributedText = attributedText;
- }
- - (void)setBold:(NSString *)text {
- if (!text || [text isEmptyString])
- return;
- CGFloat fontSize = self.font.pointSize;
- UIFont *boldFont = [UIFont fontWithName:@"AppleSDGothicNeo-Bold" size:fontSize];
- if (IOS_VERSION <= 6.0f) {
- boldFont = [UIFont boldSystemFontOfSize:fontSize];
- }
- NSDictionary *attribute = @{NSFontAttributeName : boldFont};
- NSString *lblText = [self.attributedText string];
- NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
- //변경될 텍스트의 위치를 찾음
- NSUInteger index = [lblText rangeOfString:text].location;
- NSRange range = NSMakeRange(index, text.length);
- if (range.location != NSNotFound) {
- [attrText addAttributes:attribute range:range];
- self.attributedText = attrText;
- }
- }
- - (void)setFontSize:(NSString *)text fontSize:(CGFloat)fontSize {
- if (!text || [text isEmptyString])
- return;
- UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
- if (IOS_VERSION <= 6.0f) {
- normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
- }
- NSDictionary *attribute = @{NSFontAttributeName : normalFont};
- NSString *lblText = [self.attributedText string];
- NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
- //변경될 텍스트의 위치를 찾음
- NSUInteger index = [lblText rangeOfString:text].location;
- NSRange range = NSMakeRange(index, text.length);
- if (range.location != NSNotFound) {
- [attrText addAttributes:attribute range:range];
- self.attributedText = attrText;
- }
- }
- - (void)setRedColor:(NSString *)text {
- [self setColor:[UIColor redColor] text:text];
- }
- - (void)setBlueColor:(NSString *)text {
- [self setColor:[UIColor blueColor] text:text];
- }
- - (void)setColor:(UIColor *)color text:(NSString *)text {
- CGFloat fontSize = self.font.pointSize;
- UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
- if (IOS_VERSION <= 6.0f) {
- normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
- }
- NSDictionary *colorAttribute = @{NSForegroundColorAttributeName : color};
- NSString *lblText = [self.attributedText string];
- NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
- //변경될 텍스트의 위치를 찾음
- NSInteger index = [lblText rangeOfString:text].location;
- NSRange range = NSMakeRange(index, text.length);
- if (range.location != NSNotFound) {
- [attrText addAttributes:colorAttribute range:range];
- self.attributedText = attrText;
- }
- }
- - (void)setUnderLine:(NSString *)text {
- CGFloat fontSize = self.font.pointSize;
- UIFont *normalFont = [UIFont fontWithName:@"AppleSDGothicNeo-Medium" size:fontSize];
- if (IOS_VERSION <= 6.0f) {
- normalFont = [UIFont fontWithName:@"AppleGothic" size:fontSize];
- }
- NSDictionary *underLineAttribute = @{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
- NSString *lblText = [self.attributedText string];
- NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
- // [attrText addAttributes:@{NSUnderlineStyleAttributeName: } range:NSMakeRange (0, mat.length)];
- //변경될 텍스트의 위치를 찾음
- NSInteger index = [lblText rangeOfString:text].location;
- NSRange range = NSMakeRange(index, text.length);
- if (range.location != NSNotFound) {
- [attrText addAttributes:underLineAttribute range:range];
- self.attributedText = attrText;
- }
- }
- - (void)setKern:(NSInteger)kern text:(NSString *)text {
-
- NSDictionary *underLineAttribute = @{NSKernAttributeName : @(kern)};
-
- NSString *lblText = [self.attributedText string];
- NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
-
- //변경될 텍스트의 위치를 찾음
- NSInteger index = [lblText rangeOfString:text].location;
- NSRange range = NSMakeRange(index, text.length);
-
- if (range.location != NSNotFound) {
- [attrText addAttributes:underLineAttribute range:range];
- self.attributedText = attrText;
- }
- }
- - (void)setLineSpacing:(CGFloat)lineSpacing {
-
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- paragraphStyle.lineSpacing = lineSpacing;
- paragraphStyle.alignment = self.textAlignment;
- NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyle};
- NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:self.text
- attributes:attributes];
- self.attributedText = attributedText;
- }
- - (void)setLink:(NSString *)text{
- [self setColor:[UIColor blueColor] text:text];
- [self setUnderLine:text];
- }
- - (void)addTarget:(id)target action:(SEL)action {
- _userInteractionTarget = target;
- _userInteractionSelector = action;
- UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didLabelTouched:)];
- // if labelView is not set userInteractionEnabled, you must do so
- [self setUserInteractionEnabled:YES];
- [self addGestureRecognizer:gesture];
- }
- - (void)didLabelTouched:(UIGestureRecognizer*)gestureRecognizer {
- //api contidtion controls
- if (_userInteractionTarget && _userInteractionSelector) {
- NSMethodSignature *sig = [_userInteractionTarget methodSignatureForSelector:_userInteractionSelector];
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
- [invocation setTarget:_userInteractionTarget]; // index 0 (hidden)
- [invocation setSelector:_userInteractionSelector]; //index 1 (hidden)
- [invocation setArgument:(__bridge void *)(self) atIndex:2];
- [invocation invoke];
- }
- }
- - (void)addTouchEventHandler:(CustomLabelTouchHandler)handler {
- _touchHandler = handler;
- UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didLabelTouchedWithHandler:)];
- // if labelView is not set userInteractionEnabled, you must do so
- [self setUserInteractionEnabled:YES];
- [self addGestureRecognizer:gesture];
- }
- - (void)didLabelTouchedWithHandler:(UIGestureRecognizer *)gestureRecognizer {
- UIColor *_tmpColor = self.textColor;
- self.textColor = self.highlightedTextColor;
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- self.textColor = _tmpColor;
- _touchHandler(self);
- });
- }
- @end
- @implementation CustomTopLabel
- - (void)drawTextInRect:(CGRect)rect {
- if (self.text) {
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- paragraphStyle.lineBreakMode = self.lineBreakMode;
- CGSize labelStringSize = [self.attributedText boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame), CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
- [super drawTextInRect:CGRectMake(0, 0, CGRectGetWidth(self.frame),ceilf(labelStringSize.height))];
- } else {
- [super drawTextInRect:rect];
- }
- }
- @end
|