關(guān)于iOS中的各種顏色設(shè)置總結(jié)大全(推薦)
前言
最近因?yàn)楣ぷ鞯脑?,在做界面的時(shí)候,有時(shí)會(huì)忘記某種控件的顏色怎么設(shè)置,需要去網(wǎng)上進(jìn)行搜索,所以寫下這篇文章。
一方面是收藏起來(lái)自己查閱,一方面是分享給大家。目標(biāo)是有了這篇文章,不用再去搜索和顏色設(shè)置有關(guān)的內(nèi)容。 話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
下面進(jìn)入正題
導(dǎo)航欄
/* 全局設(shè)置 */ // 標(biāo)題顏色 // 如果需要設(shè)置字體就在字典中加入 [UIFont fontWithName:@"Hiragino Sans GB" size:14] [[UINavigationBar appearance] setTitleTextAttributes: @{NSForegroundColorAttributeName:[UIColor whiteColor]}]; // 導(dǎo)航欄背景顏色 [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]]; // 導(dǎo)航欄返回按鈕、自定義UIBarButtonItem顏色 [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
/* 單獨(dú)設(shè)置 */ // 導(dǎo)航欄標(biāo)題顏色 self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; // 導(dǎo)航欄背景顏色 self.navigationController.navigationBar.barTintColor = [UIColor whiteColor]; // 導(dǎo)航欄返回按鈕、自定義UIBarButtonItem顏色 self.navigationController.navigationBar.tintColor = [UIColor blackColor];
狀態(tài)欄
進(jìn)入 Targets -> General -> Status Bar Style,可以設(shè)置 黑色(默認(rèn)) 和 白色。
如果需要精確控制不同頁(yè)面的顏色,還是需要代碼設(shè)置。
首先給 info.plist 加上這句話
// View controller-based status bar appearance // 加入這個(gè)參數(shù),我們前面方法的設(shè)置就會(huì)失效 // 接下來(lái)就可以使用代碼進(jìn)行設(shè)置了 /* 全局設(shè)置 */ [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; /* 單獨(dú)設(shè)置 */ - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } // 細(xì)心的朋友讀者可能會(huì)疑問(wèn),為什么這次不能用 self.navigationController.preferredStatusBarStyle = UIStatusBarStyleLightContent;
答案很簡(jiǎn)單,仔細(xì)看報(bào)錯(cuò)就知道這是一個(gè) readonly 的屬性,所有我們直接重寫他的 set 方法。
TabBar
/* 全局設(shè)置 */ // TabBar背景顏色 [UITabBar appearance].barTintColor = [UIColor whiteColor]; /* 單獨(dú)設(shè)置 */ // TabBar背景顏色 self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
TabBar圖標(biāo)顏色
不用寫亂七八糟的代碼,直接到 Assets.xcassets 里把圖片的屬性 Render 設(shè)置為 Original Image 就可以讓顏色按照?qǐng)D片的來(lái),而不會(huì)選中變藍(lán)了。
Button
// 字體顏色 // 有人可能會(huì)誤用這兩個(gè)錯(cuò)誤的方法 // 錯(cuò)誤1:[button.titleLabel setTextColor:[UIColorblackColor]]; // 錯(cuò)誤2:button.titleLabel.textColor = [UIColor redColor]; // 正確 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // 邊框顏色 // 默認(rèn)沒(méi)有邊框,第一行是設(shè)置線條,第二行重點(diǎn)在于layer的顏色要用CGColor button.layer.borderWidth = 2.0; button.layer.borderColor = [UIColor blackColor].CGColor;
TextField
// placeholder顏色設(shè)置 textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"placeHoldtext" attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];
AttributedString
// 初始化NSMutableAttributedString NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"]; // 顏色設(shè)置 [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)]; // 字體設(shè)置 [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];
工具
系統(tǒng)自帶的測(cè)色工具,位置在 應(yīng)用程序 -> 實(shí)用工具( Launchpad 里叫其他) -> 數(shù)碼測(cè)色計(jì)
使用方法:
打開(kāi)后指向你想測(cè)色的地方即可顯示他的 RGB 色,以這個(gè) Switch 舉個(gè)例子。
我們?cè)O(shè)置完rgb色后和你想要的略有差別。這里提供一個(gè)解決辦法。設(shè)置顏色的時(shí)候,點(diǎn)擊右邊的小齒輪,選擇 sRGB。
幾種常用的列舉的差不多了。不完整的地方大家可以提出來(lái),我會(huì)對(duì)這個(gè)文章進(jìn)行更新。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS Xcode8更新后輸出log日志關(guān)閉的方法
今天剛把xcode更新到了xcode8,運(yùn)行發(fā)現(xiàn)好多l(xiāng)og輸出,怎么關(guān)閉呢,不是很清楚,通過(guò)查閱相關(guān)資料順利關(guān)掉這些log日志,下面小編把方法共享下,需要的朋友參考下2016-09-09iOS中UIAlertController設(shè)置自定義標(biāo)題與內(nèi)容的方法
UIAlertController是iOS8推出的新概念,取代了之前的 UIAlertView和UIActionSheet(雖然現(xiàn)在仍可以使用,但是會(huì)有警告)。下面這篇文章主要給大家介紹了關(guān)于iOS中UIAlertController如何設(shè)置自定義標(biāo)題與內(nèi)容的相關(guān)資料,需要的朋友可以參考下。2017-10-10iOS實(shí)現(xiàn)無(wú)感知上拉加載更多功能的思路與方法
下拉刷新和上拉加載更多功能是一個(gè)應(yīng)用非常廣泛的一個(gè)效果,最新項(xiàng)目中就遇到這個(gè)功能,這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)無(wú)感知上拉加載更多功能的思路與方法,需要的朋友可以參考下2021-07-07iOS UIButton擴(kuò)大按鈕響應(yīng)區(qū)域的解決方法
這篇文章主要為大家詳細(xì)介紹了iOS UIButton擴(kuò)大按鈕響應(yīng)區(qū)域的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08深入解析iOS應(yīng)用開(kāi)發(fā)中對(duì)設(shè)計(jì)模式中的橋接模式的使用
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中對(duì)設(shè)計(jì)模式中的橋接模式的使用,bridge橋接模式中主張把抽象部分與實(shí)現(xiàn)部分分離,需要的朋友可以參考下2016-03-03理解Objective-C的變量以及面相對(duì)象的繼承特性
這篇文章主要介紹了理解Objective-C的變量以及面相對(duì)象的繼承特性,文中的所說(shuō)的點(diǎn)語(yǔ)法即是'對(duì)象名.成員變量名'這種對(duì)變量的訪問(wèn),需要的朋友可以參考下2016-01-01iOS多控制器實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了iOS多控制器實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06