前言
最近因為工作的原因,在做界面的時候,有時會忘記某種控件的顏色怎么設置,需要去網上進行搜索,所以寫下這篇文章。
一方面是收藏起來自己查閱,一方面是分享給大家。目標是有了這篇文章,不用再去搜索和顏色設置有關的內容。 話不多說了,來一起看看詳細的介紹吧。
下面進入正題
導航欄
1
2
3
4
5
6
7
8
9
10
11
12
|
/* 全局設置 */ // 標題顏色 // 如果需要設置字體就在字典中加入 [uifont fontwithname:@"hiragino sans gb" size:14] [[uinavigationbar appearance] settitletextattributes: @{nsforegroundcolorattributename:[uicolor whitecolor]}]; // 導航欄背景顏色 [[uinavigationbar appearance] setbartintcolor:[uicolor whitecolor]]; // 導航欄返回按鈕、自定義uibarbuttonitem顏色 [[uinavigationbar appearance] settintcolor:[uicolor blackcolor]]; |
1
2
3
4
5
6
7
8
9
10
|
/* 單獨設置 */ // 導航欄標題顏色 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 加上這句話
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// view controller-based status bar appearance // 加入這個參數,我們前面方法的設置就會失效 // 接下來就可以使用代碼進行設置了 /* 全局設置 */ [uiapplication sharedapplication].statusbarstyle = uistatusbarstylelightcontent; /* 單獨設置 */ - (uistatusbarstyle)preferredstatusbarstyle { return uistatusbarstylelightcontent; } // 細心的朋友讀者可能會疑問,為什么這次不能用 self.navigationcontroller.preferredstatusbarstyle = uistatusbarstylelightcontent; |
答案很簡單,仔細看報錯就知道這是一個 readonly 的屬性,所有我們直接重寫他的 set 方法。
tabbar
1
2
3
4
5
6
7
|
/* 全局設置 */ // tabbar背景顏色 [uitabbar appearance].bartintcolor = [uicolor whitecolor]; /* 單獨設置 */ // tabbar背景顏色 self.tabbarcontroller.tabbar.bartintcolor = [uicolor whitecolor]; |
tabbar圖標顏色
不用寫亂七八糟的代碼,直接到 assets.xcassets 里把圖片的屬性 render 設置為 original image 就可以讓顏色按照圖片的來,而不會選中變藍了。
button
1
2
3
4
5
6
7
8
9
10
11
12
|
// 字體顏色 // 有人可能會誤用這兩個錯誤的方法 // 錯誤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
1
2
|
// placeholder顏色設置 textfield.attributedplaceholder = [[nsattributedstring alloc] initwithstring:@ "placeholdtext" attributes:@{nsforegroundcolorattributename: [uicolor redcolor]}]; |
attributedstring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// 初始化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; |
通用部分
1
2
3
4
5
6
7
|
// 字體顏色 適用于label、textfield、textview等 label.textcolor = [uicolor whitecolor]; textfield.textcolor = [uicolor yellowcolor]; textview.textcolor = [uicolor yellowcolor]; // 背景顏色 基本都使用 someview.backgroundcolor = [uicolor whitecolor]; |
工具
系統自帶的測色工具,位置在 應用程序 -> 實用工具( launchpad 里叫其他) -> 數碼測色計
使用方法:
打開后指向你想測色的地方即可顯示他的 rgb 色,以這個 switch 舉個例子。
我們設置完rgb色后和你想要的略有差別。這里提供一個解決辦法。設置顏色的時候,點擊右邊的小齒輪,選擇 srgb。
幾種常用的列舉的差不多了。不完整的地方大家可以提出來,我會對這個文章進行更新。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://segmentfault.com/a/1190000010364486