美文网首页
Swift - 生成各种控件的工厂类(包含标签,按钮,输入框等)

Swift - 生成各种控件的工厂类(包含标签,按钮,输入框等)

作者: 小驴拉磨 | 来源:发表于2020-07-03 15:44 被阅读0次

在iOS开发中,页面里有时会大量的用到一些控件,如果要一个个单独创建再设置样式的话就显得很麻烦。我们可以创建一个生成各种控件的工厂类,这样在需要的时候调用下就可以了。

下面以一个自定义的工厂类为例,其中提供了文本标签,按钮,文本输入框,分段单选控件的生成,效果图如下:


Simulator Screen Shot - iPhone SE (2nd generation) - 2020-07-03 at 15.41.10.png

工厂类:ViewFactory.swift


import UIKit

enum ViewType {
    case label
    case button
    case text
    case segment
}

class ViewFactory {

    /// 默认控件的属性
    class func getDefaultFrame() -> CGRect
    {
        let defaulltFrame = CGRect(x: 0, y: 0, width: 100, height: 30)
        return defaulltFrame
    }
    
    /// 工厂方法
    class func creatView(type: ViewType, titles: [String]?, action: Selector?, target: AnyObject?) -> UIView
    {
        switch type {
        case .label:
            return ViewFactory.createLabel(title: titles?[0])
        case .button:
            return ViewFactory.createButton(title: titles?[0], action: action, target: target)
        case .text:
            return ViewFactory.createTextField(value: titles?[0], action: action, target: (target as? UITextFieldDelegate))
        case .segment:
            return ViewFactory.createSegment(items: titles, action: action, target: target)
        default:
            break
        }
        return UIView()
    }
    
    /// 创建label
    class func createLabel(title: String?) -> UILabel
    {
        let label = UILabel()
        label.textColor = UIColor.black
        label.backgroundColor = UIColor.white
        label.text = title;
        label.frame = ViewFactory.getDefaultFrame()
        label.font =  UIFont(name: "HelveticaNeue-Bold", size: 16)
        return label
    }
    
    /// 创建button
    class func createButton(title:String?, action:Selector?, target:AnyObject?) -> UIButton
    {
        let button = UIButton(frame:ViewFactory.getDefaultFrame())
        button.backgroundColor = UIColor.orange
        button.setTitle(title, for: .normal)
        button.titleLabel!.textColor = UIColor.white
        button.titleLabel!.font = UIFont.systemFont(ofSize: 14)
        if action != nil && target != nil {
            button.addTarget(target, action: action!, for: .touchUpInside)
        }
        
        return button
    }
    
    /// 创建文本输入框UITextField
    class func createTextField(value: String?, action: Selector?, target: UITextFieldDelegate?) -> UITextField
    {
        let textField = UITextField(frame:ViewFactory.getDefaultFrame())
        textField.backgroundColor = UIColor.clear
        textField.textColor = UIColor.black
        textField.text = value
        textField.borderStyle = UITextField.BorderStyle.roundedRect
        textField.adjustsFontSizeToFitWidth = true
        textField.delegate = target
        return textField
    }
    
    ///  创建分段单选控件
    class func createSegment(items: [String]?, action: Selector?, target: AnyObject?) -> UISegmentedControl
    {
        let segment = UISegmentedControl(items:items)
        segment.frame = ViewFactory.getDefaultFrame()
        segment.isMomentary = false
        if action != nil && target != nil {
            segment.addTarget(target, action: action!, for: .valueChanged)
        }
        return segment
    }

}

工厂类的使用:

extension ViewController
{
    func setupControls()
    {
        //创建文本标签
//        let labelNum = ViewFactory.createLabel(title: "阈值:")
        let labelNum = ViewFactory.creatView(type: .label, titles: ["阈值:"], action: nil, target: nil)
        labelNum.frame = CGRect(x: 20, y: 100, width: 60, height: 30)
        self.view.addSubview(labelNum)
         
//        let labelDm = ViewFactory.createLabel(title: "维度:")
        let labelDm = ViewFactory.creatView(type: .label, titles: ["维度:"], action: nil, target: nil)
        labelDm.frame = CGRect(x: 20, y: 200, width: 60, height: 30)
        self.view.addSubview(labelDm)
         
        //创建文本输入框
//        txtNum = ViewFactory.createTextField(value: "", action: nil, target: self)
        txtNum = (ViewFactory.creatView(type: .text, titles: nil, action: nil, target: self) as! UITextField)
        txtNum.frame = CGRect(x:80,y:100,width:200,height:30)
        txtNum.returnKeyType = UIReturnKeyType.done
        self.view.addSubview(txtNum)
         
        //创建分段单选控件
//        segDimension = ViewFactory.createSegment(items: ["3x3", "4x4", "5x5"], action: #selector(dimensionChanged), target: self)
        segDimension = (ViewFactory.creatView(type: .segment, titles: ["3x3", "4x4", "5x5"], action: nil, target: self) as! UISegmentedControl)
        segDimension.frame = CGRect(x:80,y: 200,width: 200,height: 30)
        self.view.addSubview(segDimension)
        segDimension.selectedSegmentIndex = 1
         
        //创建按钮控件
//        btn = ViewFactory.createButton(title: "确定", action: #selector(dimensionChanged), target: self)
        btn = (ViewFactory.creatView(type: .button, titles: ["确定"], action: #selector(dimensionChanged), target: self) as! UIButton)
        btn.frame.origin = CGPoint(x: 80, y: 300)
        self.view.addSubview(btn)
    }
     
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        //收起键盘
        txtNum.resignFirstResponder()
        //打印出文本框中的值
        print(txtNum.text)
        return true
    }
     
    @objc func dimensionChanged(sender:AnyObject) {
        print("dimensionChanged")
    }

原文出自:www.hangge.com 转载请保留原文链接:https://www.hangge.com/blog/cache/detail_655.html

相关文章

网友评论

      本文标题:Swift - 生成各种控件的工厂类(包含标签,按钮,输入框等)

      本文链接:https://www.haomeiwen.com/subject/qlxhqktx.html