我如何从十六进制字符串格式创建一个UIColor,如#00FF00?
当前回答
上面的几个解决方案涉及到一些不必要的nsstring使用。这个UIColor类扩展更简单更快:
+ colorWithHex:(UInt32)hex alpha:(CGFloat)alpha
{
return [UIColor colorWithRed:((hex & 0xFF0000) >> 16)/255.0
green:((hex & 0x00FF00) >> 8)/255.0
blue:( hex & 0x0000FF)/255.0
alpha:alpha];
}
简单来说:
return [UIColor colorWithHex:0x006400 alpha:1.0]; // HTML darkgreen
其他回答
一个伟大的Swift实现(为Xcode 7更新)使用扩展,从各种不同的答案和地方拉到一起。在结尾还需要字符串扩展名。
Use:
let hexColor = UIColor(hex: "#00FF00")
注意:我添加了一个选项,为alpha通道的标准6位十六进制值的末尾添加2个额外的数字(传入值为00-99)。如果这冒犯了你,就把它拿掉。您可以实现它来传递一个可选的alpha参数。
扩展:
extension UIColor {
convenience init(var hex: String) {
var alpha: Float = 100
let hexLength = hex.characters.count
if !(hexLength == 7 || hexLength == 9) {
// A hex must be either 7 or 9 characters (#RRGGBBAA)
print("improper call to 'colorFromHex', hex length must be 7 or 9 chars (#GGRRBBAA)")
self.init(white: 0, alpha: 1)
return
}
if hexLength == 9 {
// Note: this uses String subscripts as given below
alpha = hex[7...8].floatValue
hex = hex[0...6]
}
// Establishing the rgb color
var rgb: UInt32 = 0
let s: NSScanner = NSScanner(string: hex)
// Setting the scan location to ignore the leading `#`
s.scanLocation = 1
// Scanning the int into the rgb colors
s.scanHexInt(&rgb)
// Creating the UIColor from hex int
self.init(
red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgb & 0x0000FF) / 255.0,
alpha: CGFloat(alpha / 100)
)
}
}
字符串扩展: 浮动的来源 下标源
extension String {
/**
Returns the float value of a string
*/
var floatValue: Float {
return (self as NSString).floatValue
}
/**
Subscript to allow for quick String substrings ["Hello"][0...1] = "He"
*/
subscript (r: Range<Int>) -> String {
get {
let start = self.startIndex.advancedBy(r.startIndex)
let end = self.startIndex.advancedBy(r.endIndex - 1)
return self.substringWithRange(start..<end)
}
}
}
斯威夫特4
你可以像这样在扩展中创建一个非常方便的构造函数:
extension UIColor {
convenience init(hexString: String, alpha: CGFloat = 1.0) {
var hexInt: UInt32 = 0
let scanner = Scanner(string: hexString)
scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
scanner.scanHexInt32(&hexInt)
let red = CGFloat((hexInt & 0xff0000) >> 16) / 255.0
let green = CGFloat((hexInt & 0xff00) >> 8) / 255.0
let blue = CGFloat((hexInt & 0xff) >> 0) / 255.0
let alpha = alpha
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
}
以后再用
let color = UIColor(hexString: "#AABBCCDD")
有cocoapod支持,这很好
https://github.com/mRs-/HexColors
// with hash
NSColor *colorWithHex = [NSColor colorWithHexString:@"#ff8942" alpha:1];
// wihtout hash
NSColor *secondColorWithHex = [NSColor colorWithHexString:@"ff8942" alpha:1];
// short handling
NSColor *shortColorWithHex = [NSColor colorWithHexString:@"fff" alpha:1]
我发现最简单的方法是使用宏。只要把它包括在你的标题中,它就可以在你的整个项目中使用。
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
Uicolor宏十六进制值
这段代码的格式化版本:
#define UIColorFromRGB(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \
blue:((float)((rgbValue & 0x0000FF) >> 0))/255.0 \
alpha:1.0]
用法:
label.textColor = UIColorFromRGB(0xBC1128);
迅速:
static func UIColorFromRGB(_ rgbValue: Int) -> UIColor! {
return UIColor(
red: CGFloat((Float((rgbValue & 0xff0000) >> 16)) / 255.0),
green: CGFloat((Float((rgbValue & 0x00ff00) >> 8)) / 255.0),
blue: CGFloat((Float((rgbValue & 0x0000ff) >> 0)) / 255.0),
alpha: 1.0)
}
为UIColor创建优雅的扩展:
extension UIColor {
convenience init(string: String) {
var uppercasedString = string.uppercased()
uppercasedString.remove(at: string.startIndex)
var rgbValue: UInt32 = 0
Scanner(string: uppercasedString).scanHexInt32(&rgbValue)
let red = CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0
let green = CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0
let blue = CGFloat(rgbValue & 0x0000FF) / 255.0
self.init(red: red, green: green, blue: blue, alpha: 1)
}
}
创建红色:
let red = UIColor(string: "#ff0000")
推荐文章
- iOS如何设置应用程序图标和启动图像
- 更改UITextField和UITextView光标/插入符颜色
- 'Project Name'是通过优化编译的——步进可能会表现得很奇怪;变量可能不可用
- 如何设置回退按钮文本在Swift
- 模拟器慢动作动画现在打开了吗?
- 如何为TableView创建NSIndexPath
- 滑动删除和“更多”按钮(就像iOS 7的邮件应用程序)
- 如何比较两个nsdate:哪个是最近的?
- 使UINavigationBar透明
- 如何改变推和弹出动画在一个基于导航的应用程序
- 删除/重置核心数据中的所有条目?
- setNeedsLayout vs. setNeedsUpdateConstraints和layoutIfNeeded vs. updateConstraintsIfNeeded
- 不区分大小写的比较
- 我怎么能得到一个uiimage的高度和宽度?
- 我如何模仿地图应用程序的底部表格?