UISS代表UIKit样式表。
UISS 是一个iOS库,它提供一个方便的方式来定义您应用程序的样式。UISS 构建在UIKit UIAppearance代理之上。
UISS有强大的功能
UISS 不对您的应用程序强制任何依赖。您可以生成用于UISS样式的Objective-C代码,因此在生产构建中您甚至无需链接到UISS库。
假设您熟悉UIAppearance代理,您可能已经编写了类似以下的代码
[[UITabBar appearance] setTintColor:[UIColor darkGrayColor]]
在UISS中,它看起来像这样
{
"UITabBar": {
"tintColor": "darkGray",
}
}
这里没有太大的区别,所以让我们看看更复杂的示例
[[UIButton appearance] setTitleColor:[[UIColor whiteColor] colorWithAlphaComponent:0.800]
forState:UIControlStateNormal];
[[UIButton appearance] setTitleColor:[UIColor whiteColor]
forState:UIControlStateHighlighted];
[[UIButton appearance] setBackgroundImage:[[UIImage imageNamed:@"button-background-normal"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0)]
forState:UIControlStateNormal];
[[UIButton appearance] setBackgroundImage:[[UIImage imageNamed:@"button-background-highlighted"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0)]
forState:UIControlStateHighlighted];
[[UILabel appearanceWhenContainedIn:[UIButton class], nil] setFont:[UIFont fontWithName:@"Copperplate-Bold" size:18.0]];
[[UIButton appearance] setTitleEdgeInsets:UIEdgeInsetsMake(1.0, 0.0, 0.0, 0.0)];
{
"UIButton":{
"titleColor:normal":["white", 0.8],
"titleColor:highlighted":"white",
"backgroundImage:normal": ["button-background-normal", [0,10,0,10]],
"backgroundImage:highlighted": ["button-background-highlighted", [0,10,0,10]],
"titleEdgeInsets": [1,0,0,0],
"UILabel":{
"font":["Copperplate-Bold", 18]
}
}
}
开始使用UISS的最简单方法是创建一个uiss.json
文件并将其添加到项目资源中。为了激活该文件,添加以下行
[UISS configureWithDefaultJSONFile];
这应该在您的视图显示之前调用,通常是在应用程序代理的didFinishLaunching方法中。
如果您想从远程位置加载样式以启用实时更新,请按照以下步骤操作
self.uiss = [UISS configureWithURL:[NSURL URLWithString:@"https:///uiss.json"]];
UISS 可以检测样式是否更改,并自动更新您的界面。要启用此功能,请调用此方法
uiss.autoReloadEnabled = YES;
uiss.autoReloadTimeInterval = 1;
uiss.statusWindowEnabled = YES;
UISS 将占用屏幕的一小部分(通常是状态栏所占的区域)来显示幕后发生的事情。
点击UISS状态栏将显示控制台视图,其中
UISS为用于设置UIAppearance属性的所有类型提供了值转换器。这些转换器提供了定义您的属性时的有用快捷方式和方便的语法。
以下是一些示例和等价的 Objective-C 代码中的值。
"#ffffff"
[UIColor colorWithRed:255.0f green:255.0f blue:255.0f alpha:1]
"red"
[UIColor redColor]
"redColor"
[UIColor redColor]
"patternImageName"
[UIColor colorWithPatternImage:@"patternImageName"]
[0, 255, 255]
[UIColor colorWithRed:0.0f green:255.0f blue:255.0f alpha:1.0f]
["#ffffff", 0.5]
[UIColor colorWithRed:255.0f green:255.0f blue:255.0f alpha:.0.5f]
["red", 0.5]
[[UIColor redColor] colorWithAlphaComponent:0.5f]
[0, 255, 255, 0.5]
[UIColor colorWithRed:0.0f green:255.0f blue:255.0f alpha:.0.5f]
"imageName"
[UIImage imageNamed:@"imageName"]
["imageName", 1, 2, 3, 4]
[[UIImage imageNamed:@"imageName"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 2, 3, 4)]
14
[UIFont systemFontOfSize:14.0f]
["bold", 14]
[UIFont boldSystemFontOfSize:14.0f]
["italic", 20]
[UIFont italicSystemFontOfSize:14.0f]
["Georgia-Italic", 12]
[UIFont fontWithName:@"Georgia-Italic" size:12.0f]
{
"font": ["bold", 12],
"textColor": "black",
"textShadowColor": "lightGray",
"textShadowOffset": [1, 2]
}
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:12], UITextAttributeFont,
[UIColor blackColor], UITextAttributeTextColor,
[UIColor lightGrayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(1, 2)], UITextAttributeTextShadowOffset, nil];
JSON | Objective-C |
---|---|
1 |
CGSizeMake(1, 1) |
[1] |
CGSizeMake(1, 1) |
[1, 2] |
CGSizeMake(1, 2) |
JSON | Objective-C |
---|---|
[1, 2, 3, 4] |
CGRectMake(1, 2, 3, 4) |
JSON | Objective-C |
---|---|
[1, 2, 3, 4] |
UIEdgeInsetsMake(1, 2, 3, 4) |
JSON | Objective-C |
---|---|
1 |
UIOffsetMake(1, 1) |
[1] |
UIOffsetMake(1, 1) |
[1, 2] |
UIOffsetMake(1, 2) |
JSON | Objective-C |
---|---|
1 |
CGPointMake(1, 1) |
[1] |
CGPointMake(1, 1) |
[1, 2] |
CGPointMake(1, 2) |
JSON | Objective-C |
---|---|
默认 |
UIBarMetricsDefault |
横屏电话 |
UIBarMetricsLandscapePhone |
JSON | Objective-C |
---|---|
正常 |
UIControlStateNormal |
高亮显示 |
UIControlStateHighlighted |
禁用 |
UIControlStateDisabled |
选中 |
UIControlStateSelected |
保留 |
UIControlStateReserved |
应用程序 |
UIControlStateApplication |
JSON | Objective-C |
---|---|
任何 |
UISegmentedControlSegmentAny |
左 |
UISegmentedControlSegmentLeft |
中心 |
UISegmentedControlSegmentCenter |
右 |
UISegmentedControlSegmentRight |
单独 |
UISegmentedControlSegmentAlone |
JSON | Objective-C |
---|---|
任何 |
UIToolbar位置任意 |
底部 |
UIToolbar位置底部 |
顶部 |
UIToolbar位置顶部 |
JSON | Objective-C |
---|---|
搜索 |
UISearchBarIcon搜索 |
清除 |
UISearchBarIcon清除 |
书签 |
UISearchBarIcon书签 |
结果列表 |
UISearchBarIcon结果列表 |
您可以在UISS风格中定义可以用到的变量。所有变量都应该在您的样式字典中的变量键下定义。引用变量时,请在其名称前加上$符号。
示例
{
"Variables": {
"tintColor": "red"
},
"UIToolbar": {
"tintColor": "$tintColor"
}
}
JSON规范不支持注释。但有时轻松禁用UISS风格的某些部分可能非常有用。您可以通过给字典键添加-
前缀来完成此操作。UISS将忽略这些键而不会报告错误。
示例
{
"UIToolbar": {
"-tintColor": "blue",
"backgroundImage:any:default": "background"
},
"-UITabbar": {
"tintColor": "blue"
}
}
这只会设置UIToolbar的背景图片。