swift:使用协议protocol设置颜色,UIImage的切圆角ImageWithCornerRadius
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift:使用协议protocol设置颜色,UIImage的切圆角ImageWithCornerRadius相关的知识,希望对你有一定的参考价值。
//使用协议 方便以后切换颜色配置文件、或者做主题配色之类乱七八糟产品经理最爱的功能
protocol V2EXColorProtocol{
var v2_backgroundColor: UIColor { get }
var v2_navigationBarTintColor: UIColor { get }
var v2_TopicListTitleColor : UIColor { get }
var v2_TopicListUserNameColor : UIColor { get }
var v2_TopicListDateColor : UIColor { get }
var v2_LinkColor : UIColor { get }
var v2_TextViewBackgroundColor: UIColor { get }
var v2_CellWhiteBackgroundColor : UIColor { get }
var v2_NodeBackgroundColor : UIColor { get }
var v2_SeparatorColor : UIColor { get }
var v2_LeftNodeBackgroundColor : UIColor { get }
var v2_LeftNodeBackgroundHighLightedColor : UIColor { get }
var v2_LeftNodeTintColor: UIColor { get }
/// 小红点背景颜色
var v2_NoticePointColor : UIColor { get }
var v2_ButtonBackgroundColor : UIColor { get }
}
class V2EXDefaultColor: NSObject,V2EXColorProtocol {
static let sharedInstance = V2EXDefaultColor()
fileprivate override init(){
super.init()
}
var v2_backgroundColor : UIColor{
get{
return colorWith255RGB(242, g: 243, b: 245);
}
}
var v2_navigationBarTintColor : UIColor{
get{
return colorWith255RGB(102, g: 102, b: 102);
}
}
var v2_TopicListTitleColor : UIColor{
get{
return colorWith255RGB(15, g: 15, b: 15);
}
}
var v2_TopicListUserNameColor : UIColor{
get{
return colorWith255RGB(53, g: 53, b: 53);
}
}
var v2_TopicListDateColor : UIColor{
get{
return colorWith255RGB(173, g: 173, b: 173);
}
}
var v2_LinkColor : UIColor {
get {
return colorWith255RGB(119, g: 128, b: 135)
}
}
var v2_TextViewBackgroundColor :UIColor {
get {
return colorWith255RGB(255, g: 255, b: 255)
}
}
var v2_CellWhiteBackgroundColor :UIColor {
get {
return colorWith255RGB(255, g: 255, b: 255)
}
}
var v2_NodeBackgroundColor : UIColor {
get {
return colorWith255RGB(242, g: 242, b: 242)
}
}
var v2_SeparatorColor : UIColor {
get {
return colorWith255RGB(190, g: 190, b: 190)
}
}
var v2_LeftNodeBackgroundColor : UIColor {
get {
return colorWith255RGBA(255, g: 255, b: 255, a: 76)
}
}
var v2_LeftNodeBackgroundHighLightedColor : UIColor {
get {
return colorWith255RGBA(255, g: 255, b: 255, a: 56)
}
}
var v2_LeftNodeTintColor : UIColor {
get {
return colorWith255RGBA(0, g: 0, b: 0, a: 140)
}
}
var v2_NoticePointColor : UIColor {
get {
return colorWith255RGB(207, g: 70, b: 71)
}
}
var v2_ButtonBackgroundColor : UIColor {
get {
return colorWith255RGB(85, g: 172, b: 238)
}
}
}
/////////////////////////
import UIKit
extension UIImage {
func roundedCornerImageWithCornerRadius(_ cornerRadius:CGFloat) -> UIImage {
let w = self.size.width
let h = self.size.height
var targetCornerRadius = cornerRadius
if cornerRadius < 0 {
targetCornerRadius = 0
}
if cornerRadius > min(w, h) {
targetCornerRadius = min(w,h)
}
let imageFrame = CGRect(x: 0, y: 0, width: w, height: h)
UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale)
UIBezierPath(roundedRect: imageFrame, cornerRadius: targetCornerRadius).addClip()
self.draw(in: imageFrame)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
使用的地方
/// 节点信息label的圆角背景图
fileprivate static var nodeBackgroundImage_Default =
createImageWithColor( V2EXDefaultColor.sharedInstance.v2_NodeBackgroundColor ,size: CGSize(width: 10, height: 20))
.roundedCornerImageWithCornerRadius(2)
.stretchableImage(withLeftCapWidth: 3, topCapHeight: 3)
以上是关于swift:使用协议protocol设置颜色,UIImage的切圆角ImageWithCornerRadius的主要内容,如果未能解决你的问题,请参考以下文章
swift protocol(协议) associatedtype关联类型