gpt4 book ai didi

ios - 在Xcode中制作RGB颜色

转载 作者:行者123 更新时间:2023-11-30 12:17:34 26 4
gpt4 key购买 nike

我使用 Photoshop 中颜色的 RGB 值,并在 Xcode 中使用相同的颜色值,值为:Color-R-160、G-97、B-5...Photoshop 中的颜色呈淡黄色,但在 Xcode 中,当我使用过

myLabel.textColor = [UIColor colorWithRed:160 green:97 blue:5 alpha:1] ;

颜色呈白色。

为什么会出现这种差异?

最佳答案

objective-C

您必须提供 0 到 1.0 之间的值。因此将 RGB 值除以 255。

myLabel.textColor= [UIColor colorWithRed:(160/255.0) green:(97/255.0) blue:(5/255.0) alpha:1] ;

更新:

您也可以使用此宏

#define Rgb2UIColor(r, g, b)  [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]

你可以像这样调用你的任何类(class)

 myLabel.textColor = Rgb2UIColor(160, 97, 5);

swift

这是正常的颜色语法

myLabel.textColor = UIColor(red: (160/255.0), green: (97/255.0), blue: (5/255.0), alpha: 1.0) 
//The values should be between 0 to 1

Swift 对宏不太友好

Complex macros are used in C and Objective-C but have no counterpart in Swift. Complex macros are macros that do not define constants, including parenthesized, function-like macros. You use complex macros in C and Objective-C to avoid type-checking constraints or to avoid retyping large amounts of boilerplate code. However, macros can make debugging and refactoring difficult. In Swift, you can use functions and generics to achieve the same results without any compromises. Therefore, the complex macros that are in C and Objective-C source files are not made available to your Swift code.

所以我们为此使用扩展

extension UIColor {
convenience init(_ r: Double,_ g: Double,_ b: Double,_ a: Double) {
self.init(red: r/255, green: g/255, blue: b/255, alpha: a)
}
}

你可以像这样使用它

myLabel.textColor = UIColor(160.0, 97.0, 5.0, 1.0)

关于ios - 在Xcode中制作RGB颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45199482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com