服务器之家

服务器之家 > 正文

swift控件工厂类的实现代码

时间:2021-01-07 15:38     来源/作者:稻草人11223

控件工厂类,简而言之就是,减少代码的复用率,只在哪里用,然后在哪里调:

代码如下:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import UIKit
 
class ViewFactory: UIView,UITextFieldDelegate {
 
  //默认控件的尺寸
  class func getDefaultFrame( ) -> CGRect
  {
    let defaultFrame = CGRect(x:0,y:0,width:100,height:30)
    return defaultFrame
  }
  
  //类方法
  class func createControl(type:String,title:[String],action:Selector,sender:AnyObject) -> UIView
  {
    switch type {
    case "label":
      return ViewFactory.creatLabel(title: title[0])
      case "button":
      return ViewFactory.createButton(title: title[0], action: action, sender: sender as! UIViewController)
      case "text":
      return ViewFactory.creatTextField(value: title[0], action: action, sender: sender as! UIViewController as UIViewController as! UITextFieldDelegate)
      case "segment":
      return ViewFactory.creatSegment(items: [title[0]], action: action, sender: sender as! UIViewController)
    default:
      return ViewFactory.creatLabel(title: title[0])
    }
  }
  
  //创建按钮控件
  class func createButton(title:String, action:Selector, sender:UIViewController)
    -> 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)
      button.addTarget(sender, action:action, for:.touchUpInside)
      return button
  }
  
  //创建文本输入框控件
  class func creatTextField(value:String,action:Selector,sender:UITextFieldDelegate) -> UITextField
  {
    let textField = UITextField(frame:ViewFactory.getDefaultFrame())
    textField.backgroundColor = UIColor.clear
    textField.textColor = UIColor.black
    textField.text = value
    textField.borderStyle = .roundedRect
    textField.adjustsFontSizeToFitWidth = true
    textField.delegate = sender
    return textField
  }
  
  //创建分段单选组件
  class func creatSegment(items:[String],action:Selector,sender:UIViewController) -> UISegmentedControl
  {
    let segment = UISegmentedControl(items:items)
    segment.frame = ViewFactory.getDefaultFrame()
    segment.isMomentary = false
    segment.addTarget(self, action: action, for: .valueChanged)
    return segment
  }
  
  //创建文本标签控件
  class func creatLabel(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:"微软雅黑",size:16)
    return label
    
  }
}

调用:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
func initVIewFactory()
 {
   //创建文本标签
   let labelNum = ViewFactory.creatLabel(title: "阈值")
   labelNum.frame = CGRect(x:20,y:100,width:60,height:30)
   self.view.addSubview(labelNum)
   
   let labelDm = ViewFactory.creatLabel(title: "维度")
   labelDm.frame = CGRect(x:20,y:200,width:60,height:30)
   self.view.addSubview(labelDm)
   
   //创建文本输入框
   textNum = ViewFactory.creatTextField(value: "", action:#selector(factoryAction), sender: self as UITextFieldDelegate)
   textNum.frame = CGRect(x:80,y:100,width:200,height:30)
   textNum.returnKeyType = .done
   self.view.addSubview(textNum)
   
   let textNumSecond = ViewFactory.creatTextField(value: "", action: #selector(factoryActionSecond), sender: self as UITextFieldDelegate)
   textNumSecond.frame = CGRect(x:80,y:200,width:200,height:30)
   textNum.returnKeyType = .done
   self.view.addSubview(textNumSecond)
   
   //创建分段单选控件
   segmentC = ViewFactory.creatSegment(items: ["3*3","4*4","5*5"], action: #selector(segmentAction), sender: self)
   segmentC.frame = CGRect(x:80,y:200,width:200,height:30)
   self.view.addSubview(segmentC)
   segmentC.selectedSegmentIndex = 0
   
   //创建按钮控件
   factorybtn = ViewFactory.createButton(title: "确定", action: #selector(factoryClick), sender: self)
   factorybtn.frame.origin = CGPoint(x:80,y:300)
   self.view.addSubview(factorybtn)
   
 }
 
 func factoryAction()
 {
   
 }
 
 func factoryActionSecond()
 {
   
 }
 
 func segmentAction()
 {
   
 }
 
 func factoryClick()
 {
   print("我点击了")
 }

效果如下:

swift控件工厂类的实现代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/hero11223/p/7610081.html

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部