TapThemeManager-iOS
这是一个 SDK,它提供了一个接口,以实现不同主题之间响应式的 iOS 应用程序主题。
要求
要使用 SDK,必须满足以下要求:
- Xcode 11.0 或更高版本
- Swift 4.0 或更高版本(由 Xcode 预先安装)
- 应用的部署目标 SDK:iOS 12.0 或更高版本
安装
使用 CocoaPods 进行安装
CocoaPods 是一个依赖管理器,它可以自动简化您项目中使用第三方库的过程。您可以使用以下命令安装它
$ gem install cocoapods
Podfile
要使用 CocoaPods 将 goSellSDK 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
platform :ios, '12.0'
use_frameworks!
source 'https://github.com/CocoaPods/Specs.git'
target 'MyApp' do
pod 'TapThemeManager2020'
end
然后,运行以下命令
$ pod update
Setup
首先,需要通过提供主题本身来设置 TapThememanager
。这可以是 Plist 或 JSON 文件,也可以是一个给定的主题字典。
设置当前主题
以下是您可以将特定主题文件附加到共享 ThemeManager 的不同方式以及如何在运行时更改主题。
JSON 文件
确保您的项目主包中有 JSON 文件。
Swift:
TapThemeManager.setTapTheme(jsonName: "MyThemeFile")
Objective-C:
[TapThemeManager setTapThemeWithJsonName:@"MyThemeFile"];
PLIST 文件
确保您的项目中主包中的 PLIST 文件。
Swift:
TapThemeManager.setTapTheme(plistName: "MyThemeFile")
Objective-C:
[TapThemeManager setTapThemeWithPlistName:@"MyThemeFile"];
主题字典
Swift:
TapThemeManager.setTapTheme(themeDict: ["MyLabelFontSize":22])
Objective-C:
[TapThemeManager setTapThemeWithThemeDict:@{@"MyLabelFontSize":@(22)}];
不要忘记在文件开头导入框架
Swift:
import TapThemeManager2020
Objective-C:
@import TapThemeManager2020;
或者
#import <TapThemeManager2020/TapThemeManager2020.h>
主题接受值
如设置中所述,SDK 需要一个主题(文件或对象),SDK 会将主题中提供的值与 UIKit 子类的 UI 属性进行匹配。以下将说明主题(文件或对象)中可接受值范围。
类型 | 用途 | 接受格式 | 示例 |
---|---|---|---|
十六进制颜色 | 当您需要改变接受 UIColor 或 CGColor 的属性时,例如 UIView.backgroundColor 或 UIBar.barTintColor | 3, 4, 6, 8 位十六进制颜色代码 | "#RGB", "#RGBA", "#RRGGBB", "#RRGGBBAA" |
数字 | 当您需要改变接受 CGFloat 或 Double 的属性时,例如 UIView.alpha 或 UIVIew.layer.borderWidth | 正浮点数 | 0, 0.3, 5, 10.2 |
字体 | 当您需要改变接受 UIFont 的属性时,例如 UILabel.font | "Font FamilyName, Size" 或 "Size" | "FontFamilyName,23" -> 使用给定字体名和大小创建字体,"23" -> 使用默认字体和给定的大小创建字体 |
UIBarStyle | 当您需要改变接受 UIBarStyle 的属性时,例如 UIBarStyle.barStyle | "default","black","blackTranslucent" | |
UIImage | 当您需要改变接受 UIImage 的属性时,例如 UIImageView.image | 字符串表示要应用的图像名称 | "MyImage" |
键盘外观 | 当您需要改变接受 UIKeyboardAppearance 的属性时 | "default","dark","light","alert" | |
导航栏外观 | 当您需要改变接受 UINavigationBarAppearance 的属性时。需要 iOS 13+ | 标题文本属性 大文本属性 紧凑外观 |
|
滚动视图指示器 | 当您需要改变接受 UIScrollView.IndicatorStyle 的属性时 | "default" "black" "white" |
|
状态栏样式 | 当你需要更改接受UIStatusBarStyle属性时 | "default" "lightcontent" "darkcontent" 需要iOS 13及以上 |
|
活力模糊 | 当你需要更改接受UIVisualEffect属性时 | "dark" "light" "extralight" "prominent" 需要iOS 10及以上 "regular" 需要iOS 10及以上 |
|
字符串属性 | 当你需要更改接受NSAttributedString属性时 | 一个具有所需属性(如字体、颜色等)的字典,使用之前给出的值 |
主题化属性
SDK提供了针对UIKit各个子类的高级接口,以便轻松地将主题值应用到UIKit子类中对应的属性。以下是一些SDK支持的不同UIView的属性。 要知道SDK支持的属性,对于任何UIView,只需输入.tab_theme,所有可用的方法都将显示
UIView
SDK使用方法 | UIKit选择器应用 | 接受的主题值 | 示例主题值 |
---|---|---|---|
UIView.tap_theme_alpha | setAlpha | 浮点数 | 0.4 |
UIView.tap_theme_backgroundColor | setBackgroundColor | 十六进制颜色 | "#FF00FF" |
UIView.tap_theme_tintColor | setAlpha | 十六进制颜色 | "#FF00FF" |
UIApplication
SDK使用方法 | UIKit选择器应用 | 接受的主题值 | 示例主题值 |
---|---|---|---|
UIApplication.tap_theme_setStatusBarStyle | setStatusBarStyle:animated | 状态栏样式 | "default" |
UIBarItem
SDK使用方法 | UIKit选择器应用 | 接受的主题值 | 示例主题值 |
---|---|---|---|
UIBarItem.点击主题图片 | 设置图像 | UIImage | "我的图像名称" |
UIBarItem.点击主题设置标题文本属性 | 设置标题文本属性:对于状态 | 字符串属性 | "导航标题": { |
UIBarButtonItem
SDK使用方法 | UIKit选择器应用 | 接受的主题值 | 示例主题值 |
---|---|---|---|
UIBarButtonItem.点击主题颜色 | 设置颜色 | 十六进制颜色 | "#FF00FF" |
UILabel
SDK使用方法 | UIKit选择器应用 | 接受的主题值 | 示例主题值 |
---|---|---|---|
UILabel.点击主题字体 | 设置字体 | 字体 | "我的字体名称,24" |
UILabel.点击主题文本颜色 | 设置文本颜色 | 十六进制颜色 | "#FF00FF" |
UILabel.点击主题高亮文本颜色 | 设置文本颜色 | 十六进制颜色 | "#FF00FF" |
UILabel.点击主题阴影颜色 | 设置阴影颜色 | 十六进制颜色 | "#FF00FF" |
UILabel.点击主题文本属性 | 设置标题文本属性:对于状态 | 字符串属性 | "导航标题": { |
UINavigationBar
SDK使用方法 | UIKit选择器应用 | 接受的主题值 | 示例主题值 |
---|---|---|---|
UINavigationBar.点击主题栏风格 | 设置栏风格 | 栏风格 | "default" |
UINavigationBar.点击主题栏颜色 | 设置栏颜色 | 十六进制颜色 | "#FF00FF" |
UINavigationBar.点击主题标题文本属性 | 设置标题文本属性 | 字符串属性 | "导航标题": { |
UINavigationBar.点击主题大标题文本属性 | 设置标题文本属性 | 字符串属性 | "导航标题": { |
UITabBar
SDK使用方法 | UIKit选择器应用 | 接受的主题值 | 示例主题值 |
---|---|---|---|
UITabBar的事件:主题栏样式 | 设置栏风格 | 栏风格 | "default" |
UITabBar的事件:未选中项的字体颜色 | 设置未选中项的字体颜色 | 十六进制颜色 | "#FF00FF" |
UITabBar的事件:主题栏颜色 | 设置栏颜色 | 十六进制颜色 | "#FF00FF" |
UITabBarItem
SDK使用方法 | UIKit选择器应用 | 接受的主题值 | 示例主题值 |
---|---|---|---|
UITabBarItem的事件:主题栏选中项的图片 | 设置选中项的图片 | UIImage | "MyImage" |
UITableView
SDK使用方法 | UIKit选择器应用 | 接受的主题值 | 示例主题值 |
---|---|---|---|
UITableView的事件:分隔符颜色 | 设置分隔符颜色 | 十六进制颜色 | "#FF00FF" |
UITableView的事件:分区索引颜色 | 设置分区索引颜色 | 十六进制颜色 | "#FF00FF" |
UITableView的事件:分区索引背景颜色 | 设置分区索引背景颜色 | 十六进制颜色 | "#FF00FF" |
UITextField
SDK使用方法 | UIKit选择器应用 | 接受的主题值 | 示例主题值 |
---|---|---|---|
UITextField的事件:主题字体 | 设置字体 | UIFont | "我的字体,22" |
UITextField的事件:键盘外观 | 设置键盘外观 | 键盘外观 | "default" |
UITextField的事件:文本颜色 | 设置文本颜色 | 十六进制颜色 | "#FF00FF" |
UITextField的事件:占位符属性 | 使用选择器设置占位符属性 | 字符串属性 | "导航标题": { |
UITextView
SDK使用方法 | UIKit选择器应用 | 接受的主题值 | 示例主题值 |
---|---|---|---|
UITextView的事件:主题字体 | 设置字体 | UIFont | "我的字体,22" |
UITextView的事件:主题键盘外观 | 设置键盘外观 | 键盘外观 | "default" |
UITextView的事件:主题文本颜色 | 设置文本颜色 | 十六进制颜色 | "#FF00FF" |
主题文件示例
在此处,我们提供了一个示例主题文件,以及如何访问文件中的不同值并与其对应的 UIView 匹配的方法。
示例主题文件
{
"CustomButton": {
"buttonTitleColorHighlighted": "#555",
"buttonTitleColorNormal": "#FFFFFF",
"buttonBackgroundColor": "#ABA6BF"
},
"CustomView1": {
"backgroundColor": "#36688D",
"borderColor": "#F3CD05",
"cornerRadius":4,
"borderWidth": 2
},
"CustomLabel1": {
"textColor": "#F3CD05",
"font":"24"
},
"CustomView2": {
"backgroundColor": "#F49F05",
"borderColor": "#F18904",
"cornerRadius":10,
"borderWidth": 1
},
"CustomLabel2": {
"textColor": "#F18904",
"font":"24"
},
"CustomSwitch2": {
"onTint": "#F18904",
"thumbTint":"#36688D"
},
"CustomView3": {
"backgroundColor": "#BDA589",
},
"CustomLabel3": {
"textColor": "#FFFFFF",
"font":"24"
},
"CustomUIImageView3": {
"image": "icon_theme_1"
},
"Global": {
"UIStatusBarStyle": "LightContent",
"NavigationTint": "#FF0000",
"NavigationTitle": {
"NavigationTitleColor": "#FFFFFF",
"Font":"24"
}
}
}
应用主题值代码示例
Swift
// Apply background color for UIView
customView1.tap_theme_backgroundColor = "CustomView1.backgroundColor"
// Apply border color for UIView
customView1.layer.tap_theme_borderColor = "CustomView1.borderColor"
// Apply text color for UILabel
customLabel1.tap_theme_textColor = "CustomLabel1.textColor"
// Apply title color for UIButton in normal state
customButton.tap_theme_setTitleColor(selector: "CustomButton.buttonTitleColorNormal", forState: .normal)
// Apply text attributes
// 1- Fetch the main directory that has the attributes
self.navigationBar.tap_theme_titleTextAttributes = ThemeStringAttributesSelector(keyPath: "Global.NavigationTitle", map: { (entry) -> [NSAttributedString.Key : Any]? in
// 2- Returns an array of all needed attributes
return [NSAttributedString.Key.foregroundColor:TapThemeManager.colorValue(for: "Global.NavigationTitle.NavigationTitleColor")!,
NSAttributedString.Key.font:TapThemeManager.fontValue(for: "Global.NavigationTitle.Font")!]
})
Objective-C
// Apply background color for UIView
_CustomView1.tap_theme_backgroundColor = [ThemeUIColorSelector selectorWithKeyPath:@"CustomView1.backgroundColor"];
// Apply border color for UIView
_CustomView1.layer.tap_theme_borderColor = [ThemeCgColorSelector selectorWithKeyPath:@"CustomView1.borderColor"];
// Apply text color for UILabel
_customLabel1.tap_theme_textColor = [ThemeUIColorSelector selectorWithKeyPath:@"CustomLabel1.textColor"];
// Apply title color for UIButton in normal state
[_customButton tap_theme_setTitleColorWithSelector:[ThemeUIColorSelector selectorWithKeyPath:@"CustomButton.buttonTitleColorNormal"] forState:UIControlStateNormal];
// Apply text attributes
// 1- Fetch the main directory that has the attributes
self.navigationBar.tap_theme_titleTextAttributes = [ThemeStringAttributesSelector selectorWithKeyPath:@"Global.NavigationTitle" map:^NSDictionary<NSAttributedStringKey,id> * _Nullable(id entry) {
// 2- Returns an array of all needed attributes
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
[attributesDictionary setObject:[TapThemeManager fontValueFor:@"Global.NavigationTitle.Font"] forKey:NSFontAttributeName];
[attributesDictionary setObject:[TapThemeManager colorValueFor:@"Global.NavigationTitle.NavigationTitleColor"] forKey:NSForegroundColorAttributeName];
return attributesDictionary;
}];
}