酱油03-实现一些小控件

实现简单的小控件

label/progress

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置Vlabel的位置
//        let vlabel = Vlabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 400))
//        vlabel.backgroundColor = UIColor.redColor()
//        vlabel.label.numberOfLines = 0//设置分行
//        vlabel.vLabelMode = .center//设置对齐格式
//        vlabel.label.backgroundColor = UIColor.brownColor()
//        vlabel.string = "Sometimes view controllers that are using showViewController:sender and showDetailViewController:sender: will need to know when the split view controller environment above it has changed. This notification will be posted when that happens (for example, when a split view controller is collapsing or expanding). The NSNotification's object will be the view controller that caused the change."
//        let rect = vlabel.label.textRectForBounds(vlabel.bounds, limitedToNumberOfLines: 0)
//        print(rect)
//        
//        self.view.addSubview(vlabel)
        //设置Progress的位置
        let progress = Progress(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
        progress.p = 0.5//设置progress的值
        self.view.addSubview(progress)  
    }
}

label


import UIKit
//定义枚举(对齐格式)
enum VlableMode {
    case top
    case bottom
    case center
}

class Vlabel: UIView {
    
    var label = UILabel()
    //设置label的text
    var string : String!{
        set{
            self.label.text = newValue
        }
        get{
            return self.label.text
        }
    }
    var vLabelMode = VlableMode.top
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(label)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.addSubview(label)
    }
    //调用layoutSubviews方法
    override func layoutSubviews() {
        var rect = label.textRectForBounds(self.bounds, limitedToNumberOfLines: 0)
        //判断对齐格式
        switch  vLabelMode{
        case .top:
            break
        case .bottom:
            rect.origin.y = self.bounds.size.height - rect.size.height
        default:
            rect.origin.y = self.bounds.size.height/2 - rect.size.height/2
        }
        label.frame = rect
    }
}

progress

import UIKit

class Progress: UIView {
    //1.创建UIView对象
    var proview = UIView()
    var p : CGFloat!
    //定义progress
    var progress : CGFloat{
        //判断进度条的值的范围
        set{
            if newValue > 1{
                p = 1
            }else{
                if newValue < 0{
                    p = 0
                }else{
                    p = newValue
                }
            }
            //自动调用系统的layoutSubview
            self.setNeedsLayout()
        }
        get{
            return p
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(proview)
        setcolor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.addSubview(proview)
         setcolor()
    }
    
    func setcolor(){
        self.backgroundColor = UIColor.redColor()
        proview.backgroundColor = UIColor.brownColor()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        let rect = CGRect(x: 0, y: 0, width: self.frame.size.width*progress, height: self.frame.size.height)
        proview.frame = rect
    }
    
}

你可能感兴趣的:(酱油03-实现一些小控件)