UIView+Extension
扩展方法,下同
import UIKit extension UIView { //扩展方法加载这里 }
自定义控件圆角位置 如:只左上 左下有圆角
func addCorner(roundingCorners: UIRectCorner,cornerSize: CGSize) { let path = UIBezierPath(roundedRect: bounds,byRoundingCorners: roundingCorners,cornerRadii: cornerSize) let cornerLayer = CAShapeLayer() cornerLayer.frame = bounds cornerLayer.path = path.cgPath layer.mask = cornerLayer }
使用
btn.addCorner(roundingCorners: [.topLeft,.bottomLeft],cornerSize: CGSize(width:4,height:4))
UIImage+Extension
修剪图片尺寸
func imageByCroppingImage(_ size: CGSize) -> UIImage { var returnImage: UIImage? var scaleFactor: CGFloat = 1.0 var scaledWidth = size.width var scaledHeight = size.height var thumbnailPoint = CGPoint(x: 0,y: 0) if !self.size.equalTo(size) { let widthFactor = size.width / self.size.width let heightFactor = size.height / self.size.height if widthFactor > heightFactor { scaleFactor = widthFactor } else { scaleFactor = heightFactor } scaledWidth = self.size.width * scaleFactor scaledHeight = self.size.height * scaleFactor if widthFactor > heightFactor { thumbnailPoint.y = (size.height - scaledHeight) * 0.5 } else if widthFactor < heightFactor { thumbnailPoint.x = (size.width - scaledWidth) * 0.5 } } UIGraphicsBeginImageContextWithOptions(size,true,1) var thumbnailRect = CGRect.zero thumbnailRect.origin = thumbnailPoint thumbnailRect.size.width = scaledWidth thumbnailRect.size.height = scaledHeight self.draw(in: thumbnailRect) returnImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return returnImage! }
设置圆形图片
func circleImage() -> UIImage { //取最短边长 let shotest = min(self.size.width,self.size.height) //输出尺寸 let outputRect = CGRect(x: 0,y: 0,width: shotest,height: shotest) //开始图片处理上下文(由于输出的图不会进行缩放,所以缩放因子等于屏幕的scale即可) UIGraphicsBeginImageContextWithOptions(outputRect.size,false,0) let context = UIGraphicsGetCurrentContext()! //添加圆形裁剪区域 context.addEllipse(in: outputRect) context.clip() //绘制图片 self.draw(in: CGRect(x: (shotest-self.size.width)/2,y: (shotest-self.size.height)/2,width: self.size.width,height: self.size.height)) //获得处理后的图片 let maskedImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() return maskedImage }
UIButton+Extension
设置UIButton 图片文字方向和距离
//枚举图片的位置 enum ButtonImageEdgeInsetsStyle { case top //上图下文字 case left //左图右文字 case bottom //下图上文字 case right //右图左文字 } // style:图片位置 space:图片与文字的距离 func layoutButtonImageEdgeInsetsStyle(style:ButtonImageEdgeInsetsStyle,space:CGFloat) { let imageWidth:CGFloat = (self.imageView?.frame.size.width)! let imageHeight:CGFloat = (self.imageView?.frame.size.height)! var labelWidth:CGFloat = 0 var labelHeight:CGFloat = 0 labelWidth = (self.titleLabel?.intrinsicContentSize.width)! labelHeight = (self.titleLabel?.intrinsicContentSize.height)! var imageEdgeInsets:UIEdgeInsets = UIEdgeInsets.zero var labelEdgeInsets:UIEdgeInsets = UIEdgeInsets.zero switch style { case .top: imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0,-labelWidth) labelEdgeInsets = UIEdgeInsetsMake(0,-imageWidth,-imageHeight-space/2.0,0) case .left: imageEdgeInsets = UIEdgeInsetsMake(0,-space/2.0,space/2.0) labelEdgeInsets = UIEdgeInsetsMake(0,space/2.0,-space/2.0) case .bottom: imageEdgeInsets = UIEdgeInsetsMake(0,-labelHeight-space/2.0,-labelWidth) labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0,0) case .right: imageEdgeInsets = UIEdgeInsetsMake(0,labelWidth+space/2.0,-labelWidth-space/2.0) labelEdgeInsets = UIEdgeInsetsMake(0,-labelWidth-space/2.0,labelWidth+space/2.0) default: break } self.titleEdgeInsets = labelEdgeInsets self.imageEdgeInsets = imageEdgeInsets }