UISS 1.1.2

UISS 1.1.2

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年9月

Robert Wijas维护。



UISS 1.1.2

  • 作者
  • Robert Wijas

什么是 UISS?

UISS代表UIKit样式表。

UISS 是一个iOS库,它提供一个方便的方式来定义您应用程序的样式。UISS 构建在UIKit UIAppearance代理之上。

UISS能为您做什么?

UISS有强大的功能

  • 用简单的JSON字典替换不可阅读的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.xcodeproj 添加到您的项目中
  • (可选)将 UISSResource.bundle 添加到您的目标中

配置UISS

本地

开始使用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状态栏将显示控制台视图,其中

  • 您可以获取有关您的样式中错误的详细信息
  • 可以为您的设计生成UIAppearance代码

语法

坐标参数

包含

转换器

UISS为用于设置UIAppearance属性的所有类型提供了值转换器。这些转换器提供了定义您的属性时的有用快捷方式和方便的语法。

以下是一些示例和等价的 Objective-C 代码中的值。

颜色

十六进制

"#ffffff"
[UIColor colorWithRed:255.0f green:255.0f blue:255.0f alpha:1]

默认 UIColor 颜色

"red"
[UIColor redColor]
"redColor"
[UIColor redColor]

带图案图的颜色

"patternImageName"
[UIColor colorWithPatternImage:@"patternImageName"]

RGB

[0, 255, 255]
[UIColor colorWithRed:0.0f green:255.0f blue:255.0f alpha:1.0f]

带Alpha的颜色

["#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];

结构

CGSize

JSON Objective-C
1 CGSizeMake(1, 1)
[1] CGSizeMake(1, 1)
[1, 2] CGSizeMake(1, 2)

CGRect

JSON Objective-C
[1, 2, 3, 4] CGRectMake(1, 2, 3, 4)

UIEdgeInsets

JSON Objective-C
[1, 2, 3, 4] UIEdgeInsetsMake(1, 2, 3, 4)

UIOffset

JSON Objective-C
1 UIOffsetMake(1, 1)
[1] UIOffsetMake(1, 1)
[1, 2] UIOffsetMake(1, 2)

CGPoint

JSON Objective-C
1 CGPointMake(1, 1)
[1] CGPointMake(1, 1)
[1, 2] CGPointMake(1, 2)

UIKit 枚举

UIBarMetrics

JSON Objective-C
默认 UIBarMetricsDefault
横屏电话 UIBarMetricsLandscapePhone

UIControlState

JSON Objective-C
正常 UIControlStateNormal
高亮显示 UIControlStateHighlighted
禁用 UIControlStateDisabled
选中 UIControlStateSelected
保留 UIControlStateReserved
应用程序 UIControlStateApplication

UISegmentedControlSegment

JSON Objective-C
任何 UISegmentedControlSegmentAny
UISegmentedControlSegmentLeft
中心 UISegmentedControlSegmentCenter
UISegmentedControlSegmentRight
单独 UISegmentedControlSegmentAlone

UIToolbar位置

JSON Objective-C
任何 UIToolbar位置任意
底部 UIToolbar位置底部
顶部 UIToolbar位置顶部

UISearchBar图标

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的背景图片。