封装UIlabel 辨别用户名 ,话题 ,链接,电话,高亮文字等
概述
详细
封装UIlabel辨别用户名,话题,链接,电话,高亮文字等,链接跳转网页,拨打电话
完美封装UIlabel,适合绝大多数需求
一、准备工作
整体项目中,主要是自己自定义的一个GZLabel类

只需要依赖此自定义类,可以设置自己想要的所有效果。
二、程序实现
对UIlabel进行封装,让其识别一些Label不匹配的东西,我们就要利用
NSMutableAttributeString
来对其进行处理,我们用到的主要是它的子类
@property(nonatomic, strong)NSTextStorage *GZTextString;
主要就是对Label进行匹配,给其一个类型属性

我们想要达到自己想要的效果的话,就必须要对label进行分类处理
typedef NS_ENUM(NSUInteger , GZLabelStyle){
GZLabelStyleNone = 0,
GZLabelStyleUser = 1,
GZLabelStyleTopic = 2,
GZLabelStyleLink = 3,
GZLabelStyleAgreement = 4,
GZLabelStyleUserDefine = 5,
GZLabelStylePhoneNumber = 6
};
给label设置各种属性,(点击前后颜色,代理 ,点击事件等)
/* 普通文字颜色 */
@property(nonatomic , strong)UIColor *GZLabelNormalColor ;
/* 选中时高亮背景色 */
@property(nonatomic , strong)UIColor *GZLabelHightLightBackgroundColor ;
/* 字符串+显示颜色 字典数组, */
@property(nonatomic, strong)NSArray<NSDictionary *> *GZLabelMatchArr;
/* 高亮文字设置颜色*/
-(void)setHightLightLabelColor:(UIColor *)hightLightColor forGZLabelStyle:(GZLabelStyle)GZLabelStyle;
/* delegate */
@property(nonatomic, weak)id<GZLabelDelegate> delegate;
/* 点击事件block */
@property(nonatomic, strong)TapGZLabel GZTapOperation;
设置自己想要的文字颜色,范围 位置
/* 用于记录用户选中的range */
@property(nonatomic, assign)NSRange selectedRange;
/* 用户记录点击还是松开 */
@property(nonatomic, assign)BOOL isSelected;
/* 用户文字颜色 */
@property(nonatomic, strong)UIColor *userHightColor;
/* 话题文字颜色 */
@property(nonatomic, strong)UIColor *topicHightColor;
/* 链接文字颜色 */
@property(nonatomic, strong)UIColor *linkHightColor;
/* 协议/政策文字颜色 */
@property(nonatomic, strong)UIColor *agreementHightColor;
/* 电话号码文字颜色 */
@property(nonatomic, strong)UIColor *PhoneNumberHightColor;
/* 链接范围 */
@property(nonatomic, strong)NSArray *linkRangesArr;
/* 用户名范围 */
@property(nonatomic, strong)NSArray *userRangesArr;
/* 话题范围 */
@property(nonatomic, strong)NSArray *topicRangesArr;
/* 协议/政策范围 */
@property(nonatomic, strong)NSArray *agreementRangesArr;
/* 电话号码范围 */
@property(nonatomic, strong)NSArray *PhoneNumberRangesArr;
/* 自定义要查找的范围 */
@property(nonatomic, strong)NSArray *userDefineRangesArr;
再者我们需要重写系统的属性
#pragma mark 重写系统的属性
-(void)setText:(NSString *)text{
[super setText:text];
[self prepareText];
}
-(void)setFont:(UIFont *)font{
[super setFont:font];
[self prepareText];
}
-(void)setTextColor:(UIColor *)textColor{
[super setTextColor:textColor];
[self prepareText];
}
系统回调
#pragma mark 系统回调
// 布局子控件
-(void)layoutSubviews{
[super layoutSubviews]; // 设置容器的大小为Label的尺寸
self.textContainer.size = self.frame.size;
}
字符串匹配封装
#pragma mark 字符串匹配封装
// 查找用户给定的字符串的range
-(NSArray<NSDictionary*> *)getUserDefineStringsRange{ if (self.GZLabelMatchArr.count == 0) return nil; NSMutableArray<NSDictionary*> *arrM = [NSMutableArray array]; NSString *str = [self.GZTextString string];
for (NSDictionary *dict in self.GZLabelMatchArr) {
NSString *subStr = dict[@"string"];
UIColor *color = dict[@"color"];
// 没传入字符串
if (!subStr) return nil; NSRange range = [str rangeOfString:subStr]; // 没找到
if (range.length == 0) continue; NSValue *value = [NSValue valueWithBytes:&range objCType:@encode(NSRange)];
NSMutableDictionary *aDictM = [NSMutableDictionary dictionary];
aDictM[GZRange] = value;
aDictM[GZColor] = color;
[arrM addObject:[aDictM copy]];
} return [arrM copy];
}
创建正则表达式对象
-(NSArray *)getRanges:(NSString *)pattern{
// 创建正则表达式对象
NSError *error;
NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:0 error:&error];
return [self getRangesFromResult:regex];
}
三、运行效果
这些只是我能用到的一些属性,如果你们需要其他的可以自己试着写或者联系我,我根据你们的需求来定义!
运行效果如下:

注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权
封装UIlabel 辨别用户名 ,话题 ,链接,电话,高亮文字等的更多相关文章
- Laya和白鹭的高亮文字 (textFlow)
版本2.1.1.1 游戏中用的高亮文字,例如下图 "使用 2钻石 可获得金币",2钻石是不同颜色高亮的. 白鹭的高亮文字有文本样式 txt.textFlow = <Array ...
- 【Android车载系统 News | Tech 4】知乎--车载话题链接
知乎--车联网话题http://www.zhihu.com/topic/19646713
- 封装MongoDB的 asp.net 链接类
using System;using System.Collections.Generic;using System.Linq;using MongoDB; /// <summary>// ...
- NGUI-使用UILabel呈现图片和不同格式的文字
1.可以使用BBCode标记 [b]Bold[/b] 粗体[i]italic[/i] 斜体[u]underli ...
- 【Swift】获取UILabel中点击的某个功能标签文字并作出响应动作
1.需求 首先.针对UILabel中显示的多个功能标签,作出颜色标记提示. 其次.对关键字作出点击响应动作. 如图所示: 解决: 1.使用正则匹配到关键字 public static var hash ...
- UILabel图文混排显示图片和文字
//传入文字 自动图片放在左边文字紧接着后排排布 -(void)setAttrDetailLabelWithTitle:(NSString *)title { NSMutableAttributedS ...
- Android-滑动解锁高亮文字自定义TextView
public class HightLightTextView extends TextView { // 存储view的宽度 private int mTextViewWidth = 0; // 画 ...
- 谈CSS模块化【封装-继承-多态】
第一次听到“CSS模块化”这个词是在WebReBuild的第四届“重构人生”年会上,当时我还想,“哈,CSS也有模块化,我没听错吧?”事实上,我没听错,你也没看错,早就有CSS模块化这个概念了.之所以 ...
- springboot-18-springboot的参数封装
springboot的参数封装, 和springmvc相识 简单参数的封装 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * ...
随机推荐
- 无损转换Image为Icon z
如题,市面上常见的方法是: var handle = bmp.GetHicon(); //得到图标句柄 return Icon.FromHandle(handle); //通过句柄得到图标 此法的问题 ...
- UITextView in iOS7 doesn't scroll
UITextView in iOS7 has been really weird. As you type and are entering the last line of your UITextV ...
- 仿LOL项目开发第七天
仿LOL项目开发第七天 by 草帽 不知不觉已经写到了第七篇这种类型的博客,但是回过头看看之前写的,发现都只能我自己能看懂. 我相信在看的童鞋云里雾里的,因为我基本上没怎么详细讲一个脚本怎么用?但是你 ...
- 启明星产品与微软Active Directory活动目录集成说明
本文介绍启明星Helpdesk系统,如何和微软域集成. (1)确保在集成微软域之前,系统已经能够访问. (2)进入后台,用户管理页面,新加一个账户,此账户需要在微软域里(普通账户即可),然后在角色里设 ...
- Java 从基础到进阶学习之路---类编写以及文档凝视.
Java之前在学习过,基础知识还没有忘光,并且这些高级语言实在是太像,所以那些数据类型,或者循环控制流,以及标准设备等等就直接略过不说了. 只是一些重大概念会穿插在文章的介绍中. So,这些文章适合于 ...
- libuv之介绍
本人是在研究linux下socket TCP/IP通讯时,用到了一些linux下的API,比如socket, connect, bind,listen, accept等等,简单写个点对点的通讯,直接用 ...
- Informatica 常用组件Source Qualifier之九 创建SQ转换
可以配置 Designer 在您将源拖到映射中时默认创建源限定符转换,您也可以手动创建源限定符转换. 默认创建源限定符转换 可以配置 Designer 在您将源拖到映射中时自动创建一个源限定符转换. ...
- 第十一章 springboot + mongodb(简单查询)
1.mongodb在mac上的安装 下载mongodb,https://www.mongodb.org/ 解压缩到一个指定文件夹,如:/Users/enniu1/Desktop/zjg/mongodb ...
- graphic rendering pipeline
整理下管线 此时一定要有这张图 注意表中的数据流向 强调几个细节 之前对次序理解有点乱 rasterizer之前 管线里是只有逐顶点信息的 IA里面会setup primitive 通过Primit ...
- Spring boot基础:配置文件配置变量、多环境的配置
一.配置 resources下面application.properties 1.普通配置 resources下面application.properties,比如写上:server.port=909 ...