美文网首页
iOS主题皮肤和字体内容的动态切换方案

iOS主题皮肤和字体内容的动态切换方案

作者: ireliaBR | 来源:发表于2018-05-09 09:35 被阅读0次

目录

简述

主题皮肤和字体内容的动态切换方案

您的star,是我前进的动力 😊
MTTheme地址

模块功能

  • 通过 MTThemeManager 设置主题路径,可以动态的进行主题切换
  • 通过 MTFontManager 设置文字路径,可以动态的进行文字的切换, 类似国际化

效果图

MTTheme效果图

设计结构

文件结构

文件结构
  • Categorys文件夹下的类是颜色、图片、内容赋值时所需的分类。
  • Core 文件夹下 Font 放了字体内容管理类,Theme 放了主题内容的管理类

类图结构

类图

MTFontManager :

  • 设置文字路径并解析 font.plist
  • 通过 addView:identifier:extendObj:selector:type:moduleName 注册视图,当字体变化时,重新设置已经注册的视图的字体内容
  • 通过 getTextWithModuleName:identifier: 获取对应的字体内容

MTThemeManager :

  • 设置主题路径并解析 color.plist 和图片列表
  • 通过 addView:identifier:extendObj:selector:type:moduleName 注册视图,当主题变化时,重新设置已经注册的视图的图片和颜色
  • 通过 getColorWithModuleName:identifier: 获取对应的颜色值
  • 通过 getImageWithModuleName:identifier: 获取对应的图片

MTFontTable :

  • 管理 MTElementModel 列表,并刷新字体内容列表数据

MTThemeTable :

  • 管理 MTElementModel 列表,并刷新图片颜色列表数据

MTElementModel :

  • 存储配置视图时,视图的 selector, identifier, extendObj, view, type

MTBaseTable :

  • MTThemeTable MTFontTable 的父类

MTThemePerformSelector :

  • 通过 performSelector:view:object:extendObj: 动态的执行赋值方法

MTThemeBlock :

  • 视图的 Category 通过此类与管理类交互
  • colorPickerWithView:selector:moduleName:identifier:extendObj: 视图的color设置
  • imagePickerWithView:selector:moduleName:identifier:extendObj: 视图的image设置
  • textPickerWithView:selector:moduleName:identifier:extendObj: 视图的text设置

使用方式

  1. pod导入
    pod 'MTTheme'
  1. 初始化
    //主题模块初始化
    NSString *path = [NSBundle mainBundle].bundlePath;
    NSString *themePath = [path stringByAppendingPathComponent:@"Theme/Theme1"];
    [MTThemeManager initializeWithDefaultThemePath:themePath];
    
    //字体模块初始化
    NSString *fontPath = [path stringByAppendingPathComponent:@"Font/Font2"];
    [MTFontManager initializeWithDefaultFontPath:fontPath];
  1. 视图注册颜色和图片
    [self.label theme_setBackgroundColorIdentifier:@"label.background"
                                        moduleName:@"homepage"];
    [self.label theme_setTextColorIdentifier:@"label.text"
                                  moduleName:@"homepage"];
    
    [self.colorBtn theme_setTitleColorIdentifier:@"colorBtn.title.normal"
                                        forState:UIControlStateNormal
                                      moduleName:@"homepage"];
    [self.colorBtn theme_setTitleColorIdentifier:@"colorBtn.title.highlighted"
                                        forState:UIControlStateHighlighted
                                      moduleName:@"homepage"];
    
    [self.bgColorView theme_setBackgroundColorIdentifier:@"bgColorView.background"
                                              moduleName:@"homepage"];
    
    [self.imageBtn theme_setImageIdentifier:@"icon_homepage_image"
                                   forState:UIControlStateNormal
                                 moduleName:@"homepage"];
    
    [self.bgImageBtn theme_setBackgroundImageIdentifier:@"icon_homepage_image"
                                               forState:UIControlStateNormal
                                             moduleName:@"homepage"];
    
    [self.textField theme_setTextColorIdentifier:@"textField.textColor"
                                      moduleName:@"homepage"];
    
    [self.imageView theme_setImageIdentifier:@"icon_homepage_image"
                                  moduleName:@"homepage"];
  1. 主题皮肤的切换
    NSString *path = [NSBundle mainBundle].bundlePath;
    path = [path stringByAppendingPathComponent:@"Theme"];
    path = [path stringByAppendingPathComponent:@"Theme2"];
    [MTThemeManager.manager setThemePath:path];
  1. 视图注册字体内容
    [self.label theme_setTextIdentifier:@"FirstViewController.title.text"
                             moduleName:@"homepage"];
    [self.colorBtn theme_setTitleIdentifier:@"FirstViewController.colorBtn.text.normal"
                                   forState:UIControlStateNormal
                                 moduleName:@"homepage"];
    [self.colorBtn theme_setTitleIdentifier:@"FirstViewController.colorBtn.text.highlighted"
                                   forState:UIControlStateHighlighted
                                 moduleName:@"homepage"];
  1. 字体内容的切换
    NSString *path = [NSBundle mainBundle].bundlePath;
    path = [path stringByAppendingPathComponent:@"Font"];
    path = [path stringByAppendingPathComponent:@"Font1"];
    [MTFontManager.manager setFontPath:path];

加载性能评测

  1. 环境:
    设备:iphone6 plus
    系统:iOS 11.3
  1. 在Font2的 homepage.plist 下添加了1500条记录,在Theme2下 color.plist 添加了1500条记录和3000张图片

  2. 通过添加 CFAbsoluteTimeGetCurrent() 计算方法运行时间

    //主题模块初始化
    CFAbsoluteTime themeStartTime = CFAbsoluteTimeGetCurrent();
    NSString *path = [NSBundle mainBundle].bundlePath;
    NSString *themePath = [path stringByAppendingPathComponent:@"Theme/Theme2"];
    [MTThemeManager initializeWithDefaultThemePath:themePath];
    CFAbsoluteTime themeEndTime = CFAbsoluteTimeGetCurrent();
    MTTheme_Log(@"[During]主题注册事件 during in %f seconds.", (themeStartTime - themeEndTime));
    
    //字体模块初始化
    CFAbsoluteTime fontStartTime = CFAbsoluteTimeGetCurrent();
    NSString *fontPath = [path stringByAppendingPathComponent:@"Font/Font2"];
    [MTFontManager initializeWithDefaultFontPath:fontPath];
    CFAbsoluteTime fontEndTime = CFAbsoluteTimeGetCurrent();
    MTTheme_Log(@"[During]字体注册事件 during in %f seconds.", (fontStartTime - fontEndTime));
  1. 测试结果
  • 第一次
[During]主题注册事件 during in -0.355088 seconds.
[During]字体注册事件 during in -0.023626 seconds.
  • 第二次
[During]主题注册事件 during in -0.350668 seconds.
[During]字体注册事件 during in -0.023880 seconds.
  • 第三次
[During]主题注册事件 during in -0.358335 seconds.
[During]字体注册事件 during in -0.026189 seconds.
  1. 结论
  • 在加载字体内容时,只需要加载 plist 文件,稳定在23毫秒
  • 在加载主题时,不仅需要加载 plist 文件,还需要遍历列表生成image对应的列表
  • 在遍历图片列表时,耗时太长

优化

  • MTThemeMangerMTFontManager 添加了定时器,每10秒会对每个模块的 ElementModelsviewnil 的对象进行清空。
  • 每次改变主题时也会对对每个模块的 ElementModelsviewnil 的对象进行清空

后续优化

  • 加载主题数据时由于图片数量太多,可以采用延迟加载、分模块加载
  • 图片的 contentMode 动态配置

相关文章

  • iOS主题皮肤和字体内容的动态切换方案

    目录 简述模块功能效果图 设计结构文件结构类图结构 使用方式 加载性能评测 优化 后续优化 简述 主题皮肤和字体内...

  • iOS皮肤切换方案

    iOS皮肤切换方案 iOS皮肤切换方案

  • iOS开发之App主题切换完整解决方案(Swift版)

    iOS开发之App主题切换完整解决方案(Swift版) iOS开发之App主题切换完整解决方案(Swift版)

  • iOS动态切换字体

    1、做阅读APP,现在需要添加不同的阅读字体 如果字体过多,并不适宜打包到APP项目中,会增加包体积。将字体下载到...

  • iOS皮肤切换方案

    切换皮肤,换的是什么? 颜色,包括但不限于导航栏颜色,字体颜色,其他颜色 图片,包括但不限于图标,背景图片,tab...

  • iOS皮肤切换方案

    切换皮肤,换的是什么? 1.颜色,包括但不限于导航栏颜色,字体颜色,其他颜色2.图片,包括但不限于图标,背景图片,...

  • iOS 动态加载字体

    iOS动态加载字体有两种方案 1.加载系统自带字体, 虽然叫系统自带字体, 但还是需要通过网络下载. 系统自带字体...

  • 精简字体文件

    使用自定义字体,可以丰富页面的显示效果。用作主题的字体可以动态下载,但是框架性的字体,内置字体文件还是最好的方案,...

  • iOS控件---UI适配器切换主题

    Demo 一、需求: 1、一键切换APP的主题 二、思路: 主题所包含的内容有:字体、字体大小、颜色、图片,所以需...

  • Cocos Creator小细节

    代码中动态切换字体 本地存储

网友评论

      本文标题:iOS主题皮肤和字体内容的动态切换方案

      本文链接:https://www.haomeiwen.com/subject/lixnrftx.html