封裝UIlabel 辨別使用者名稱 ,話題 ,連結,電話,高亮文字等
阿新 • • 發佈:2019-01-29
封裝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];
}
三、執行效果
這些只是我能用到的一些屬性,如果你們需要其他的可以自己試著寫或者聯絡我,我根據你們的需求來定義!
執行效果如下: