用PPLabel實現(xiàn)特定范圍文字的顏色及點擊事件--仿QQ微信非好友驗證信息

自定義cell

.h

#import <UIKit/UIKit.h>
#import "PPLabel.h"
//藍(lán)色字點擊
typedef void(^UnfriendMessageBlueTextTouch)();

@interface UnfriendCell : UITableViewCell<PPLabelDelegate>
@property (weak, nonatomic) IBOutlet PPLabel *msgLabel;
@property (weak, nonatomic) IBOutlet UIView *bgView;

// 屬性String;
@property (nonatomic, strong) NSMutableAttributedString *attText;
@property (nonatomic, assign) NSRange attRange;

@property (nonatomic,copy)UnfriendMessageBlueTextTouch blueTextTouchBlock;
@end

.m

#import "UnfriendCell.h"

@interface UnfriendCell ()

@property(nonatomic, strong) NSArray* matches;

@end
@implementation UnfriendCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.msgLabel.text = @"你現(xiàn)在不是他(她)的好友侯勉,現(xiàn)在發(fā)送好友驗證,對方驗證通過后色查,才可聊天消玄。";
    self.bgView.layer.cornerRadius =10;
    self.bgView.layer.masksToBounds = YES;
    self.msgLabel.delegate = self;

    NSError *error = NULL;
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];

    self.matches = [detector matchesInString:self.msgLabel.text options:0 range:NSMakeRange(0, self.msgLabel.text.length)];

    [self highlightLinksWithIndex:NSNotFound];
}

#pragma mark -

- (void)label:(PPLabel *)label didBeginTouch:(UITouch *)touch     onCharacterAtIndex:(CFIndex)charIndex {

[self highlightLinksWithIndex:charIndex];
}

- (void)label:(PPLabel *)label didMoveTouch:(UITouch *)touch onCharacterAtIndex:(CFIndex)charIndex {

    [self highlightLinksWithIndex:charIndex];
}

- (void)label:(PPLabel *)label didEndTouch:(UITouch *)touch onCharacterAtIndex:(CFIndex)charIndex {

    [self highlightLinksWithIndex:NSNotFound];

    for (NSTextCheckingResult *match in self.matches) {
    
    
        if (match.range.length == 10 &&match.range.location ==10) {
            NSLog(@"==yes");
        }
        if ([match resultType] == NSTextCheckingTypeLink) {
        
            NSRange matchRange = [match range];
        
            if ([self isIndex:charIndex inRange:matchRange]) {
            
                NSLog(@"==");
                break;
            }
        }
    }

}

- (void)label:(PPLabel *)label didCancelTouch:(UITouch *)touch {

    [self highlightLinksWithIndex:NSNotFound];
}

#pragma mark -

- (BOOL)isIndex:(CFIndex)index inRange:(NSRange)range {
    return index > range.location && index < range.location+range.length;
}

- (void)highlightLinksWithIndex:(CFIndex)index {

     NSMutableAttributedString* attributedString = [self.msgLabel.attributedText mutableCopy];

    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(15, 6)];

    self.msgLabel.attributedText = attributedString;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CFIndex touchIndex =  [self.msgLabel characterIndexAtPoint:[touch locationInView:self.msgLabel]];
    // 單向好友提示信息;
    if ((touchIndex-1) >= self.attRange.location &&(touchIndex-1)<= (self.attRange.location + self.attRange.length)) {
        self.blueTextTouchBlock();
        NSLog(@"%ld==%ld",touchIndex,self.attRange.location);
    }
}
@end

聊天界面

#import "ChatController.h"
#import "UnfriendCell.h"

@interface ChatController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@end
static NSString *const SenderCell = @"unfriendC";
@implementation ChatController

- (void)viewDidLoad {
    [super viewDidLoad];   
    self.dataSource = [NSMutableArray array];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UnfriendCell * cell = [tableView dequeueReusableCellWithIdentifier:SenderCell];
    NSRange attRange;
    attRange.location = 15;
    attRange.length = 6;
    cell.attRange = attRange;
    cell.blueTextTouchBlock = ^(){
        //藍(lán)色文字點擊
        NSLog(@"????????");
    };

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)touchesShouldCancelInContentView:(UIView *)view{
    if ([view isKindOfClass:[UnfriendCell class]]) {
        return NO;
    }else{
    
        return YES;
    }
}
@end

DEMO地址:https://github.com/WSGNSLog/AttributeTextInRangeTouch

附:NSAttributedString屬性總結(jié)

字符屬性可以應(yīng)用于 attributed string 的文本中跟伏。

 NSString *const NSFontAttributeName;(字體)
 
 NSString *const NSParagraphStyleAttributeName;(段落)
 
 NSString *const NSForegroundColorAttributeName;(字體顏色)
 
 NSString *const NSBackgroundColorAttributeName;(字體背景色)
 
 NSString *const NSLigatureAttributeName;(連字符)
 
 NSString *const NSKernAttributeName;(字間距)
 
 NSString *const NSStrikethroughStyleAttributeName;(刪除線)
 
 NSString *const NSUnderlineStyleAttributeName;(下劃線)
 
 NSString *const NSStrokeColorAttributeName;(邊線顏色)
 
 NSString *const NSStrokeWidthAttributeName;(邊線寬度)
 
 NSString *const NSShadowAttributeName;(陰影)(橫豎排版)
 
 NSString *const NSVerticalGlyphFormAttributeName;
常量
 1、 NSFontAttributeName(字體)

該屬性所對應(yīng)的值是一個 UIFont 對象翩瓜。該屬性用于改變一段文本的字體受扳。如果不指定該屬性,則默認(rèn)為12-point Helvetica(Neue)兔跌。

 2勘高、 NSParagraphStyleAttributeName(段落)

該屬性所對應(yīng)的值是一個 NSParagraphStyle 對象。該屬性在一段文本上應(yīng)用多個屬性坟桅。如果不指定該屬性华望,則默認(rèn)為 NSParagraphStyle 的defaultParagraphStyle 方法返回的默認(rèn)段落屬性。

 3桦卒、 NSForegroundColorAttributeName(字體顏色)

該屬性所對應(yīng)的值是一個 UIColor 對象立美。該屬性用于指定一段文本的字體顏色。如果不指定該屬性方灾,則默認(rèn)為黑色。

 4碌更、NSBackgroundColorAttributeName(字體背景色)

該屬性所對應(yīng)的值是一個 UIColor 對象裕偿。該屬性用于指定一段文本的背景顏色。如果不指定該屬性痛单,則默認(rèn)無背景色嘿棘。

 5、 NSLigatureAttributeName(連字符)

該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))旭绒。連體字符是指某些連在一起的字符鸟妙,它們采用單個的圖元符號。0 表示沒有連體字符挥吵。1 表示使用默認(rèn)的連體字符重父。2表示使用所有連體符號。默認(rèn)值為 1(注意忽匈,iOS 不支持值為 2)房午。

 6、 NSKernAttributeName(字間距)

該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))丹允。字母緊排指定了用于調(diào)整字距的像素點數(shù)郭厌。字母緊排的效果依賴于字體袋倔。值為 0 表示不使用字母緊排。默認(rèn)值為0折柠。

 7宾娜、 NSStrikethroughStyleAttributeName(刪除線)

該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))。該值指定是否在文字上加上刪除線扇售,該值參考“Underline Style Attributes”碳默。默認(rèn)值是NSUnderlineStyleNone。

 8缘眶、 NSUnderlineStyleAttributeName(下劃線)

該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))嘱根。該值指定是否在文字上加上下劃線,該值參考“Underline Style Attributes”巷懈。默認(rèn)值是NSUnderlineStyleNone该抒。

 9、 NSStrokeColorAttributeName(邊線顏色)

該屬性所對應(yīng)的值是一個 UIColor 對象顶燕。如果該屬性不指定(默認(rèn))凑保,則等同于 NSForegroundColorAttributeName。否則涌攻,指定為刪除線或下劃線顏色欧引。更多細(xì)節(jié)見“Drawing attributedstrings that are both filled and stroked”。

 10恳谎、NSStrokeWidthAttributeName(邊線寬度)

該屬性所對應(yīng)的值是一個 NSNumber 對象(小數(shù))芝此。該值改變描邊寬度(相對于字體size 的百分比)。默認(rèn)為 0因痛,即不改變婚苹。正數(shù)只改變描邊寬度。負(fù)數(shù)同時改變文字的描邊和填充寬度鸵膏。例如膊升,對于常見的空心字,這個值通常為3.0谭企。

 11廓译、 NSShadowAttributeName(陰影)

該屬性所對應(yīng)的值是一個 NSShadow 對象。默認(rèn)為 nil债查。

 12非区、NSVerticalGlyphFormAttributeName(橫豎排版)

該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))。0 表示橫排文本攀操。1 表示豎排文本院仿。在 iOS 中,總是使用橫排文本,0 以外的值都未定義歹垫。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末剥汤,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子排惨,更是在濱河造成了極大的恐慌吭敢,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件暮芭,死亡現(xiàn)場離奇詭異鹿驼,居然都是意外死亡,警方通過查閱死者的電腦和手機辕宏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進(jìn)店門畜晰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人瑞筐,你說我怎么就攤上這事凄鼻。” “怎么了聚假?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵块蚌,是天一觀的道長。 經(jīng)常有香客問我膘格,道長峭范,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任瘪贱,我火速辦了婚禮纱控,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘政敢。我一直安慰自己其徙,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布喷户。 她就那樣靜靜地躺著,像睡著了一般访锻。 火紅的嫁衣襯著肌膚如雪褪尝。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天期犬,我揣著相機與錄音河哑,去河邊找鬼。 笑死龟虎,一個胖子當(dāng)著我的面吹牛璃谨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼佳吞,長吁一口氣:“原來是場噩夢啊……” “哼拱雏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起底扳,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤铸抑,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后衷模,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鹊汛,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年阱冶,在試婚紗的時候發(fā)現(xiàn)自己被綠了刁憋。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡木蹬,死狀恐怖至耻,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情届囚,我是刑警寧澤有梆,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站意系,受9級特大地震影響泥耀,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蛔添,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一痰催、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧迎瞧,春花似錦夸溶、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至足绅,卻和暖如春捷绑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背氢妈。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工粹污, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人首量。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓壮吩,卻偏偏與公主長得像进苍,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子鸭叙,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,947評論 2 355

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,144評論 25 707
  • 緊張忙碌快節(jié)奏的都市生活递雀,使很多人經(jīng)常會感覺疲憊柄延,都想在工作之余好好的放松一下身心,讓自己能夠得到足夠的休息缀程。 那...
    一箭閱讀 893評論 3 3