iOS微信朋友圈的點贊、評論功能,即點擊昵稱跳轉(zhuǎn)到個人主頁

很多社交軟件,都會有這么一個功能,就是點擊部分文字,觸發(fā)一個事件,像微信朋友圈、QQ空間那樣點擊評論者的昵稱跳轉(zhuǎn)到個人主頁,或者點擊點贊人的昵稱跳轉(zhuǎn)到個人主頁。

本菜鳥在做社交項目的時候也遇到這個問題,一開始不知道怎么處理好。找了網(wǎng)上的方法,都是說:1:昵稱上面覆蓋一個UIButton 。 2:設(shè)置文字部分區(qū)域,觸發(fā)事件。

本菜鳥覺得第二種方法可靠,于是找了第三方,比如:TTTAttributedLabel、YYText 、RTLabel都是很不錯的第三方富文本。但是本菜鳥又突發(fā)奇想,我只是要實現(xiàn)這個簡單的功能,強大的蘋果應(yīng)該不至于這么傻吧,于是就想著這么折騰一個不用第三方的辦法出來,然后就.... 你懂的 ... 下面直接貼效果圖 ... 不喜勿噴,大神多多指教。

評論的效果圖:

Paste_Image.png

點贊的效果圖:

Paste_Image.png

直接貼代碼:(調(diào)用UILable的擴展類)
在使用的地方調(diào)用方法:

-(void)richTextLable
{
    //評論
    NSString *nameOne = @"張三";
    NSString *nameTwo = @"我是李四";
    NSString *replyString = @"您好,您現(xiàn)在在干什么,么么噠。這個功能應(yīng)該滿足需求了。";
    
    NSString *totalString = [NSString stringWithFormat:@"%@回復(fù)%@:%@",nameOne,nameTwo,replyString];
    NSMutableAttributedString *newString = [[NSMutableAttributedString alloc] initWithString:totalString];
    [newString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, totalString.length)];
    [newString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, nameOne.length)];
    [newString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(nameOne.length+2, nameTwo.length)];
    //設(shè)置行距 實際開發(fā)中間距為0太丑了,根據(jù)項目需求自己把握
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 3;
    [newString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, totalString.length)];
    
    UILabel *richTextLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, self.view.bounds.size.width - 40, 20)];
    //richTextLbl.backgroundColor = [UIColor greenColor];
    richTextLbl.numberOfLines = 0;//設(shè)置UILable自適應(yīng)
    richTextLbl.attributedText = newString;
    [self.view addSubview:richTextLbl];
    [richTextLbl sizeToFit];
    
    [richTextLbl onTapRangeActionWithString:@[nameOne,nameTwo] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
        NSLog(@"點擊了-->--%@ ---下標(biāo):%ld",string,index);
    }];
    
    
    //點贊
    UILabel *goodLbl = [[UILabel alloc] init];
    goodLbl.frame = CGRectMake(20, 170, 100, 20);
    goodLbl.text = @"點贊的:";
    [self.view addSubview:goodLbl];
    
    NSArray *goodArray = @[@"張三",@"李四",@"王五",@"李兆",@"粟子",@"小李",@"李四",@"王五",@"李兆",@"粟子",@"小李"];
    NSString *goodTotalString = [goodArray componentsJoinedByString:@", "];
    
    NSMutableAttributedString *newGoodString = [[NSMutableAttributedString alloc] initWithString:goodTotalString];
    [newGoodString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, goodTotalString.length)];
    
    //設(shè)置行距 實際開發(fā)中間距為0太丑了,根據(jù)項目需求自己把握
    NSMutableParagraphStyle *paragraphstyle = [[NSMutableParagraphStyle alloc] init];
    paragraphstyle.lineSpacing = 3;
    [newGoodString addAttribute:NSParagraphStyleAttributeName value:paragraphstyle range:NSMakeRange(0, goodTotalString.length)];
    
    UILabel *goodTextLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, self.view.bounds.size.width - 40, 20)];
    goodTextLbl.backgroundColor = [UIColor orangeColor];
    goodTextLbl.numberOfLines = 0;//設(shè)置UILable自適應(yīng)
    goodTextLbl.attributedText = newGoodString;
    [self.view addSubview:goodTextLbl];
    [goodTextLbl sizeToFit];
    
    [goodTextLbl onTapRangeActionWithString:goodArray tapClicked:^(NSString *string, NSRange range, NSInteger index) {
        NSLog(@"這是第--%ld--個點贊的,他是--%@",index,string);
    }];
}

下面是UILable擴展類

#import <UIKit/UIKit.h>

@interface XLRichTextModel : NSObject

@property (nonatomic, copy) NSString *string;
@property (nonatomic, assign) NSRange range;

@end

@interface UILabel (Category)

///是否顯示點擊效果,默認(rèn)是打開
@property (nonatomic, assign) BOOL isShowTagEffect;

///TagArray  點擊的字符串?dāng)?shù)組
- (void)onTapRangeActionWithString:(NSArray <NSString *> *)TagArray tapClicked:(void (^) (NSString *string , NSRange range , NSInteger index))tapClick;

@end
#import "UILabel+Category.h"
#import <objc/runtime.h>
#import <CoreText/CoreText.h>
#import <Foundation/Foundation.h>

@implementation XLRichTextModel

@end

@implementation UILabel (Category)

#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)isShowTagEffect
{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

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

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

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

#pragma mark - 事件方法
- (void)onTapRangeActionWithString:(NSArray <NSString *> *)TagArray tapClicked:(void (^) (NSString *string , NSRange range , NSInteger index))tapClick
{
    //獲取部分點擊的區(qū)域
    [self tagRangeActionWithString:TagArray];
    
    if (self.tapBlock != tapClick) {
        self.tapBlock = tapClick;
    }
}

#pragma mark - touchAction
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (!self.isTapAction) {
        return;
    }
    
    if (objc_getAssociatedObject(self, @selector(isShowTagEffect))) {
        self.isTapEffect = self.isShowTagEffect;
    }
    
    UITouch *touch = [touches anyObject];
    
    CGPoint point = [touch locationInView:self];
    
    __weak typeof(self) weakSelf = self;
    
    [self tapFrameWithTouchPoint: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 tapFrameWithTouchPoint:point result:nil]) {
            return self;
        }
    }
    return [super hitTest:point withEvent:event];
}

#pragma mark - 獲取點擊的范圍
- (BOOL)tapFrameWithTouchPoint:(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];
        }
        
        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) {
        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 lineBounds: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++) {
                
                XLRichTextModel *model = self.attributeStrings[j];
                
                NSRange link_range = model.range;
                if (NSLocationInRange(index, link_range)) {
                    if (resultBlock) {
                        resultBlock (model.string , model.range , (NSInteger)j);
                    }
                    return YES;
                }
            }
        }
    }
    return NO;
}

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

- (void)touchesCancelled:(NSSet<UITouch *> *)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)lineBounds:(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 - 是否顯示點擊效果  默認(rèn)是   項目中一般是現(xiàn)實點擊效果
- (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 lightGrayColor] 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 - 獲取部分點擊的區(qū)域
- (void)tagRangeActionWithString:(NSArray <NSString *>  *)strings
{
    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;
    
    [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 stringWithRange:range]];
            
            XLRichTextModel *model = [XLRichTextModel new];
            
            model.range = range;
            
            model.string = obj;
            
            [weakSelf.attributeStrings addObject:model];
            
        }
        
    }];
}

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

@end

------------------------互勉、不喜勿噴、大神多多指教----------------------

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,372評論 25 708
  • 2017.02.22 可以練習(xí),每當(dāng)這個時候,腦袋就犯困,我這腦袋真是神奇呀,一說讓你做事情,你就犯困,你可不要太...
    Carden閱讀 1,378評論 0 1
  • 豆瓣APP的上線已經(jīng)有很長周期,版本更迭數(shù)次,在功能和交互界面上都有了很大改變。這次通過倒推的形式制作PRD文檔,...
    郅絕閱讀 3,120評論 0 12
  • 一、概述 iOS開發(fā)中,相信許多開發(fā)者都遇到過,類似于像微信朋友圈的評論回復(fù)功能的開發(fā),難點莫過于 Cell里面的...
    CoderMikeHe閱讀 27,124評論 52 167
  • “麻煩有空的時候簽個字?!敝齑蟪菑墓陌锬贸鲆环菸募旁诓妥郎?。 “什么文件還需要我簽字呀,你代簽不行嗎?”我身...
    黃山姑娘閱讀 746評論 15 16