相关代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var shiLiuJinZhiTextField: UITextField! //输入16进制的地方
@IBOutlet weak var RTextField: UITextField! //R颜色值
@IBOutlet weak var GTextField: UITextField! //G颜色值
@IBOutlet weak var BTextField: UITextField! //B颜色值
@IBOutlet weak var jinZhiButton: UIButton! //进制按钮
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//页面初始阶段,RGB全不能编辑
self.BTextField.userInteractionEnabled = false
self.RTextField.userInteractionEnabled = false
self.GTextField.userInteractionEnabled = false
}
/**
进制点击按钮
*/
@IBAction func jinZhiButton(sender: AnyObject) {
if self.jinZhiButton.titleLabel?.text == "16进制" {
self.shiLiuJinZhiTextField.userInteractionEnabled = false
self.BTextField.userInteractionEnabled = true
self.RTextField.userInteractionEnabled = true
self.GTextField.userInteractionEnabled = true
self.jinZhiButton.setTitle("10进制", forState: UIControlState.Normal)
} else {
self.shiLiuJinZhiTextField.userInteractionEnabled = true
self.BTextField.userInteractionEnabled = false
self.RTextField.userInteractionEnabled = false
self.GTextField.userInteractionEnabled = false
self.jinZhiButton.setTitle("16进制", forState: UIControlState.Normal)
}
}
/**
完成按钮点击事件
*/
@IBAction func wanChengButtonClick(sender: AnyObject) {
if self.jinZhiButton.titleLabel?.text == "16进制" {
if self.shiLiuJinZhiTextField.text?.characters.count == 7 {
self.view.backgroundColor = RGB(self.shiLiuJinZhiTextField.text!)
}
} else {
if self.RTextField.text?.characters.count != 0 && self.GTextField.text?.characters.count != 0 && self.BTextField.text?.characters.count != 0 {
let r = CGFloat(Float(self.RTextField.text!)!)
let g = CGFloat(Float(self.GTextField.text!)!)
let b = CGFloat(Float(self.BTextField.text!)!)
self.shiLiuJinZhiTextField.text = "#\(valueToString(Int(r)/16))\(valueToString(Int(r)%16))\(valueToString(Int(g)/16))\(valueToString(Int(g)%16))\(valueToString(Int(b)/16))\(valueToString(Int(b)%16))"
self.view.backgroundColor = UIColor.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
} else {
print("请输入值")
}
}
}
/**
将10进制转换成16进制方法
- parameter value: 传入的10进制数值
- returns: 返回16进制字符串
*/
func valueToString(value: Int) -> String {
if value < 10 {
return "\(value)"
} else {
switch value {
case 10:
return "a"
case 11:
return "b"
case 12:
return "c"
case 13:
return "d"
case 14:
return "e"
case 15:
return "f"
default:
return "f"
}
}
}
/**
16进制颜色转换方法
- parameter rgb: 输入的16进制字符串
- returns: 返回颜色
*/
func RGB(rgb: String) -> UIColor {
var shuzu: [CGFloat] = []
for each in rgb.characters {
shuzu.append(check(String(each)))
}
let r = shuzu[1]*16+shuzu[2]
let g = shuzu[3]*16+shuzu[4]
let b = shuzu[5]*16+shuzu[6]
self.RTextField.text = "\(r)"
self.GTextField.text = "\(g)"
self.BTextField.text = "\(b)"
return UIColor.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
}
/**
当数值是在[0-9]时直接返回 当数值在[a-f]时转化成10进制返回
- parameter str: 接收到的16进制数值
- returns: 返回CGFloat值
*/
private func check(str: String) -> CGFloat {
// 使用正则表达式一定要加try语句
do {
//创建规则
let pattern = "[0-9]"
//创建正则表达式对象
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)
//开始匹配
let res = regex.matchesInString(str, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, str.characters.count))
//输出结果
for checkingRes in res {
let strOne = (str as NSString).substringWithRange(checkingRes.range)
return CGFloat(Float(strOne)!)
}
//创建规则
let patternStr = "[a-f]"
//创建正则表达式对象
let regexStr = try NSRegularExpression(pattern: patternStr, options: NSRegularExpressionOptions.CaseInsensitive)
//进行匹配
let resStr = regexStr.matchesInString(str, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, str.characters.count))
for each in resStr {
let b = (str as NSString).substringWithRange(each.range)
switch b {
case "a":
return 10
case "b":
return 11
case "c":
return 12
case "d":
return 13
case "e":
return 14
case "f":
return 15
default:
return 0
}
}
}
catch {
print(error)
}
return 10086
}
//收回键盘
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.shiLiuJinZhiTextField.resignFirstResponder()
self.RTextField.resignFirstResponder()
self.GTextField.resignFirstResponder()
self.BTextField.resignFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}