// // UIView+Frame.swift // RxXMLY // // Created by sessionCh on 2017/12/14. // Copyright © 2017年 sessionCh. All rights reserved. // import UIKit /// Extends CGRect with helper properties for positioning and setting dimensions extension CGRect: ExpressibleByStringLiteral { /// The top coordinate of the rect. public var top: CGFloat { get { return origin.y } set(value) { origin.y = value } } // The left-side coordinate of the rect. public var left: CGFloat { get { return origin.x } set(value) { origin.x = value } } // The bottom coordinate of the rect. Setting this will change origin.y of the rect according to // the height of the rect. public var bottom: CGFloat { get { return origin.y + size.height } set(value) { origin.y = value - size.height } } // The right-side coordinate of the rect. Setting this will change origin.x of the rect according to // the width of the rect. public var right: CGFloat { get { return origin.x + size.width } set(value) { origin.x = value - size.width } } // The width of the rect. public var width: CGFloat { get { return size.width } set(value) { size.width = value } } // The height of the rect. public var height: CGFloat { get { return size.height } set(value) { size.height = value } } // The center x coordinate of the rect. public var centerX: CGFloat { get { return origin.x + size.width / 2 } set (value) { origin.x = value - size.width / 2 } } // The center y coordinate of the rect. public var centerY: CGFloat { get { return origin.y + size.height / 2 } set (value) { origin.y = value - size.height / 2 } } // The center of the rect. public var center: CGPoint { get { return CGPoint(x: centerX, y: centerY) } set (value) { centerX = value.x centerY = value.y } } public typealias ExtendedGraphemeClusterLiteralType = StringLiteralType public init(stringLiteral value: StringLiteralType) { self.init() let rect: CGRect if value[value.startIndex] != "{" { let comp = value.components(separatedBy: ",") if comp.count == 4 { rect = NSCoder.cgRect(for: "{{\(comp[0]),\(comp[1])}, {\(comp[2]), \(comp[3])}}") } else { rect = CGRect.zero } } else { rect = NSCoder.cgRect(for: value) } self.size = rect.size self.origin = rect.origin } public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) { self.init(stringLiteral: value) } public typealias UnicodeScalarLiteralType = StringLiteralType public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) { self.init(stringLiteral: value) } } extension CGPoint: ExpressibleByStringLiteral { public typealias ExtendedGraphemeClusterLiteralType = StringLiteralType public init(stringLiteral value: StringLiteralType) { self.init() let point: CGPoint if value[value.startIndex] != "{" { point = NSCoder.cgPoint(for: "{\(value)}") } else { point = NSCoder.cgPoint(for: value) } self.x = point.x self.y = point.y } public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) { self.init(stringLiteral: value) } public typealias UnicodeScalarLiteralType = StringLiteralType public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) { self.init(stringLiteral: value) } } extension UIView { /// The top coordinate of the UIView. public var top: CGFloat { get { return frame.top } set(value) { var frame = self.frame frame.top = value self.frame = frame } } /// The left coordinate of the UIView. public var left: CGFloat { get { return frame.left } set(value) { var frame = self.frame frame.left = value self.frame = frame } } /// The bottom coordinate of the UIView. public var bottom: CGFloat { get { return frame.bottom } set(value) { var frame = self.frame frame.bottom = value self.frame = frame } } /// The right coordinate of the UIView. public var right: CGFloat { get { return frame.right } set(value) { var frame = self.frame frame.right = value self.frame = frame } } // The width of the UIView. public var width: CGFloat { get { return frame.width } set(value) { var frame = self.frame frame.width = value self.frame = frame } } // The height of the UIView. public var height: CGFloat { get { return frame.height } set(value) { var frame = self.frame frame.height = value self.frame = frame } } /// The horizontal center coordinate of the UIView. public var centerX: CGFloat { get { return frame.centerX } set(value) { var frame = self.frame frame.centerX = value self.frame = frame } } /// The vertical center coordinate of the UIView. public var centerY: CGFloat { get { return frame.centerY } set(value) { var frame = self.frame frame.centerY = value self.frame = frame } } //将当前视图转为UIImage func asImage() -> UIImage { if #available(iOS 10.0, *) { let renderer = UIGraphicsImageRenderer(bounds: bounds) return renderer.image { rendererContext in layer.render(in: rendererContext.cgContext ) } } else { // Fallback on earlier versions UIGraphicsBeginImageContext(frame.size) layer.render(in: UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! } } } extension UIView { /// 渐变设置 /// /// - Parameters: /// - colors: 渐变颜色数组 /// - startPoint: 开始 /// - endPoint: 结束 /// - locations: 位置数组 func gradient(colors: [UIColor], startPoint: CGPoint = CGPoint(x: 0, y: 0), endPoint: CGPoint = CGPoint(x: 0, y: 1), locations: [Float]?, autoLaoutFrame: CGRect? = nil) { let gradient = CAGradientLayer() gradient.frame = (autoLaoutFrame != nil) ? autoLaoutFrame! : bounds gradient.colors = colors.map{ color in return color.cgColor } gradient.startPoint = startPoint gradient.endPoint = endPoint if let location = locations { gradient.locations = location as [NSNumber] } layer.insertSublayer(gradient, at: 0) } } protocol NibLoadable { } extension NibLoadable where Self: UIView { static func loadFromNib(_ nibName: String? = nil) -> Self { return Bundle.main.loadNibNamed(nibName ?? "\(self)", owner: nil, options: nil)?.first as! Self } } extension UIView { @IBInspectable var cornerRadius: CGFloat { get { return layer.cornerRadius } set { layer.masksToBounds = (newValue > 0) layer.cornerRadius = newValue } } @IBInspectable var borderWidth: CGFloat { get { return layer.borderWidth } set { layer.borderWidth = newValue } } @IBInspectable var borderColor: UIColor { get { return UIColor(cgColor: layer.borderColor!) } set { layer.borderColor = newValue.cgColor } } }