let xxview = UIView().setBackgroundColor("#0096FF").setTag(1001).setAlpha(0.5).setIsHidden(false)
let xxLabel = UILabel()
.setText("哈哈哈哈哈哈")
.setFont(16, bold: true)
.setTextColor("#FF0000")
let xxField = UITextField().setPlaceholder("请输入xxx").setFont(14).setTextAlignment(.center)
let xxButton = UIButton()
.setImage("icon", state: .normal)
.setIsHidden(true)
写起来是挺爽的,就是不知道会不会有什么问题。知道的朋友麻烦给我讲一下。
我是自己写了一些扩展如下:
//
// ChainedUI.swift
// demo
//
// Created by mac on 2018/7/122.
// Copyright © 2018年 mac. All rights reserved.
//
import UIKit
fileprivate func getFont(size: Float, name: String? = "systemFont", bold: Bool? = false) -> UIFont {
var font = UIFont()
if let bold = bold, bold == true {
///粗体
if let name = name {
if name.elementsEqual("systemFont") {
font = UIFont.boldSystemFont(ofSize: CGFloat(size))
}else {
if let temp = UIFont(name: name, size: CGFloat(size)) {
// temp.
font = temp
}else {
font = UIFont.boldSystemFont(ofSize: CGFloat(size))
}
}
}else {
font = UIFont.boldSystemFont(ofSize: CGFloat(size))
}
}else {
if let name = name {
if name.elementsEqual("systemFont") {
font = UIFont.systemFont(ofSize: CGFloat(size))
}else {
if let temp = UIFont(name: name, size: CGFloat(size)) {
font = temp
}else {
font = UIFont.systemFont(ofSize: CGFloat(size))
}
}
}else {
font = UIFont.systemFont(ofSize: CGFloat(size))
}
}
return font
}
//MARK: - UIView的扩展
extension UIView {
func setBackgroundColor(_ hexColor: String) -> Self {
self.backgroundColor = UIColor(hexColor)
return self
}
func setIsHidden(_ isHidden: Bool) -> Self {
self.isHidden = isHidden
return self
}
func setTag(_ tag: Int) -> Self {
self.tag = tag
return self
}
func setAlpha(_ alpha: CGFloat) -> Self {
self.alpha = alpha
return self
}
func setFrame(_ frame: CGRect) -> Self {
self.frame = frame
return self
}
}
//MARK: - UIImageView的扩展
extension UIImageView {
func setImage(_ imageName: String) -> Self {
self.image = UIImage(named: imageName)
return self
}
func setContentMode(_ mode: UIView.ContentMode) -> Self {
self.contentMode = mode
return self
}
}
//MARK: - UILabel的扩展
extension UILabel {
func setFont(_ size: Float, name: String? = "systemFont", bold: Bool? = false) -> Self {
self.font = getFont(size: size, name: name, bold: bold)
return self
}
func setText(_ text: String) -> Self {
self.text = text
return self
}
func setTextAlignment(_ alignment: NSTextAlignment) -> Self {
self.textAlignment = alignment
return self
}
func setTextColor(_ hexColor: String) -> Self {
self.textColor = UIColor(hexColor)
return self
}
func setNumberOfLines(_ number: Int) -> Self {
self.numberOfLines = number
return self
}
}
//MARK: - UIButton的扩展
extension UIButton {
func setFont(_ size: Float, name: String? = "systemFont", bold: Bool? = false) -> Self {
self.titleLabel?.font = getFont(size: size, name: name, bold: bold)
return self
}
func setText(_ text: String, state: UIControl.State) -> Self {
self.setTitle(text, for: state)
return self
}
func setTextColor(_ hexColor: String, state: UIControl.State) -> Self {
self.setTitleColor(UIColor(hexColor), for: state)
return self
}
func setImage(_ imageName: String, state: UIControl.State) -> Self {
self.imageView?.contentMode = .scaleAspectFit
self.setImage(UIImage(named: imageName), for: state)
return self
}
func setBackgroundImage(_ imageName: String, state: UIControl.State) -> Self {
self.setBackgroundImage(UIImage(named: imageName), for: state)
return self
}
func setImageContentMode(_ mode: UIView.ContentMode) -> Self {
self.imageView?.contentMode = mode
return self
}
}
//MARK: - UITextField的扩展
extension UITextField {
func setFont(_ size: Float, name: String? = "systemFont", bold: Bool? = false) -> Self {
self.font = getFont(size: size, name: name, bold: bold)
return self
}
func setText(_ text: String) -> Self {
self.text = text
return self
}
func setTextColor(_ hexColor: String) -> Self {
self.textColor = UIColor(hexColor)
return self
}
func setPlaceholder(_ placeholder: String) -> Self {
self.placeholder = placeholder
return self
}
func setTextAlignment(_ alignment: NSTextAlignment) -> Self {
self.textAlignment = alignment
return self
}
func setIsSecureTextEntry(_ isSecureTextEntry: Bool) -> Self {
self.isSecureTextEntry = isSecureTextEntry
return self
}
}
对了,代码里还是用了pod 'UIColor_Hex_Swift'
,它的代码如下:
//
// UIColorExtension.swift
// HEXColor
//
// Created by R0CKSTAR on 6/13/14.
// Copyright (c) 2014 P.D.Q. All rights reserved.
//
import UIKit
/**
MissingHashMarkAsPrefix: "Invalid RGB string, missing '#' as prefix"
UnableToScanHexValue: "Scan hex error"
MismatchedHexStringLength: "Invalid RGB string, number of characters after '#' should be either 3, 4, 6 or 8"
*/
public enum UIColorInputError : Error {
case missingHashMarkAsPrefix,
unableToScanHexValue,
mismatchedHexStringLength,
unableToOutputHexStringForWideDisplayColor
}
extension UIColor {
/**
The shorthand three-digit hexadecimal representation of color.
#RGB defines to the color #RRGGBB.
- parameter hex3: Three-digit hexadecimal value.
- parameter alpha: 0.0 - 1.0. The default is 1.0.
*/
public convenience init(hex3: UInt16, alpha: CGFloat = 1) {
let divisor = CGFloat(15)
let red = CGFloat((hex3 & 0xF00) >> 8) / divisor
let green = CGFloat((hex3 & 0x0F0) >> 4) / divisor
let blue = CGFloat( hex3 & 0x00F ) / divisor
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
/**
The shorthand four-digit hexadecimal representation of color with alpha.
#RGBA defines to the color #RRGGBBAA.
- parameter hex4: Four-digit hexadecimal value.
*/
public convenience init(hex4: UInt16) {
let divisor = CGFloat(15)
let red = CGFloat((hex4 & 0xF000) >> 12) / divisor
let green = CGFloat((hex4 & 0x0F00) >> 8) / divisor
let blue = CGFloat((hex4 & 0x00F0) >> 4) / divisor
let alpha = CGFloat( hex4 & 0x000F ) / divisor
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
/**
The six-digit hexadecimal representation of color of the form #RRGGBB.
- parameter hex6: Six-digit hexadecimal value.
*/
public convenience init(hex6: UInt32, alpha: CGFloat = 1) {
let divisor = CGFloat(255)
let red = CGFloat((hex6 & 0xFF0000) >> 16) / divisor
let green = CGFloat((hex6 & 0x00FF00) >> 8) / divisor
let blue = CGFloat( hex6 & 0x0000FF ) / divisor
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
/**
The six-digit hexadecimal representation of color with alpha of the form #RRGGBBAA.
- parameter hex8: Eight-digit hexadecimal value.
*/
public convenience init(hex8: UInt32) {
let divisor = CGFloat(255)
let red = CGFloat((hex8 & 0xFF000000) >> 24) / divisor
let green = CGFloat((hex8 & 0x00FF0000) >> 16) / divisor
let blue = CGFloat((hex8 & 0x0000FF00) >> 8) / divisor
let alpha = CGFloat( hex8 & 0x000000FF ) / divisor
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
/**
The rgba string representation of color with alpha of the form #RRGGBBAA/#RRGGBB, throws error.
- parameter rgba: String value.
*/
public convenience init(rgba_throws rgba: String) throws {
guard rgba.hasPrefix("#") else {
throw UIColorInputError.missingHashMarkAsPrefix
}
let hexString: String = String(rgba[String.Index.init(encodedOffset: 1)...])
var hexValue: UInt32 = 0
guard Scanner(string: hexString).scanHexInt32(&hexValue) else {
throw UIColorInputError.unableToScanHexValue
}
switch (hexString.count) {
case 3:
self.init(hex3: UInt16(hexValue))
case 4:
self.init(hex4: UInt16(hexValue))
case 6:
self.init(hex6: hexValue)
case 8:
self.init(hex8: hexValue)
default:
throw UIColorInputError.mismatchedHexStringLength
}
}
/**
The rgba string representation of color with alpha of the form #RRGGBBAA/#RRGGBB, fails to default color.
- parameter rgba: String value.
*/
public convenience init(_ rgba: String, defaultColor: UIColor = UIColor.clear) {
guard let color = try? UIColor(rgba_throws: rgba) else {
self.init(cgColor: defaultColor.cgColor)
return
}
self.init(cgColor: color.cgColor)
}
/**
Hex string of a UIColor instance, throws error.
- parameter includeAlpha: Whether the alpha should be included.
*/
public func hexStringThrows(_ includeAlpha: Bool = true) throws -> String {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
self.getRed(&r, green: &g, blue: &b, alpha: &a)
guard r >= 0 && r <= 1 && g >= 0 && g <= 1 && b >= 0 && b <= 1 else {
throw UIColorInputError.unableToOutputHexStringForWideDisplayColor
}
if (includeAlpha) {
return String(format: "#%02X%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255), Int(a * 255))
} else {
return String(format: "#%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255))
}
}
/**
Hex string of a UIColor instance, fails to empty string.
- parameter includeAlpha: Whether the alpha should be included.
*/
public func hexString(_ includeAlpha: Bool = true) -> String {
guard let hexString = try? hexStringThrows(includeAlpha) else {
return ""
}
return hexString
}
}
extension String {
/**
Convert argb string to rgba string.
*/
public func argb2rgba() -> String? {
guard self.hasPrefix("#") else {
return nil
}
let hexString: String = String(self[self.index(self.startIndex, offsetBy: 1)...])
switch hexString.count {
case 4:
return "#\(String(hexString[self.index(self.startIndex, offsetBy: 1)...]))\(String(hexString[..<self.index(self.startIndex, offsetBy: 1)]))"
case 8:
return "#\(String(hexString[self.index(self.startIndex, offsetBy: 2)...]))\(String(hexString[..<self.index(self.startIndex, offsetBy: 2)]))"
default:
return nil
}
}
}
完
网友评论