当前位置: 移动技术网 > 移动技术>移动开发>IOS > 关于iOS中的各种颜色设置总结大全(推荐)

关于iOS中的各种颜色设置总结大全(推荐)

2019年07月24日  | 移动技术网移动技术  | 我要评论

前言

最近因为工作的原因,在做界面的时候,有时会忘记某种控件的颜色怎么设置,需要去网上进行搜索,所以写下这篇文章。

一方面是收藏起来自己查阅,一方面是分享给大家。目标是有了这篇文章,不用再去搜索和颜色设置有关的内容。 话不多说了,来一起看看详细的介绍吧。

下面进入正题

导航栏

/* 全局设置 */

// 标题颜色
// 如果需要设置字体就在字典中加入 [uifont fontwithname:@"hiragino sans gb" size:14]
[[uinavigationbar appearance] settitletextattributes:
  @{nsforegroundcolorattributename:[uicolor whitecolor]}];

// 导航栏背景颜色
[[uinavigationbar appearance] setbartintcolor:[uicolor whitecolor]];

// 导航栏返回按钮、自定义uibarbuttonitem颜色
[[uinavigationbar appearance] settintcolor:[uicolor blackcolor]];
/* 单独设置 */

// 导航栏标题颜色
self.navigationcontroller.navigationbar.titletextattributes = @{nsforegroundcolorattributename:[uicolor whitecolor]};

// 导航栏背景颜色
self.navigationcontroller.navigationbar.bartintcolor = [uicolor whitecolor];

// 导航栏返回按钮、自定义uibarbuttonitem颜色
self.navigationcontroller.navigationbar.tintcolor = [uicolor blackcolor];

状态栏

进入 targets -> general -> status bar style,可以设置 黑色(默认) 和 白色。


如果需要精确控制不同页面的颜色,还是需要代码设置。

首先给 info.plist 加上这句话


// view controller-based status bar appearance
// 加入这个参数,我们前面方法的设置就会失效
// 接下来就可以使用代码进行设置了

/* 全局设置 */

[uiapplication sharedapplication].statusbarstyle = uistatusbarstylelightcontent;

/* 单独设置 */

- (uistatusbarstyle)preferredstatusbarstyle {
 return uistatusbarstylelightcontent;
}

// 细心的朋友读者可能会疑问,为什么这次不能用
self.navigationcontroller.preferredstatusbarstyle = uistatusbarstylelightcontent;


答案很简单,仔细看报错就知道这是一个 readonly 的属性,所有我们直接重写他的 set 方法。

tabbar

/* 全局设置 */
// tabbar背景颜色
[uitabbar appearance].bartintcolor = [uicolor whitecolor];

/* 单独设置 */
// tabbar背景颜色
self.tabbarcontroller.tabbar.bartintcolor = [uicolor whitecolor];

tabbar图标颜色

不用写乱七八糟的代码,直接到 assets.xcassets 里把图片的属性 render 设置为 original image 就可以让颜色按照图片的来,而不会选中变蓝了。

button

// 字体颜色
// 有人可能会误用这两个错误的方法
// 错误1:[button.titlelabel settextcolor:[uicolorblackcolor]];
// 错误2:button.titlelabel.textcolor = [uicolor redcolor];
// 正确
[button settitlecolor:[uicolor blackcolor]
 forstate:uicontrolstatenormal];

// 边框颜色
// 默认没有边框,第一行是设置线条,第二行重点在于layer的颜色要用cgcolor
button.layer.borderwidth = 2.0;
button.layer.bordercolor = [uicolor blackcolor].cgcolor;

textfield

// placeholder颜色设置
textfield.attributedplaceholder = [[nsattributedstring alloc] initwithstring:@"placeholdtext" attributes:@{nsforegroundcolorattributename: [uicolor redcolor]}]; 

attributedstring

// 初始化nsmutableattributedstring
nsmutableattributedstring *str = [[nsmutableattributedstring alloc] initwithstring:@"using nsattributed string"];
// 颜色设置
[str addattribute:nsforegroundcolorattributename
 value:[uicolor bluecolor]
 range:nsmakerange(0,5)];
[str addattribute:nsforegroundcolorattributename
 value:[uicolor redcolor]
 range:nsmakerange(6,12)];
[str addattribute:nsforegroundcolorattributename
 value:[uicolor greencolor]
 range:nsmakerange(19,6)];
// 字体设置
[str addattribute:nsfontattributename
 value:[uifont fontwithname:@"arial-bolditalicmt" size:30.0]
 range:nsmakerange(0, 5)];
[str addattribute:nsfontattributename
 value:[uifont fontwithname:@"helveticaneue-bold" size:30.0]
 range:nsmakerange(6, 12)];
[str addattribute:nsfontattributename
 value:[uifont fontwithname:@"courier-boldoblique" size:30.0]
 range:nsmakerange(19, 6)];
// 把attributedstring赋值给label
attrlabel.attributedtext = str;

通用部分

// 字体颜色 适用于label、textfield、textview等
label.textcolor = [uicolor whitecolor];
textfield.textcolor = [uicolor yellowcolor];
textview.textcolor = [uicolor yellowcolor];

// 背景颜色 基本都使用
someview.backgroundcolor = [uicolor whitecolor];

工具

系统自带的测色工具,位置在 应用程序 -> 实用工具( launchpad 里叫其他) -> 数码测色计


使用方法:

打开后指向你想测色的地方即可显示他的 rgb 色,以这个 switch 举个例子。

我们设置完rgb色后和你想要的略有差别。这里提供一个解决办法。设置颜色的时候,点击右边的小齿轮,选择 srgb。

几种常用的列举的差不多了。不完整的地方大家可以提出来,我会对这个文章进行更新。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网