酱油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
    }
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,025评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,474评论 4 61
  • 效果图: 尺码表: 价格: 长袖:98 短袖:79 统计完后统计在群里给猴子直接打钱 订购方式: 直接在群里告诉猴...
    今天能看见山阅读 2,312评论 0 0
  • 2017年,2月2O日,本是一个不普通的日子。因为…这次他们,再次相遇。或许说,是因为她来了。 她,是大家心目中...
    懵A阅读 1,036评论 0 0
  • 不废话月计划来了,相比较来说它更具有实际意义。你可能听过21天养成一个好习惯,在一段不短的时间准确的衡量你自己,并...
    唐苦不苦阅读 3,051评论 0 3

友情链接更多精彩内容