过滤 UILabel 显示时的网页字符串

来源:互联网 发布:公务员工作累吗 知乎 编辑:程序博客网 时间:2024/05/02 00:02

@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