// // UIView+Extension.swift // AiKeyboard // // Created by Destiny on 2025/4/23. // import Foundation import UIKit import Toast_Swift // MARK: - UIView 有关 Frame 的扩展 public extension UIView { // MARK: 3.1、x 的位置 /// x 的位置 var kb_x: CGFloat { get { return frame.origin.x } set(newValue) { var tempFrame: CGRect = frame tempFrame.origin.x = newValue frame = tempFrame } } // MARK: 3.2、y 的位置 /// y 的位置 var kb_y: CGFloat { get { return frame.origin.y } set(newValue) { var tempFrame: CGRect = frame tempFrame.origin.y = newValue frame = tempFrame } } // MARK: 3.3、height: 视图的高度 /// height: 视图的高度 var kb_height: CGFloat { get { return frame.size.height } set(newValue) { var tempFrame: CGRect = frame tempFrame.size.height = newValue frame = tempFrame } } // MARK: 3.4、width: 视图的宽度 /// width: 视图的宽度 var kb_width: CGFloat { get { return frame.size.width } set(newValue) { var tempFrame: CGRect = frame tempFrame.size.width = newValue frame = tempFrame } } // MARK: 3.5、size: 视图的zize /// size: 视图的zize var kb_size: CGSize { get { return frame.size } set(newValue) { var tempFrame: CGRect = frame tempFrame.size = newValue frame = tempFrame } } // MARK: 3.6、centerX: 视图的X中间位置 /// centerX: 视图的X中间位置 var kb_centerX: CGFloat { get { return center.x } set(newValue) { var tempCenter: CGPoint = center tempCenter.x = newValue center = tempCenter } } // MARK: 3.7、centerY: 视图的Y中间位置 /// centerY: 视图Y的中间位置 var kb_centerY: CGFloat { get { return center.y } set(newValue) { var tempCenter: CGPoint = center tempCenter.y = newValue center = tempCenter } } // MARK: 3.9、top 上端横坐标(y) /// top 上端横坐标(y) var kb_top: CGFloat { get { return frame.origin.y } set(newValue) { var tempFrame: CGRect = frame tempFrame.origin.y = newValue frame = tempFrame } } // MARK: 3.10、left 左端横坐标(x) /// left 左端横坐标(x) var kb_left: CGFloat { get { return frame.origin.x } set(newValue) { var tempFrame: CGRect = frame tempFrame.origin.x = newValue frame = tempFrame } } // MARK: 3.11、bottom 底端纵坐标 (y + height) /// bottom 底端纵坐标 (y + height) var kb_bottom: CGFloat { get { return frame.origin.y + frame.size.height } set(newValue) { frame.origin.y = newValue - frame.size.height } } // MARK: 3.12、right 底端纵坐标 (x + width) /// right 底端纵坐标 (x + width) var kb_right: CGFloat { get { return frame.origin.x + frame.size.width } set(newValue) { frame.origin.x = newValue - frame.size.width } } // MARK: 3.13、origin 点 /// origin 点 var kb_origin: CGPoint { get { return frame.origin } set(newValue) { var tempOrigin: CGPoint = frame.origin tempOrigin = newValue frame.origin = tempOrigin } } } extension UIView { //添加4个不同大小的圆角 func addFourCorner(topLeft: CGFloat, topRight: CGFloat, bottomLeft: CGFloat, bottomRight: CGFloat){ let cornerRadii = UIView.CornerRadii.init(topLeft: topLeft, topRight: topRight, bottomLeft: bottomLeft, bottomRight: bottomRight) let path = createPathWithRoundedRect(bounds: self.bounds, cornerRadii:cornerRadii) let shapLayer = CAShapeLayer() shapLayer.frame = self.bounds shapLayer.path = path self.layer.mask = shapLayer } //各圆角大小 struct CornerRadii { var topLeft :CGFloat = 0 var topRight :CGFloat = 0 var bottomLeft :CGFloat = 0 var bottomRight :CGFloat = 0 } //切圆角函数绘制线条 func createPathWithRoundedRect (bounds:CGRect,cornerRadii:CornerRadii) -> CGPath { let minX = bounds.minX let minY = bounds.minY let maxX = bounds.maxX let maxY = bounds.maxY //获取四个圆心 let topLeftCenterX = minX + cornerRadii.topLeft let topLeftCenterY = minY + cornerRadii.topLeft let topRightCenterX = maxX - cornerRadii.topRight let topRightCenterY = minY + cornerRadii.topRight let bottomLeftCenterX = minX + cornerRadii.bottomLeft let bottomLeftCenterY = maxY - cornerRadii.bottomLeft let bottomRightCenterX = maxX - cornerRadii.bottomRight let bottomRightCenterY = maxY - cornerRadii.bottomRight //虽然顺时针参数是YES,在iOS中的UIView中,这里实际是逆时针 let path :CGMutablePath = CGMutablePath(); //顶 左 path.addArc(center: CGPoint(x: topLeftCenterX, y: topLeftCenterY), radius: cornerRadii.topLeft, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 3 / 2, clockwise: false) //顶右 path.addArc(center: CGPoint(x: topRightCenterX, y: topRightCenterY), radius: cornerRadii.topRight, startAngle: CGFloat.pi * 3 / 2, endAngle: 0, clockwise: false) //底右 path.addArc(center: CGPoint(x: bottomRightCenterX, y: bottomRightCenterY), radius: cornerRadii.bottomRight, startAngle: 0, endAngle: CGFloat.pi / 2, clockwise: false) //底左 path.addArc(center: CGPoint(x: bottomLeftCenterX, y: bottomLeftCenterY), radius: cornerRadii.bottomLeft, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi, clockwise: false) path.closeSubpath(); return path; } } // MARK: - Toast吐司🍞 extension UIView { // MARK: 普通消息 func toast(text: String) { var style = ToastStyle() style.messageFont = .systemFont(ofSize: 14) style.backgroundColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.85) style.cornerRadius = 8 style.verticalPadding = 14 style.horizontalPadding = 44 self.makeToast(text, duration: 1.5, position: .center, style: style) } }