UIView中一个相对来说比较小的属性,tintColor属性是相当的强大。今天我们就来看看如何使用他,包含使用tint color进行着色标准控件、我们自定义控件甚至重新着色图像。

本章的实例程序能够在github上面进行访问,访问地址:github.com/ShinobiControls/iOS7-day-by-day

Tint color of existing iOS controller – 使用tint color为iOS中已经存在的控件进行着色

在iOS7中,UIView新增了一个属性tintColor.这是一个UIColor,被使用在UIView中改变应用程序的外观的。默认tintColor的值为nil,这表示它将会运用父视图层次的颜色来进行着色。如果父视图中没有设置tintColor,那么默认系统就会使用蓝色。因此,可以通过设置root view controller的tintColor来改变系统整体的颜色。

为了证明这一点,我们需要知道tintColor如何改变标准控件的外观,可以看看我们已经写好的ColorChanger应用程序。

在storyboard中包含了一系列的控件,包括UIButtonUISliderUIStepper,在view controller中我们有一个改变颜色的按钮关联到下面的方法:

1
2
3
4
5
6
7
8
- (IBAction)changeColorHandler:(id)sender {
// Generate a random color
CGFloat hue = (arc4random() % 256 / 256.0);
CGFloat saturation = (arc4random() % 128 / 256.0) + 0.5;
CGFloat brightness = (arc4random() % 128 / 256.0) + 0.5;
UIColor *color = [UIColor colorWithHue:hue staturation:saturation brightness:brightnee alpha:1];
self.view.tintColor = color;
}

这个方法的主体就是生成一个随机的颜色, 最后一行设置tint color。

有一个UI control并不是响应tintColor的变化,它就是UIProgressView.是因为它具有两个tint colors(一个是设置进度条本身,一个设置进度轨道色的),为了能够改变,我们需要添加一个方法:

1
2
3
- (void)updateProgressViewTint {
self.progressView.progressTintColor = self.view.tintColor;
}

当调用changeColorHandler:方法之后:

Tint Dimming – 颜色变暗

除了能够设置色调颜色(tintColor),还有另外的一个属性在UIView中,它可以使tint color变暗, 因此整个视图层次变暗。这个属性是tintAdjustmentMode,并且它可以在三个值里面选择一个设置(UIViewTintAdjustmentModeNormalUIViewTintAdjustmentModeDimmedUIViewTintAdjustmentModeAutomatic)

为了展示这个效果,我们需要添加一个UISwitch控件并且绑定它的valueChanged:事件到下面的方法上面:

1
2
3
4
5
6
7
8
9
- (IBAction)dimTintHandler:(id)sender {
if (self.dimTintSwitch.isOn) {
self.view.tintAdjustMentMode = UIViewTintAdjustmentModeDimmed;
}else {
self.view.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
} [self updateProgressViewTint];
}

当你点击UISwitch之后,整个页面变暗了,这样的很方便进行弹出框的效果,展示弹出框的时候,后面的页面变暗,以免分散用户的注意力。

Using tint color in custom views – 给自定义发视图进行着色

UIView中有一个新的方法,当tintColor或者tintAdjustmentMode属性发生变化的时候就会调用这个方法。

为了查看它是如何工作的,我们需要创建一个UIView的子类,它包含一个整个的色块,一个标签的文本颜色和tint color相同,还有一个一直保持灰色的标签。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@implementation SCSampleCustomControl {
UIView *_tintColorBlock;
UILabel *_greyLabel;
UILabel *_tintColorLabel;
} - (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self prepareSubViews];
}
} - (void)prepareSubviews {
_tintColorBlock = [[UIView alloc] init];
_tintColorBlock.backgroundColor = self.tintColor;
[self addSubview:_titnColorBlock]; _greyLabel = [[UILabel alloc] init];
_greyLabel.text = @"Grey label";
_greyLabel.textColor = [UIColor grayColor];
[_greyLabel sizeToFit];
[self addSubview:_greyLabel]; _tintColorLabel = [[UILabel alloc] init];
_tintColorLabel.text = @"Tint color label";
_tintColorLabel.textColor = self.tintColor;
[_tintColorLabel sizeToFit];
[self addSubview:_tintColorLabel];
} - (void)layoutSubviews {
_tintColorBlock.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds) / 3, CGRectGetHeight(self.bounds));
CGRect frame = _greyLabel.frame;
frame.origin.x = CGRectGetWidth(self.bounds) / 3 + 10;
frame.origin.y = 0;
_greyLabel.frame = frame; frame = _tintColorLabel.frame;
frame.origin.x = CGRectGetWidth(self.bounds) / 3 + 10;
frame.origin.y = CGRectGetHeight(self.bounds) / 2;
_tintColorLabel.frame = frame;
} @end

到目前为止,我们都没有做什么新鲜的事情,只是搭建了一个简单的UIView的子类。有趣的内容现在就来了。我们复写一个新的方法tintColorDidChange:

1
2
3
4
- (void)tintColorDidChange {
_tintColorLabel.textColor = self.tintColor;
_tintColorBlock.backgroundColor = self.tintColor;
}

我们做的就是根据当前view的tintColor进行设置一些控件的颜色。

就是这么的简单。在view controller中的代码并不需要修改,因为设置tintColor只是在UIView层次中进行赋值。

Tinting images with tintColor – 给图像着色

最后有关tintColor比较cool的能力就是,它可以为一个图像着色。图像着色的时候会把所有像素alpha为1的全部变换成tintColor的颜色,其他的颜色就会设置成透明。

在这个demo中,我将会展示如何着色图片。

我们需要在storyboard中添加一个UIImageView,并且设置关联为tintedImageView,然后在viewDidLoad中添加如下代码:

1
2
3
4
5
6
7
// Load the image
UIImage *shinobiHead = [UIImage imageNamed:@"shinobihead"];
// Set the rendering mode to respect tint color
shinobiHead = [shinobiHead imageWithRenderingMode:UIImageRenderingModeAlwarysTemplate];
// And set to the image view
self.tintedImageView.image = shinobiHead;
self.tintedImageView.contentMode = UIViewContentModeScaleAspectFit;

首先我们加载一个图像,然后我们调用imageWithRenderingMode:方法来改变图像渲染模式为UIImageRenderingModeAlwaysTemplate,其他两个选项是UIImageRenderingModeAlwaysOriginalandUIImageRenderingModeAutomatic。默认是andUIImageRenderingModeAutomatic,在这个情况下,根据上下文来改变图像,例如标签栏、工具栏等都是设置成模板对象。

一旦你设置图像的模式为模板图像,并且设置比例,确保图不失真。

Conclusion – 总结

表面上tintColor很简单的UIView,然而,它实际上代表了一些令人难以置信的强大的外观定制功能。如果你创建自己的UIView子类或自定义控件,然后,我鼓励你,实现tintColorDidChange——它会让你的工作更多的与标准UIKit组件串联。

iOS tintColor解析的更多相关文章

  1. 【译】UI设计基础(UI Design Basics)--iOS应用解析(iOS App Anatomy)(三)

    2.1  iOS应用解析(iOS App Anatomy) 几乎所有的iOS应用都会用到UIKit框架中的组件.了解这些基础组件的名称,角色,功能可以帮你在应用界面设计时做出更好的决策. UIKit提 ...

  2. iOS中解析 XML / JSON

    JSON数据格式 1. 概述: JSON (JavaScript Object Notation) 是⼀一种轻量级的数据交换格式 基于⽂文本格式,易于⼈人阅读和编写,同时也易于机器解析和⽣生成. 2. ...

  3. IOS 原生解析JSON 问题

    服务器----WebService 返回的是JSON数据 IOS解析报错: Error Domain=NSCocoaErrorDomain Code=3840 "Unable to conv ...

  4. iOS 基础类解析 - NSDate

    版权声明:本文为博主原创文章,未经博主同意不得转载.转载联系 QQ 30952589,加好友请注明来意. https://blog.csdn.net/sleks/article/details/248 ...

  5. IOS 文件解析

    import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...

  6. iOS书摘之Objective-C编程之道 iOS设计模式解析

    来自<Objective-C编程之道iOS设计模式解析>一书的摘要总结 一.Prototype 原型模式 定义:使用原型实例指定创建对象的种类,并通过复制这个原型创建新的对象.(<设 ...

  7. iOS 基础类解析 - NSData、NSMutableData

    iOS 基础类解析 - NSData.NSMutableData 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致& ...

  8. iOS 基础类解析 - NSString、NSMutableString

    iOS 基础类解析 - NSString 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 ...

  9. 【读书笔记】iOS网络-解析响应负载

    Web Service可以通过多种格式返回结构化数据, 不过大多数时候使用的是XML与JSON.也可以让应用只接收HTML结构的数据.实现了这些Web Service或是接收HTML文档的应用必须能解 ...

随机推荐

  1. Swing做的非阻塞式仿飞秋聊天程序

    采用Swing 布局 NIO非阻塞式仿飞秋聊天程序, 切换皮肤颜色什么的小功能以后慢慢做 启动主程序. 当用户打开主程序后自动获取局域网段IP可以在 设置 --> IP网段过滤, 拥有 JMF ...

  2. cocos2dx中帧循环的伪代码实现

    1.在游戏开发中,帧率很大程度上体现了游戏的流畅度,帧循环是游戏中一个很重要的概念 2.下面用伪代码实现了cocos2dx中的帧循环 /*main函数调用*/ CCApplication::share ...

  3. json序列指定名称

    class jsonModel{ public string md5 { get; set; } public object report { get; set; } public string @v ...

  4. ELK kibana查询与过滤(17th)

    在kibana中,可通过搜索查询过滤事务或者在visualization界面点击元素过滤. 创建查询 在Discover界面的搜索栏输入要查询的字段.查询语法是基于Lucene的查询语法.允许布尔运算 ...

  5. Java_Swing实现小球沿正弦曲线运动的代码

    1 package zuidaimapack_1; import java.awt.*; import javax.swing.*; /** *Java_Swing实现小球沿正弦曲线运动的代码 * @ ...

  6. Netsharp快速入门(之11) 销售管理(开发销售订单工作区)

    作者:秋时 杨昶   时间:2014-02-15  转载须说明出处 4.3     销售订单开发 4.3.1  部件工作区设置 1.创建部件工作区,建工作区向导中要注意勾选组合并系部分.具体要建立的部 ...

  7. hdu 4685 二分匹配+强连通分量

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 题解: 这一题是poj 1904的加强版,poj 1904王子和公主的人数是一样多的,并且给出 ...

  8. POSIX 线程详解 一种支持内存共享的简捷工具

    线程是有趣的 了解如何正确运用线程是每一个优秀程序员必备的素质.线程类似于进程.如同进程,线程由内核按时间分片进行管理.在单处理器系统中,内核使用时间分片来模拟线程的并发执行,这种方式和进程的相同.而 ...

  9. js的全局函数

    JS的全局函数,全局函数和window对象的函数不一样. 全局函数不属于任何一个内置对象. JS包含以下7个全局函数,用于一些常用的功能: escape(),unescape(); //编码,解码. ...

  10. MAC下搭建web开发环境

    具体做法,参照此链接:http://mallinson.ca/osx-web-development/ Mac系统本身自带apache和PHP,MySQL可以安装也可以不安装 web开发的IDE可以是 ...