源代碼:https://github.com/lyb5834/YBAttributeTextTapAction地址html
若是想用富文本文件,能夠參考的另一篇博客;git
http://www.javashuo.com/article/p-wdukvoch-gx.htmlgithub
若是隻須要作點擊,只須要使用一個類別文件就能夠數組
#import <UIKit/UIKit.h> @protocol RichTextDelegate <NSObject> @optional /** * RichTextDelegate * * @param string 點擊的字符串 * @param range 點擊的字符串range * @param index 點擊的字符在數組中的index */ - (void)didClickRichText:(NSString *)string range:(NSRange)range index:(NSInteger)index; @end @interface UILabel (RichText) /** * 是否打開點擊效果,默認是打開 */ @property (nonatomic, assign) BOOL enabledClickEffect; /** * 點擊效果顏色 默認lightGrayColor */ @property (nonatomic, strong) UIColor *clickEffectColor; /** * 給文本添加Block點擊事件回調 * * @param strings 須要添加的字符串數組 * @param clickAction 點擊事件回調 */ - (void)clickRichTextWithStrings:(NSArray <NSString *> *)strings clickAction:(void (^) (NSString *string, NSRange range, NSInteger index))clickAction; /** * 給文本添加點擊事件delegate回調 * * @param strings 須要添加的字符串數組 * @param delegate 富文本代理 */ - (void)clickRichTextWithStrings:(NSArray <NSString *> *)strings delegate:(id <RichTextDelegate> )delegate;
#import "UILabel+RichText.h" #import <objc/runtime.h> #import <CoreText/CoreText.h> #import <Foundation/Foundation.h> #define weakSelf(type) __weak typeof(type) weak##type = type; @interface RichTextModel : NSObject @property (nonatomic, copy) NSString *str; @property (nonatomic, assign) NSRange range; @end @implementation RichTextModel @end @implementation UILabel (RichText) #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)isClickAction { return [objc_getAssociatedObject(self, _cmd) boolValue]; } - (void)setIsClickAction:(BOOL)isClickAction { objc_setAssociatedObject(self, @selector(isClickAction), @(isClickAction), OBJC_ASSOCIATION_ASSIGN); } - (void (^)(NSString *, NSRange, NSInteger))clickBlock { return objc_getAssociatedObject(self, _cmd); } - (void)setClickBlock:(void (^)(NSString *, NSRange, NSInteger))clickBlock { objc_setAssociatedObject(self, @selector(clickBlock), clickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC); } - (id<RichTextDelegate>)delegate { return objc_getAssociatedObject(self, _cmd); } - (BOOL)enabledClickEffect { return [objc_getAssociatedObject(self, _cmd) boolValue]; } - (void)setEnabledClickEffect:(BOOL)enabledClickEffect { objc_setAssociatedObject(self, @selector(enabledClickEffect), @(enabledClickEffect), OBJC_ASSOCIATION_ASSIGN); // self.isClickEffect = enabledClickEffect; } - (UIColor *)clickEffectColor { UIColor *obj = objc_getAssociatedObject(self, _cmd); if(obj == nil) {obj = [UIColor lightGrayColor];} return obj; } - (void)setClickEffectColor:(UIColor *)clickEffectColor { objc_setAssociatedObject(self, @selector(clickEffectColor), clickEffectColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (BOOL)isClickEffect { return [objc_getAssociatedObject(self, _cmd) boolValue]; } - (void)setIsClickEffect:(BOOL)isClickEffect { objc_setAssociatedObject(self, @selector(isClickEffect), @(isClickEffect), OBJC_ASSOCIATION_ASSIGN); } - (void)setDelegate:(id<RichTextDelegate>)delegate { objc_setAssociatedObject(self, @selector(delegate), delegate, OBJC_ASSOCIATION_ASSIGN); } #pragma mark - mainFunction - (void)clickRichTextWithStrings:(NSArray<NSString *> *)strings clickAction:(void (^)(NSString *, NSRange, NSInteger))clickAction { [self richTextRangesWithStrings:strings]; if (self.clickBlock != clickAction) { self.clickBlock = clickAction; } } - (void)clickRichTextWithStrings:(NSArray<NSString *> *)strings delegate:(id<RichTextDelegate>)delegate { [self richTextRangesWithStrings:strings]; if ([self delegate] != delegate) { [self setDelegate:delegate]; } } #pragma mark - touchAction - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if (!self.isClickAction) { return; } if (objc_getAssociatedObject(self, @selector(enabledClickEffect))) { self.isClickEffect = self.enabledClickEffect; } UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self]; weakSelf(self); [self richTextFrameWithTouchPoint:point result:^(NSString *string, NSRange range, NSInteger index) { if (weakself.clickBlock) { weakself.clickBlock (string , range , index); } if ([weakself delegate] && [[weakself delegate] respondsToSelector:@selector(didClickRichText:range:index:)]) { [[weakself delegate] didClickRichText:string range:range index:index]; } if (weakself.isClickEffect) { [weakself saveEffectDicWithRange:range]; [weakself clickEffectWithStatus:YES]; } }]; } - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { if((self.isClickAction) && ([self richTextFrameWithTouchPoint:point result:nil])) { return self; } return [super hitTest:point withEvent:event]; } #pragma mark - getClickFrame - (BOOL)richTextFrameWithTouchPoint:(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++) { RichTextModel *model = self.attributeStrings[j]; NSRange link_range = model.range; if (NSLocationInRange(index, link_range)) { if (resultBlock) { resultBlock (model.str , model.range , (NSInteger)j); } CFRelease(frame); CFRelease(framesetter); CGPathRelease(Path); return YES; } } } } CFRelease(frame); CFRelease(framesetter); CGPathRelease(Path); return NO; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if (self.isClickEffect) { [self performSelectorOnMainThread:@selector(clickEffectWithStatus:) withObject:nil waitUntilDone:NO]; } } - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if (self.isClickEffect) { [self performSelectorOnMainThread:@selector(clickEffectWithStatus:) 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 - clickEffect - (void)clickEffectWithStatus:(BOOL)status { if (self.isClickEffect) { 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:self.clickEffectColor 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 - getRange - (void)richTextRangesWithStrings:(NSArray <NSString *> *)strings { if (self.attributedText == nil) { self.isClickAction = NO; return; } self.isClickAction = YES; self.isClickEffect = YES; __block NSString *totalStr = self.attributedText.string; self.attributeStrings = [NSMutableArray array]; weakSelf(self); [strings enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSRange range = [totalStr rangeOfString:obj]; if (range.length != 0) { totalStr = [totalStr stringByReplacingCharactersInRange:range withString:[weakself getStringWithRange:range]]; RichTextModel *model = [[RichTextModel alloc]init]; model.range = range; model.str = obj; [weakself.attributeStrings addObject:model]; } }]; } - (NSString *)getStringWithRange:(NSRange)range { NSMutableString *string = [NSMutableString string]; for (int i = 0; i < range.length ; i++) { [string appendString:@" "]; } return string; } @end
使用方法:app
NSString *text = @"池塘大橋下,遊過一羣鴨池塘,快來快來數一數,二四六七八,嘎嘎嘎嘎,真呀真多呀,fly,數不清到底多少鴨"; NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:text]; [string addAttribute:action1 value:@"" range:NSMakeRange(2, 3)]; [string addAttributes:@{NSStrikethroughStyleAttributeName:@"",NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(2, 3)]; self.label2.userInteractionEnabled = true; self.label2.attributedText = string; // self.label2.enabledClickEffect = false; [self.label2 clickRichTextWithStrings:@[@"池塘"] clickAction:^(NSString *string, NSRange range, NSInteger index) { NSLog(@"-------"); }];
若是想用富文本文件,能夠參考的另一篇博客;ui