UILabel 添加点击事件、下划线、删除线、字体颜色

#import 

NS_ASSUME_NONNULL_BEGIN

/*
 特殊字符串model
 */
@interface LYAttributeModel : NSObject

//带点击事件的字符串
@property(nonatomic, copy)NSString *string;
//带点击事件字符串的range
@property(nonatomic, assign)NSRange range;

@end

@interface UILabel (LYTouchAction)
//点击效果,默认是打开
@property(nonatomic)BOOL enabledTapEffect;

//特殊字符串字体颜色
@property(nonatomic, strong)UIColor *attributeColor;

//特殊字符串下划线
@property(nonatomic)BOOL underLine;

//特殊字符串删除线
@property(nonatomic)BOOL throughLine;

/**
 *  给文本添加点击事件Block回调
 *
 *  @param strings  需要添加的字符串数组
 *  @param tapClick 点击事件回调
 */
-(void)addAttributeTapActionWithStrings:(NSArray  *)strings
                             tapClicked:(void (^) (NSString *string , NSRange range , NSInteger index))tapClick;

@end

NS_ASSUME_NONNULL_END
#import "UILabel+LYTouchAction.h"
#import 
#import 
#import 

/*
 特殊字符串model
 */
@implementation LYAttributeModel
@end

@implementation UILabel (LYTouchAction)

#pragma mark - AssociatedObjects ** <
-(NSMutableArray *)attributeStrings{
    return objc_getAssociatedObject(self, _cmd);
}

-(void)setAttributeStrings:(NSMutableArray *)attributeStrings{
    objc_setAssociatedObject(self, @selector(attributeStrings), attributeStrings, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSMutableDictionary *)effectDic{
    return objc_getAssociatedObject(self, _cmd);
}

-(void)setEffectDic:(NSMutableDictionary *)effectDic{
    objc_setAssociatedObject(self, @selector(effectDic), effectDic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

//点击手势
-(BOOL)isTapAction{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

-(void)setIsTapAction:(BOOL)isTapAction{
    objc_setAssociatedObject(self, @selector(isTapAction), @(isTapAction), OBJC_ASSOCIATION_ASSIGN);
}

//回调
-(void (^)(NSString *, NSRange, NSInteger))tapBlock{
    return objc_getAssociatedObject(self, _cmd);
}

-(void)setTapBlock:(void (^)(NSString *, NSRange, NSInteger))tapBlock{
    objc_setAssociatedObject(self, @selector(tapBlock), tapBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

//点击效果
-(BOOL)enabledTapEffect{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

-(void)setEnabledTapEffect:(BOOL)enabledTapEffect{
    objc_setAssociatedObject(self, @selector(enabledTapEffect), @(enabledTapEffect), OBJC_ASSOCIATION_ASSIGN);
    self.isTapEffect = enabledTapEffect;
}

-(BOOL)isTapEffect{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

-(void)setIsTapEffect:(BOOL)isTapEffect{
    objc_setAssociatedObject(self, @selector(isTapEffect), @(isTapEffect), OBJC_ASSOCIATION_ASSIGN);
}

//特殊字符串字体颜色
-(UIColor *)attributeColor{
    return objc_getAssociatedObject(self, _cmd);
}

-(void)setAttributeColor:(UIColor *)attributeColor{
    objc_setAssociatedObject(self, @selector(attributeColor), attributeColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

//下划线
-(BOOL)underLine{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

-(void)setUnderLine:(BOOL)underLine{
    objc_setAssociatedObject(self, @selector(underLine), @(underLine), OBJC_ASSOCIATION_ASSIGN);
}

//删除线
-(BOOL)throughLine{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

-(void)setThroughLine:(BOOL)throughLine{
    objc_setAssociatedObject(self, @selector(throughLine), @(throughLine), OBJC_ASSOCIATION_ASSIGN);
}
#pragma mark - AssociatedObjects ** >



#pragma mark - 设置特殊字符串并获取回调结果
-(void)addAttributeTapActionWithStrings:(NSArray  *)strings tapClicked:(void (^) (NSString *string , NSRange range , NSInteger index))tapClick{
    
    //获取特殊字符串的Range
    [self getRangesWithArrayStrings:strings];
    
    //设置block回调
    if (self.tapBlock != tapClick) {
        self.tapBlock = tapClick;
    }
    
}

#pragma mark - 为Label添加点击手势
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    //如果没有设置关键,则直接跳出,不添加手势
    if (!self.isTapAction) {
        return;
    }
    
    //获取点击效果是否开启
    if (objc_getAssociatedObject(self, @selector(enabledTapEffect))) {
        self.isTapEffect = self.enabledTapEffect;
    }
    
    //获取手势
    UITouch *touch = [touches anyObject];
    //在当前View获取点击位置
    CGPoint point = [touch locationInView:self];
    
    __weak typeof(self) weakSelf = self;
    
    //
    [self getTapFrameWithTouchPoint:point result:^(NSString *string, NSRange range, NSInteger index) {
        
        if (weakSelf.tapBlock) {
            weakSelf.tapBlock (string , range , index);
        }
        
        if (self.isTapEffect) {
            [self saveEffectDicWithRange:range];
            [self tapEffectWithStatus:YES];
        }
        
    }];
    
}

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    if (self.isTapAction) {
        if ([self getTapFrameWithTouchPoint:point result:nil]) {
            return self;
        }
    }
    return [super hitTest:point withEvent:event];
    
}

#pragma mark - getTapFrame
-(BOOL)getTapFrameWithTouchPoint:(CGPoint)point result:(void (^) (NSString *string , NSRange range , NSInteger index))resultBlock{
    
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.attributedText);
    CGMutablePathRef Path = CGPathCreateMutable();
    CGPathAddRect(Path, NULL, CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height));
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), Path, NULL);
    CFRange range = CTFrameGetVisibleStringRange(frame);
    
    if (self.attributedText.length > range.length) {
        
        UIFont *font;
        //获取字体大小
        if ([self.attributedText attribute:NSFontAttributeName atIndex:0 effectiveRange:nil]) {
            font = [self.attributedText attribute:NSFontAttributeName atIndex:0 effectiveRange:nil];
        }else if (self.font){
            font = self.font;
        }else {
            font = [UIFont systemFontOfSize:17];
        }
        
        CGPathRelease(Path);
        Path = CGPathCreateMutable();
        CGPathAddRect(Path, NULL, CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height + font.lineHeight));
        frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), Path, NULL);
        
    }
    
    CFArrayRef lines = CTFrameGetLines(frame);
    
    if (!lines) {
        CFRelease(frame);
        CFRelease(framesetter);
        CGPathRelease(Path);
        return NO;
    }
    
    CFIndex count = CFArrayGetCount(lines);
    CGPoint origins[count];
    CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), origins);
    CGAffineTransform transform = [self transformForCoreText];
    CGFloat verticalOffset = 0;
    
    for (CFIndex i = 0; i < count; i++) {
        
        CGPoint linePoint = origins[i];
        CTLineRef line = CFArrayGetValueAtIndex(lines, i);
        CGRect flippedRect = [self getLineBounds:line point:linePoint];
        CGRect rect = CGRectApplyAffineTransform(flippedRect, transform);
        rect = CGRectInset(rect, 0, 0);
        rect = CGRectOffset(rect, 0, verticalOffset);
        NSParagraphStyle *style = [self.attributedText attribute:NSParagraphStyleAttributeName atIndex:0 effectiveRange:nil];
        CGFloat lineSpace;
        
        if (style) {
            lineSpace = style.lineSpacing;
        }else {
            lineSpace = 0;
        }
        
        CGFloat lineOutSpace = (self.bounds.size.height - lineSpace * (count - 1) -rect.size.height * count) / 2;
        rect.origin.y = lineOutSpace + rect.size.height * i + lineSpace * i;
        
        if (CGRectContainsPoint(rect, point)) {
            
            CGPoint relativePoint = CGPointMake(point.x - CGRectGetMinX(rect), point.y - CGRectGetMinY(rect));
            CFIndex index = CTLineGetStringIndexForPosition(line, relativePoint);
            CGFloat offset;
            CTLineGetOffsetForStringIndex(line, index, &offset);
            
            if (offset > relativePoint.x) {
                index = index - 1;
            }
            
            NSInteger link_count = self.attributeStrings.count;
            
            for (int j = 0; j < link_count; j++) {
                
                LYAttributeModel *model = self.attributeStrings[j];
                
                NSRange link_range = model.range;
                
                if (NSLocationInRange(index, link_range)) {
                    if (resultBlock) {
                        resultBlock (model.string , model.range , (NSInteger)j);
                    }
                    CFRelease(frame);
                    CFRelease(framesetter);
                    CGPathRelease(Path);
                    return YES;
                }
                
            }
            
        }
        
    }
    CFRelease(frame);
    CFRelease(framesetter);
    CGPathRelease(Path);
    return NO;
    
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    if (self.isTapEffect) {
        [self performSelectorOnMainThread:@selector(tapEffectWithStatus:) withObject:nil waitUntilDone:NO];
    }
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    if (self.isTapEffect) {
        [self performSelectorOnMainThread:@selector(tapEffectWithStatus:) withObject:nil waitUntilDone:NO];
    }
}

-(CGAffineTransform)transformForCoreText{
    return CGAffineTransformScale(CGAffineTransformMakeTranslation(0, self.bounds.size.height), 1.f, -1.f);
}

-(CGRect)getLineBounds:(CTLineRef)line point:(CGPoint)point{
    
    CGFloat ascent = 0.0f;
    CGFloat descent = 0.0f;
    CGFloat leading = 0.0f;
    CGFloat width = (CGFloat)CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
    CGFloat height = ascent + fabs(descent) + leading;
    
    return CGRectMake(point.x, point.y , width, height);
    
}

#pragma mark - tapEffect
-(void)tapEffectWithStatus:(BOOL)status{
    
    if (self.isTapEffect) {
        
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
        NSMutableAttributedString *subAtt = [[NSMutableAttributedString alloc] initWithAttributedString:[[self.effectDic allValues] firstObject]];
        
        NSRange range = NSRangeFromString([[self.effectDic allKeys] firstObject]);
        
        //点击效果的背景颜色
        if (status) {
            [subAtt addAttribute:NSBackgroundColorAttributeName value:[UIColor groupTableViewBackgroundColor] range:NSMakeRange(0, subAtt.string.length)];
            [attStr replaceCharactersInRange:range withAttributedString:subAtt];
        }else {
            [attStr replaceCharactersInRange:range withAttributedString:subAtt];
        }
        self.attributedText = attStr;
        
    }
    
}

-(void)saveEffectDicWithRange:(NSRange)range{
    
    self.effectDic = [NSMutableDictionary dictionary];
    NSAttributedString *subAttribute = [self.attributedText attributedSubstringFromRange:range];
    [self.effectDic setObject:subAttribute forKey:NSStringFromRange(range)];
    
}

#pragma mark - 获取特殊字符串的Range
-(void)getRangesWithArrayStrings:(NSArray   *)arrayStrings{
    
    //容错处理
    if (self.attributedText == nil) {
        self.isTapAction = NO;
        return;
    }
    
    //添加手势
    self.isTapAction = YES;
    
    //添加点击效果
    self.isTapEffect = YES;
    
    //设置弱引用接收字符串
    __block NSString *totalStr = self.attributedText.string;
    
    //初始化可变数组存储特殊字符串
    self.attributeStrings = [NSMutableArray array];
    
    __weak typeof(self) weakSelf = self;
    
    //开始遍历整个字符串,获取到对应的特殊字符串和对应的range,并存到self.attributeStrings可变数组中
    [arrayStrings enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        //获取到对应特殊字符串的range
        NSRange range = [totalStr rangeOfString:obj];
        
        //特殊字符串存在,则继续处理
        if (range.length != 0) {
            
            //特殊字符串用空格替换掉
            totalStr = [totalStr stringByReplacingCharactersInRange:range withString:[weakSelf getStringWithRange:range]];
            
            //获取特殊字符串的位置和内容,并存储到可变数组中
            LYAttributeModel *model = [LYAttributeModel new];
            model.range = range;
            model.string = obj;
            [weakSelf.attributeStrings addObject:model];
            
        }
        
    }];
    
//    NSLog(@"throughLine = %@",self.throughLine ? @"yes" : @"no");
    //修改特殊字符串字体颜色
    if (self.attributeColor) {
        
        for (int i = 0; i < self.attributeStrings.count; i ++) {
            
            LYAttributeModel *model = self.attributeStrings[i];
            NSMutableAttributedString *mutAttr = [[NSMutableAttributedString alloc]initWithAttributedString:self.attributedText];
            [mutAttr addAttribute:NSForegroundColorAttributeName value:self.attributeColor range:model.range];
            
            //特殊字符串添加下划线
            if (self.underLine) {
                [mutAttr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:model.range];
            }
            
            //特殊字符串添加删除线
            if (self.throughLine) {
                [mutAttr addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),
                                         NSBaselineOffsetAttributeName:@(0)}
                                 range:model.range];
            }
            
            self.attributedText = mutAttr;
            
        }
        
    }
    
}

-(NSString *)getStringWithRange:(NSRange)range{
    
    NSMutableString *string = [NSMutableString string];
    for (int i = 0; i < range.length ; i++) {
        [string appendString:@" "];
    }
    return string;
    
}
@end

你可能感兴趣的:(UILabel 添加点击事件、下划线、删除线、字体颜色)