iOS 颜色设置看我就够了

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

一方面是收藏起来自己查阅,一方面是分享给大家。目标是有了这篇文章,不用再去搜索和颜色设置有关的内容。
下面进入正题

导航栏

/* 全局设置 */

// 标题颜色
// 如果需要设置字体就在字典中加入 [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,可以设置 黑色(默认) 和 白色。

状态栏.png

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

首先给 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 就可以让颜色按照图片的来,而不会选中变蓝了。

TabBar图标颜色

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。

sRGB

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,094评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,194评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,670评论 7 249
  • 曾经拥有的东西,现在回想是否早已遗弃?曾经约好的誓言,现在回首是否已经放弃?曾经美好的时光,现在回忆是否留下遗憾?...
    梦醒醉余生阅读 1,878评论 2 7
  • 今天是周一,公布一下本周的主题——阅读。我会在已经读过的有关阅读的十多本书里面,选择5本周一至周五进行分享,然后会...
    公明同学阅读 3,767评论 0 1