概述

对UIlabel进行封装 用于辨别用户名 ,话题 ,链接,电话,高亮文字等,链接跳转网页,电话点击拨打电话,完美封装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 辨别用户名 ,话题 ,链接,电话,高亮文字等的更多相关文章

  1. Laya和白鹭的高亮文字 (textFlow)

    版本2.1.1.1 游戏中用的高亮文字,例如下图 "使用 2钻石 可获得金币",2钻石是不同颜色高亮的. 白鹭的高亮文字有文本样式 txt.textFlow = <Array ...

  2. 【Android车载系统 News | Tech 4】知乎--车载话题链接

    知乎--车联网话题http://www.zhihu.com/topic/19646713

  3. 封装MongoDB的 asp.net 链接类

    using System;using System.Collections.Generic;using System.Linq;using MongoDB; /// <summary>// ...

  4. NGUI-使用UILabel呈现图片和不同格式的文字

    1.可以使用BBCode标记 [b]Bold[/b]                      粗体[i]italic[/i]                         斜体[u]underli ...

  5. 【Swift】获取UILabel中点击的某个功能标签文字并作出响应动作

    1.需求 首先.针对UILabel中显示的多个功能标签,作出颜色标记提示. 其次.对关键字作出点击响应动作. 如图所示: 解决: 1.使用正则匹配到关键字 public static var hash ...

  6. UILabel图文混排显示图片和文字

    //传入文字 自动图片放在左边文字紧接着后排排布 -(void)setAttrDetailLabelWithTitle:(NSString *)title { NSMutableAttributedS ...

  7. Android-滑动解锁高亮文字自定义TextView

    public class HightLightTextView extends TextView { // 存储view的宽度 private int mTextViewWidth = 0; // 画 ...

  8. 谈CSS模块化【封装-继承-多态】

    第一次听到“CSS模块化”这个词是在WebReBuild的第四届“重构人生”年会上,当时我还想,“哈,CSS也有模块化,我没听错吧?”事实上,我没听错,你也没看错,早就有CSS模块化这个概念了.之所以 ...

  9. springboot-18-springboot的参数封装

    springboot的参数封装, 和springmvc相识 简单参数的封装 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * ...

随机推荐

  1. 【spring cloud】spring cloud zuul 路由网关

    GitHub源码地址:https://github.com/AngelSXD/springcloud 版本介绍: <properties> <project.build.source ...

  2. CentOS 6.5安装配置Nginx

    Ubuntu下安装nginx,直接apt-get install nginx就行了,很方便. 但是今天装了CentOS6.5,直接yum install nginx不行,要先处理下源,下面是安装完整流 ...

  3. VMware虚拟机的三种联网方法及原理

    VMware虚拟机的三种联网方法及原理 博客分类: 操作系统 虚拟机Vmware互联网网络应用网络协议 一.Brigde——桥接  :默认使用VMnet0 1.原理: Bridge  桥"就 ...

  4. GNU GRUB

    Introduction GNU GRUB is a Multiboot boot loader. It was derived from GRUB, the GRand Unified Bootlo ...

  5. Android触摸事件

    简单介绍: 做了一个语音发送UI的小demo. 按下显示语音窗体,依据音量调节UI音量显示,上划至窗体显示取消发送. 原理: 1:获取什么事件来运行操作: 给Button加入setOnTouchLis ...

  6. Windows 下Npm和NodeJS升级

    前提电脑中已经安装过NodeJS, npm.现在需要进行升级操作. 1.查看当前的npm和NodeJs的版本: C:\Users\Administrator>node -vv4.4.3 C:\U ...

  7. UNIX 域套接字——UNIX domain socket

    /*********************程序相关信息********************* * 程序编号:015 * 程序编写起始日期:2013.11.30 * 程序编写完成日期:2013.1 ...

  8. [leetcode]Balanced Binary Tree @ Python

    原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...

  9. 搭建个人博客-hexo+github

    自己也算是摸爬滚打搭建成功,然后自己再重新安装部署一遍,把完整步骤分享给大家,同时最后有一些连接,如果我的步骤不行,大家可以参考其他人的(这个有点花费时间,大家提前有个心理准备 - _-) 一.第一步 ...

  10. 让两个DIV的高度隐式同步

    以前遇到两个相临近的块,高度要一样,但是内容多少又不定时,我都是通过把这两块封装在TD里面实现,但今天在CSDN上面看到有人要通过JS来实现这个,我尝试了一下.http://topic.csdn.ne ...