유용한 UIView Extension
Posted on 2021-11-03 by GKSRUDTN99
Swift&Xcode
Xcode
Swift
유용한 UIView Extension
1. 원형으로 View Clip하는 옵션 추가
@IBInspectable var useAutoCornerRadius: Bool {
set {
guard newValue else { return }
layer.cornerRadius = frame.height / 2
clipsToBounds = layer.cornerRadius > 0
}
get {
return self.useAutoCornerRadius
}
}
2. Border Configuration 옵션 추가
@IBInspectable var ibCornerRadius: CGFloat {
set {
layer.cornerRadius = newValue
clipsToBounds = newValue > 0
}
get {
return layer.cornerRadius
}
}
@IBInspectable var ibBorderWidth: CGFloat {
set {
layer.borderWidth = newValue
}
get {
return layer.borderWidth
}
}
@IBInspectable var ibBorderColor: UIColor? {
set {
self.layer.borderColor = newValue?.cgColor
}
get {
guard let color = layer.borderColor else { return nil }
return UIColor(cgColor: color)
}
}
3. Shadow Configuration 옵션 추가
@IBInspectable var shadowRadius: CGFloat {
get { return layer.shadowRadius }
set { layer.shadowRadius = newValue }
}
@IBInspectable var shadowOpacity: CGFloat {
get { return CGFloat(layer.shadowOpacity) }
set { layer.shadowOpacity = Float(newValue) }
}
@IBInspectable var shadowOffset: CGSize {
get { return layer.shadowOffset }
set { layer.shadowOffset = newValue }
}
@IBInspectable var shadowColor: UIColor? {
get {
guard let cgColor = layer.shadowColor else {
return nil
}
return UIColor(cgColor: cgColor)
}
set { layer.shadowColor = newValue?.cgColor }
}