自定義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 以外的值都未定義歹垫。