IOS7下特殊字符串的崩溃处理

来源:互联网 发布:app视频直播源码下载 编辑:程序博客网 时间:2024/06/06 16:55

在IOS7刚出来的时候,一些阿拉伯符号的排版顺序问题,在一个label不够显示长度,而需要三个...显示的时候会发生崩溃。并且这些字符在绘制的时候特别的卡。因为内部将其替换掉其他字符。

#import<objc/objc.h>#import<objc/runtime.h>NSMutableSet *filterUILabelCharactersSet;@implementation LVLiveWorldFilterCharacters+ (void)initGSet{    if (!filterUILabelCharactersSet)    {         NSArray *tmpArray = @[@(1442),@(1631),@(1809),@(1840),@(1841),@(1842),@(1843),@(1844),@(1845),@(1846),@(1847),@(1848),@(1849),@(1850),@(1851),@(1852),@(1853),@(1854),@(1855),@(1856),@(1857),@(1858),@(1859),@(1860),@(1861),@(1862),@(1863),@(1864),@(1865),@(1866),@(1958),@(1959),@(1960),@(1961),@(1962),@(1963),@(1964),@(1965),@(1966),@(1967),@(1968),@(2027),@(2028),@(2029),@(2030),@(2031),@(2032),@(2033),@(2034),@(2035),@(2070),@(2071),@(2072),@(2073),@(2075),@(2076),@(2077),@(2078),@(2079),@(2080),@(2081),@(2082),@(2083),@(2085),@(2086),@(2087),@(2089),@(2090),@(2091),@(2092),@(2093),@(2137),@(2138),@(2139),@(2276),@(2277),@(2278),@(2279),@(2280),@(2281),@(2282),@(2283),@(2284),@(2285),@(2286),@(2287),@(2288),@(2289),@(2290),@(2291),@(2292),@(2293),@(2294),@(2295),@(2296),@(2297),@(2298),@(2299),@(2300),@(2301),@(2302),@(2362),@(2363),@(2383),@(2390),@(2391),@(2677),@(2946),@(3550),@(3981),@(3982),@(3983),@(4139),@(4140),@(4141),@(4142),@(4143),@(4144),@(4145),@(4146),@(4147),@(4148),@(4149),@(4150),@(4151),@(4152),@(4153),@(4154),@(4155),@(4156),@(4157),@(4158),@(4182),@(4183),@(4184),@(4185),@(4190),@(4191),@(4192),@(4194),@(4195),@(4196),@(4199),@(4200),@(4201),@(4202),@(4203),@(4204),@(4205),@(4209),@(4210),@(4211),@(4212),@(4226),@(4227),@(4228),@(4229),@(4230),@(4231),@(4232),@(4233),@(4234),@(4235),@(4236),@(4237),@(4239),@(4250),@(4251),@(4252),@(4253),@(4957),@(4958),@(4959),@(5908),@(5940),@(6070),@(6071),@(6072),@(6073),@(6074),@(6075),@(6076),@(6077),@(6078),@(6079),@(6080),@(6081),@(6082),@(6083),@(6084),@(6085),@(6086),@(6087),@(6088),@(6089),@(6090),@(6091),@(6092),@(6093),@(6094),@(6095),@(6096),@(6097),@(6098),@(6099),@(6109),@(6457),@(6458),@(6459),@(6679),@(6680),@(6752),@(6773),@(6774),@(6775),@(6776),@(6777),@(6778),@(6779),@(6780),@(6783),@(6964),@(6980),@(7019),@(7020),@(7021),@(7022),@(7023),@(7024),@(7025),@(7026),@(7027),@(7082),@(7083),@(7142),@(7154),@(7155),@(7223),@(7376),@(7377),@(7378),@(7380),@(7381),@(7382),@(7383),@(7384),@(7385),@(7386),@(7387),@(7388),@(7389),@(7390),@(7391),@(7392),@(7394),@(7395),@(7396),@(7397),@(7398),@(7399),@(7400),@(7405),@(7412),@(7627),@(7628),@(7629),@(7630),@(7631),@(7632),@(7633),@(7634),@(7635),@(7636),@(7637),@(7638),@(7639),@(7640),@(7641),@(7642),@(7643),@(7644),@(7645),@(7646),@(7647),@(7648),@(7649),@(7650),@(7651),@(7652),@(7653),@(7654),@(7676),@(7677),@(8418),@(8420),@(8421),@(8422),@(8423),@(8424),@(8425),@(8426),@(8427),@(8428),@(8429),@(8430),@(8431),@(8432),@(11503),@(11504),@(11505),@(12330),@(12331),@(12332),@(12333),@(12334),@(12335),@(42620),@(42621),@(42736),@(42737),@(43014),@(43204),@(43232),@(43233),@(43234),@(43235),@(43236),@(43237),@(43238),@(43239),@(43240),@(43241),@(43242),@(43243),@(43244),@(43245),@(43246),@(43247),@(43248),@(43249),@(43307),@(43308),@(43309),@(43347),@(43443),@(43456),@(43696),@(43698),@(43699),@(43700),@(43703),@(43704),@(43710),@(43711),@(43713),@(43766),@(44013),@(65060),@(65061),@(65062)];        filterUILabelCharactersSet = [[NSMutableSet alloc] initWithArray:tmpArray];    }}+ (void)hookLabelSetText{    [LVLiveWorldFilterCharacters initGSet];        //获取到UILabel中setText对应的method    Method setText = class_getInstanceMethod([UILabel class], @selector(setText:));    Method setTextMySelf = class_getInstanceMethod([LVLiveWorldFilterCharacters class], @selector(setTextMySelf:));        //将目标函数的原实现绑定到setTextOriginal方法上    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));}+ (void)hookTextFieldSetText{    [LVLiveWorldFilterCharacters initGSet];        //获取到UILabel中setText对应的method    Method setText = class_getInstanceMethod([UITextField class], @selector(setText:));    Method setTextMySelf = class_getInstanceMethod([LVLiveWorldFilterCharacters class], @selector(setTextMySelf:));        //将目标函数的原实现绑定到setTextOriginal方法上    IMP setTextIMP = method_getImplementation(setText);    class_addMethod([UITextField class], @selector(setTextOriginal:), setTextIMP, method_getTypeEncoding(setText));        //然后用我们自己的函数的实现,替换目标函数对应的实现    IMP setTextMySelfImp = method_getImplementation(setTextMySelf);    class_replaceMethod([UITextField class], @selector(setText:), setTextMySelfImp, method_getTypeEncoding(setText));}- (void)setTextMySelf:(NSString *)text{    NSString *newText = [LVLiveWorldFilterCharacters replaceSpecialCharacter:text];    //invoke original implemention    [self performSelector:@selector(setTextOriginal:) withObject:newText];}- (void)setTextOriginal:(NSString *)text{    }+ (NSString *)replaceSpecialCharacter:(NSString *)string{    [LVLiveWorldFilterCharacters initGSet];    NSMutableString * newString = [[NSMutableString alloc] initWithCapacity:string.length];    for (int i = 0; i < string.length; i++)    {        UniChar r = [string characterAtIndex:i];        if ([filterUILabelCharactersSet containsObject:@(r)]) {            r = 1939;        }                [newString appendString:[NSString stringWithCharacters:&r length:1]];    }    return newString;}@end


0 0
原创粉丝点击