IOS基本 控件系列

自定义形状背景

   //创建路径
        let path = UIBezierPath.init(roundedRect: CGRect.init(x: 0, y: 0, width: YYScreenWidth, height: 15), byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize.init(width: 15, height: 0))
        
        let shapeLayer = CAShapeLayer()
        shapeLayer.frame = CGRect.init(x: 0, y:0, width: 300, height: 300)
        shapeLayer.path = path.cgPath
        shapeLayer.lineWidth = 2
        shapeLayer.strokeColor = UIColor.white.cgColor
        shapeLayer.fillColor = UIColor.white.cgColor
        self.radiusBtn.layer.addSublayer(shapeLayer)

image.png
    // 标签
    private var paidLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 9)
        label.textColor = AppBlue
        label.layer.masksToBounds = true
        label.layer.cornerRadius = 2.0
        label.layer.borderColor = AppBlue.cgColor
        label.layer.borderWidth = 1
        label.text = ""
//        label.contentInset = UIEdgeInsetsMake(20, 20, 20, 20)
//        label.intrinsicContentSize = CGSize(10, 10, 10, 10)
        label.textAlignment = NSTextAlignment.center
        return label
    }()

image.png

带占位符的当行文本输入框

import Foundation
import UIKit
class CustomTextView: UITextView {
    private var _placeholder:String!
    
    init(frame:CGRect,placeholder:String) {
        super.init(frame: frame, textContainer: nil)
        _placeholder = placeholder
        NotificationCenter.default.addObserver(self, selector: #selector(textDidChange), name: .UITextViewTextDidChange, object: self)//观察是否有文字输入
    }
    
  
    override func draw(_ rect: CGRect) {
        if hasText{
            return//如果输入框内有文字,直接返回
        }
        _placeholder.draw(in: CGRect(x:5,y:7,width:rect.width-10,height:rect.height), withAttributes: [NSAttributedStringKey.foregroundColor:UIColor.lightGray,NSAttributedStringKey.font:UIFont.systemFont(ofSize: 10)])//占位符的位置坐标,字体大小以及绘制区域大小,如果你对占位符的大小和位置不满意,请在这修改
    }
    
    @objc func textDidChange(){
        setNeedsDisplay()//调用drawRect方法,切勿手动调用drawRect方法
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

image.png
    lazy var contactView :UIView = {
        let view = UIView()
        //        view.backgroundColor = UIColor.lightGray
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 2.0
        view.backgroundColor = UIColor.white
//        view.layer.borderWidth = 1
        return view
    }()
image.png

你可能感兴趣的:(IOS基本 控件系列)