import UIKit
public extension UIView {
/// 获取 控件高度
func heightToFit() -> CGFloat {
self.setNeedsLayout()
self.layoutIfNeeded()
return self.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
}
/// 圆角
func maskRadius(radius: CGFloat, corners: UIRectCorner) {
guard self.bounds != CGRect.zero else { return }
let bezierPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let shapeLayer = CAShapeLayer()
shapeLayer.frame = self.bounds
shapeLayer.path = bezierPath.cgPath
self.layer.mask = shapeLayer
}
/// 边框 + 圆角
func maskBorder(radius: CGFloat, corners: UIRectCorner, fillColor: UIColor, strokeColor: UIColor, borderWidth: CGFloat) {
guard self.bounds != CGRect.zero else { return }
let bezierPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let shapeLayer = CAShapeLayer()
shapeLayer.frame = self.bounds
shapeLayer.path = bezierPath.cgPath
self.layer.mask = shapeLayer
let borderLayer = CAShapeLayer()
borderLayer.frame = self.bounds
borderLayer.path = bezierPath.cgPath
borderLayer.lineWidth = borderWidth
borderLayer.fillColor = fillColor.cgColor
borderLayer.strokeColor = strokeColor.cgColor
self.layer.addSublayer(borderLayer)
}
/// 渐变色
func addGradientLayer(start: UIColor, end: UIColor) {
let gradientLayer = CAGradientLayer()
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 0, y: 1)
gradientLayer.locations = [0, 1]
gradientLayer.colors = [start.cgColor, end.cgColor]
self.layer.addSublayer(gradientLayer)
}
}
[Swift] UIView+Extension
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- JKSwiftExtension[https://github.com/JoanKing/JKSwiftExten...
- 在日常代码中,我们常常要重复用到view的一些属性,比如view.center.x、view.frame.orig...
- 在OC中我们通过类目Category做UIView属性宽展,方便我们获取常用属性,(x、y、宽、高等)在Swift...
- 前言: 我们在开发工作中很有可能遇到需要view相应各种手势的需求,使用RunTime+Extension的形式为...