ios 除了预设的 UIColor 之外,如何制作自己的自定义 UIColor?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1956587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make my own custom UIColor's other than the preset ones?
提问by Jab
I want to make my own RGB colors that are UIColors
and that I could use just like UIColor blackColor
or any other.
我想制作我自己的 RGB 颜色UIColors
,我可以使用它UIColor blackColor
或任何其他颜色。
回答by EEE
You can write your own method for UIColor class using categories.
您可以使用类别为 UIColor 类编写自己的方法。
#import <UIKit/UIKit.h>
@interface UIColor(NewColor)
+(UIColor *)MyColor;
@end
#import "UIColor-NewColor.h"
@implementation UIColor(NewColor)
+(UIColor *)MyColor {
return [UIColor colorWithRed:0.0-1.0 green:0.0-1.0 blue:0.0-1.0 alpha:1.0f];
}
By this way, you create a new color and now you can call it like
通过这种方式,您创建了一种新颜色,现在您可以将其命名为
[UIColor MyColor];
You can also implement this method to obtain random color. Hope this helps.
您也可以实现此方法来获取随机颜色。希望这可以帮助。
回答by James
I needed to define a couple of custom colors for use in several places in an app - but the colours are specific to that app. I thought about using categories, but didn't want to have extra files to include every time. I've therefore created a couple of static methods in my App delegate.
我需要定义几个自定义颜色以在应用程序的多个位置使用 - 但颜色特定于该应用程序。我考虑过使用类别,但不想每次都包含额外的文件。因此,我在我的 App 委托中创建了几个静态方法。
In MyAppDelegate.h
在 MyAppDelegate.h 中
+ (UIColor*)myColor1;
In MyAppDelegate.m
在 MyAppDelegate.m 中
+ (UIColor*)myColor1 {
return [UIColor colorWithRed:26.0f/255.0f green:131.0f/255.0f blue:32.0f/255.0f alpha:1.0f];
}
I have a method per color, or you could do a single method and add a parameter.
我对每种颜色都有一个方法,或者您可以执行一个方法并添加一个参数。
I can then use it anywhere in the app like this:
然后我可以在应用程序的任何地方使用它,如下所示:
myView.backgroundColor = [MyAppDelegate myColor1];
I hope this helps someone else.
我希望这对其他人有帮助。
回答by Suragch
Swift 3
斯威夫特 3
Creating a Swift extension allows you to define your own custom colors and use them just like the built in colors.
创建 Swift 扩展允许您定义自己的自定义颜色并像内置颜色一样使用它们。
UIColor+CustomColor.swift
UIColor+CustomColor.swift
import UIKit
extension UIColor {
class var customGreen: UIColor {
let darkGreen = 0x008110
return UIColor.rgb(fromHex: darkGreen)
}
class func rgb(fromHex: Int) -> UIColor {
let red = CGFloat((fromHex & 0xFF0000) >> 16) / 0xFF
let green = CGFloat((fromHex & 0x00FF00) >> 8) / 0xFF
let blue = CGFloat(fromHex & 0x0000FF) / 0xFF
let alpha = CGFloat(1.0)
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
}
Usage:
用法:
view.backgroundColor = UIColor.customGreen
回答by M.Arsalan
[UIColor colorWithRed:51.0 / 255.0, green:0.0, blue:153.0 / 255.0];
As long as you use a floating point value in your division you don't have to cast anything. Make sure you use floating point values. For example: 33 / 255 = 0
. Which will become black.
只要您在除法中使用浮点值,您就不必进行任何转换。确保使用浮点值。例如:33 / 255 = 0
。哪个会变黑。
回答by iPhone Guy
There are a couple of ways to create a color.
有几种方法可以创建颜色。
I prefer to use the RGB method. If you use the RGB values, divide them by 255 (I do not remember why, but I know you need to do it).
我更喜欢使用RGB方法。如果您使用 RGB 值,请将它们除以 255(我不记得为什么,但我知道您需要这样做)。
float rd = 225.00/255.00;
float gr = 177.00/255.00;
float bl = 140.00/255.00;
[label setTextColor:[UIColor colorWithRed:rd green:gr blue:bl alpha:1.0]];
Hope this helps.....
希望这可以帮助.....
回答by ovatsug25
For Swift:
对于斯威夫特:
let swiftColor = UIColor(red: 1, green: 165/255, blue: 233, alpha: 1)
回答by gerry3
Use initWithRed:green:blue:alpha:or colorWithRed:green:blue:alpha:.
使用initWithRed:green:blue:alpha:或colorWithRed:green:blue:alpha:。
For example:
例如:
// create new autoreleased UIColor object named "myColor"
UIColor *myColor = [UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1.0f];
// create new retained UIColor object named "myColor2"
UIColor *myColor2 = [[UIColor alloc] initWithRed:0.5f green:0.5f blue:0.5f alpha:1.0f];