最近發現作界面的時候,有時會忘記某種控件的顏色怎麼設置,須要去網上進行搜索,因此寫下這篇文章。 app
一方面是收藏起來本身查閱,一方面是分享給你們。目標是有了這篇文章,不用再去搜索和顏色設置有關的內容。
下面進入正題工具
/* 全局設置 */ // 標題顏色 // 若是須要設置字體就在字典中加入 [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 方法。spa
/* 全局設置 */ // TabBar背景顏色 [UITabBar appearance].barTintColor = [UIColor whiteColor]; /* 單獨設置 */ // TabBar背景顏色 self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
TabBar圖標顏色
不用寫亂七八糟的代碼,直接到 Assets.xcassets 裏把圖片的屬性 Render 設置爲 Original Image 就可讓顏色按照圖片的來,而不會選中變藍了。
// 字體顏色 // 有人可能會誤用這兩個錯誤的方法 // 錯誤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;
// placeholder顏色設置 textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"placeHoldtext" attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];
// 初始化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 舉個例子。
code
咱們設置完rgb色後和你想要的略有差異。這裏提供一個解決辦法。設置顏色的時候,點擊右邊的小齒輪,選擇 sRGB。orm
幾種經常使用的列舉的差很少了。不完整的地方你們能夠提出來,我會對這個文章進行更新。