JDTheme
先上图,本demo数据模拟,样式和图片全部来自配置文件
下面是demo效果图
下面是demo中的配置属性以及使用图
使用步骤
1、集成 SDK,支持 CocoaPods
2、在 AppDelegate 中通过 [[JDThemeManager sharedInstance] setBundle:bundle]; 设置一个包含资源的 bundle,当然,该 bundle 的来源无关紧要,您可以网络下载也可以是 mainBundle。
3、给您的控件设置 jd_themeID 属性(可以通过代码设置,也可以直接在 xib 中设置),jd_themeID 是您 bundle 中的一组配置 ID
4、享受吧
如果您想更改样式,需要 [[JDThemeManager sharedInstance] changeBundle:bundle]; 通过这样做来更改 bundle。
现有支持的属性:
各控件支持的属性
UIView {
backgroundColor : '#fff',
borderColor : '#cecece',
borderWidth : 1,
cornerRadius : 1,
tintColor : '#cececece',
frame : { {0,0},{100,100} },
visible : 1,
opacity : 0.2,
center : {100,100},
left : 0,
top: 0,
right : 0,
bottom : 0,
width : 10,
height: 10,
centerX : 1,
centerY : 1
}
UIButton {
text : 'text',
textColor : '#fffff',
selectedTextColor : '#000',
image : 'imageName',
selectedImage : 'imageName',
font : {
size : 14,
name : '',
bold : YES,
italic : YES
}
numberOfLines : 0,
lineBreakMode : 0 ,
backgroundImage : 'imageName',
selectedBackgroundImage : 'imageName',
textShadowColor : '#ccc',
textShadowOffset : {100,100}
}
UILabel {
text : 'text' ,
textColor : '#fff',
highlightedTextColor : '#ccc',
font : {},
adjustsFontSize : 10,
baselineAdjustment : 0,
textAlignment : 0,
numberOfLines : 0,
lineBreakMode : 0
}
UITabBar {
backgroundColor : '#fff',
barTintColor : ''#fff,
translucent : NO,
}
UITextView {
text : 'text',
textColor : '#ccc',
font : {},
textAlignment : 0,
editable : YES
}
UITextField {
text : 'text',
textColor : '#ccc',
font : {},
textAlignment : 0,
placeholder : 'placeholder'
}
UITableView {
separatorColor : '#fff'
}
UITabBarItem {
text : 'text',
image : 'imageName',
selectedImage : 'imageName'
}
UISwitch {
image : 'imageName'
selectedImage : 'imageName'
checked : YES
}
UIPageControl {
textColor : '#ffffff',
selectedTextColor : '#cecccc'
}
UINavigationBar {
barTintColor : '#fff',
translucent : NO,
backgroundImage : 'imageName',
shadowImage : 'imageName',
statusBarStyle : 0
}
UIImageView {
image : 'imageName'
}
UIBarItem {
text : 'text',
image : 'imageName',
enabled : NO
}
从上面可以看出,不仅可以定制 image 和颜色,连文字、font、边框等都可以定制,甚至还支持对约束设置。(提示:父类拥有的子类也拥有)
支持换肤
基础功能,首页它主要为换肤服务
支持设置约束
它可以随意更改位置,在换肤的基础上,可以控制控件的位置
支持常规属性
支持设置控件的常规属性,如 label 的 numberOfLines、lineBreakMode 等。
支持自定义控件
当然这些是系统的控件和属性,还可以支持自定义控件和属性,只需要你的类重写 jud_applyThemeWithRuleSet 即可,拿到 JDRuleSet 你想怎么渲染就怎么渲染。
支持多文件
可以看出key的规则是keyPath,它是区分文件的,懒加载模式极大提高大规模使用能力,避免配置文件过大加载很慢。
支持全局样式
你可以通过 [[JDStyleable sharedInstance] setGlobalStyleableName:@"Global"] 来设置一份全局的样式文件。
支持XIB和代码
xib 需要在 attribute 栏目设置 Jd themeID,代码只需要 view.jd_themeID = @"id" 即可。
支持各种格式的样式文件
默认是解析 plist,通过 JDStyleable 的 setStyleableParser 方法,可以自定义解析器,你想解析 json 格式的,css 格式的都随你。
如下:
[[JDStyleable sharedInstance] setStyleableParser:^NSDictionary *(NSString *fileName) {
NSURL *url = [[JDThemeManager sharedInstance].bundle URLForResource:fileName withExtension:@"json"];
return [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:url] options:kNilOptions error:nil];
}];
CocoaPods
1、在 Podfile 中添加 pod 'JDTheme'
。
2、执行 pod install
或 pod update
。
3、导入 <JDTheme/JDTheme.h>。