解决IOS10字体适配问题(下)

上一篇 解决IOS10字体适配问题(上) 讲解了纯代码布局的解决方案, 但在这XIB泛滥的时代, 必须把XIB也解决一下, 嘿嘿

严重警告!!! 以下代码会直接修改你的代码文件, 测试前请务必备份!!!!!

首先你需要一个强大的功能库, 来自鄙人的代码垃圾堆, https://github.com/qokelate/sma11caseA, 然后在OSX中运行如下代码, 顶多也就4-5秒, 一份完全兼容的XIB就诞生了

+ (void)fix_ios10_xib
{
    NSString *path = @"/Users/xxx/xxx/xxx/xxx/sma11case/code/xxxxxx";
    
    [NSFM enumFilesWithPath:path block:^BOOL(NSString *dirPath, NSString *fileName, BOOL isDir) {
        if (isDir) return YES;
        
        if (NO == [fileName hasSuffix:@"xib"]
            && NO == [fileName hasSuffix:@".nib"]
            && NO == [fileName hasSuffix:@".storyboard"]) return YES;
        
        NSString *file = [NSString stringWithFormat:@"%@/%@", dirPath, fileName];
        NSMutableString *code = [NSMutableString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:NULL];
        
        MLog(@"process: %@", file);
        
        NSArray *fonts = nil;
        do{
            fonts = [code regexpMatchResults:@" pointSize=\"[\\d\\.]+\""];
            if (0 == fonts.count) break;
            
            NSTextCheckingResult *exp = fonts.firstObject;
            NSString *line = [code substringWithRange:exp.range];
            double weight = [line regexpFirstMatch:@"\\d+"].doubleValue;
            double nweight = weight * 17.0 / 17.5;
            NSString *nline = [NSString stringWithFormat:@" pointSize=\"%.1lf<sma11caseFont>\"", nweight];
            [code replaceCharactersInRange:exp.range withString:nline];
            MLog(@"%@ => %@", line, nline);
            BreakPointHere;
        }while (fonts.count);
        
        [code replaceOccurrencesOfString:@"<sma11caseFont>" withString:@"" options:0 range:NSMakeRange(0, code.length)];
        [code writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:NULL];
        
        BreakPointHere;
        return YES;
    }];
}

推荐阅读更多精彩内容