源代码:https://github.com/lyb5834/YBAttributeTextTapAction地址
如果想用富文本文件,可以参考的另外一篇博客;
https://www.cnblogs.com/hualuoshuijia/p/5732619.html
如果只需要做点击,只需要使用一个类别文件就可以
#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
使用方法:
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(@"-------");
}];
如果想用富文本文件,可以参考的另外一篇博客;
https://www.cnblogs.com/hualuoshuijia/p/5732619.html
所有评论(0)