@implementation LableTextFilter
//
+ (void)initialize
{
// 获取到UILabel中setText对应的method
Method setText = class_getInstanceMethod([UILabel class], @selector(setText:));
Method setTextMySelf = class_getInstanceMethod([self class], @selector(setTextHooked:));
// 将目标函数的原实现绑定到setTextOriginalImplemention方法上
IMP setTextImp = method_getImplementation(setText);
class_addMethod([UILabel class], @selector(setTextOriginal:), setTextImp, method_getTypeEncoding(setText));
// 然后用我们自己的函数的实现,替换目标函数对应的实现
IMP setTextMySelfImp = method_getImplementation(setTextMySelf);
class_replaceMethod([UILabel class], @selector(setText:), setTextMySelfImp, method_getTypeEncoding(setText));
}
/*
* 截获到 UILabel 的setText
* 我们可以先处理完以后,再继续调用正常处理流程
*/
- (void)setTextHooked:(NSString *)text
{
//在这里插入过滤算法
text = [text stringByReplacingOccurrencesOfString:@"<br>" withString:@"\r\n"];
// do something what ever you want
NSLog(@"haha, this is my self setText !!!!!!!");
// invoke original implemention
[self performSelector:@selector(setTextOriginal:) withObject:text];
}
@end
过滤 UILabel 显示时的网页字符串
最新推荐文章于 2021-05-31 04:08:27 发布
本文详细介绍了如何通过Objective-C替换UILabel的setText方法,并在此基础上实现了一个自定义过滤算法,用于处理输入的文本。通过在设置文本前进行过滤,可以有效避免特定字符或标签的直接显示,提升用户界面的安全性和可读性。
1758

被折叠的 条评论
为什么被折叠?



