• swift系统学习控件篇:UIbutton+UIlabel+UITextField+UISwitch+UISlider


    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码:

    UIButton+UILabel

      1 //
      2 //  ViewController.swift
      3 //  manyControl
      4 //
      5 //  Created by shaoting on 16/3/23.
      6 //  Copyright © 2016年 9elephas. All rights reserved.
      7 //
      8 /// swift控件篇
      9 //  UIButton
     10 //  UILabel 
     11 import UIKit
     12 
     13 class ViewController: UIViewController {
     14 
     15     override func viewDidLoad() {
     16         super.viewDidLoad()
     17         
     18         makebtn();  //按钮
     19         makelabel();  //UILabel
     20                // Do any additional setup after loading the view, typically from a nib.
     21     }
     22     func makebtn(){
     23         //按钮button
     24         let btn:UIButton = UIButton(type:.Custom)
            btn.frame = CGRect(x:20,y:20, 100,height: 100)
    25 //背景色 26 btn.backgroundColor = UIColor.yellowColor(); 27 //背景图片 28 btn.setImage(UIImage(named: "520"), forState: UIControlState.Normal) 29 //标题文字 30 btn.setTitle("按钮", forState: UIControlState.Normal) 31 //标题颜色 32 btn.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal) 33 //设置按钮标题阴影 34 btn.setTitleShadowColor(UIColor.blackColor(), forState: UIControlState.Normal) 35 //设置按钮阴影 36 btn.titleLabel?.shadowOffset = CGSizeMake(2, 3) 37 //设置按钮标题字体样式 38 btn.titleLabel?.font = UIFont.systemFontOfSize(20) 39 //设置按钮内部内容边距 40 btn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5) 41 //去掉高亮状态下的图像颜色加深 42 btn.adjustsImageWhenHighlighted = false 43 //去掉禁用状态下的图像颜色加深 44 btn.adjustsImageWhenDisabled = false 45 //添加按钮按下的发光效果 46 btn.showsTouchWhenHighlighted = true 47 //添加点击事件 48 btn.addTarget(self, action: "btnOnclick:", forControlEvents: UIControlEvents.AllTouchEvents) 49 //为按钮设置个标签 50 btn.tag = 2 51 self.view.addSubview(btn); 52 // 54 //设置点击事件 55 func btnOnclick(sender:UIButton!){ 56 print("点击了标签为(sender.tag)按钮") 57 } 58 59 60 func makelabel(){ 61 // 普通label 62 let label1:UILabel = UILabel(frame: CGRect(x: 50,y: 100,100,height:50)) 63 label1.text = "普通label"; 64 label1.transform = CGAffineTransformMakeRotation(0.2) //形变角度 65 self.view.addSubview(label1) 66 67 // 圆角label 68 // Label的圆角对其背景不起作用,只对其边框起作用 69 let label2:UILabel = UILabel(frame: CGRect(x: 50, y: 160, 100, height: 50)) 70 label2.text = "圆角label" //设置文字 71 label2.textColor = UIColor.redColor() //文字颜色 72 label2.backgroundColor = UIColor.blackColor() //label颜色 73 label2.textAlignment = NSTextAlignment.Center //文字对齐方式 74 label2.layer.cornerRadius = 10 //剪切圆的半径 75 label2.layer.borderWidth = 2 //边框宽度 76 label2.layer.borderColor = UIColor.redColor().CGColor //边框颜色 77 self.view.addSubview(label2) 78 79 80 // 让label拥有点击事件 81 let label3:UILabel = UILabel(frame: CGRect(x: 50, y: 220, 100, height: 50)) 82 label3.text = "可以点击的label" 83 label3.adjustsFontSizeToFitWidth = true // 根据label的宽度,改变文字的大小 84 let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "labelOnclick:") //创建个手势 85 label3.userInteractionEnabled = true //打开用户交互 86 label3.addGestureRecognizer(tap) //为label添加一个手势 87 label3.shadowColor = UIColor.purpleColor() 88 self.view.addSubview(label3) 89 90 // 让label显示html标签 91 let label4:UILabel = UILabel(frame: CGRect(x: 50, y: 300, 100, height: 50)) 92 let html = "this is html <a href="http://www.baidu.com">跳</a>" 93 let data = html.dataUsingEncoding(NSUTF32StringEncoding, allowLossyConversion: false) 94 do{ 95 let atext = try NSAttributedString(data: data!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil) 96 label4.attributedText = atext 97 }catch{ 98 print(error) 99 } 100 label4.multipleTouchEnabled = true 101 self.view.addSubview(label4) 102 //设置多行文本 103 let label5:UILabel = UILabel(frame: CGRect(x: 50, y: 360, 200, height: 200)) 104 label5.backgroundColor = UIColor.yellowColor() 105 label5.text = "好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,"; 106 label5.lineBreakMode = NSLineBreakMode.ByTruncatingTail //换行模式 107 label5.numberOfLines = 10 108 label5.adjustsFontSizeToFitWidth = true //字号随框大小改变 109 self.view.addSubview(label5) 110 111 } 112 113 func labelOnclick(sender:AnyObject){ 114 print("点击了label") 115 } 116 117 override func didReceiveMemoryWarning() { 118 super.didReceiveMemoryWarning() 119 // Dispose of any resources that can be recreated. 120 } 121 122 123 }

    UITextField+UISwitch+UISlider+UIimageView

      1 //
      2 //  ViewController.swift
      3 //  UITextField+UIImage
      4 //
      5 //  Created by shaoting on 16/3/24.
      6 //  Copyright © 2016年 9elephas. All rights reserved.
      7 //
      8 //     swift 控件学习篇
      9 //     UITextFiled  
     10 //     UISwitch
     11 //     UISlider
     12 //     UIimageView
     13 import UIKit
     14 
     15 class ViewController: UIViewController,UITextFieldDelegate {
     16         var textField:UITextField!
     17         var switch1:UISwitch!
     18         var slider:UISlider!
     19     var imageView:UIImageView!
     20     override func viewDidLoad() {
     21         super.viewDidLoad()
     22         makeTextField();
     23         makeUISwitch();
     24         makeSlide();
     25         
     26         let btn = UIButton(frame: CGRect(x: 200, y: 150,  100, height: 50))
     27         btn.setTitle("复位", forState: .Normal);
     28         btn.addTarget(self, action: Selector("recover:"), forControlEvents: UIControlEvents.AllEvents)
     29         btn.backgroundColor = UIColor.redColor()
     30         btn.tag = 20
     31         self.view.addSubview(btn)
     32         
     33         imageView = UIImageView(frame: CGRect(x: 200, y: 400,  50, height: 50))
     34         imageView.image = UIImage(named: "stop")
     35         self.view.addSubview(imageView);
     36          // Do any additional setup after loading the view, typically from a nib.
     37     }
     38     
     39      func makeSlide(){
     40         slider = UISlider(frame: CGRect(x: 20, y: 200,  400, height: 200))
     41         slider.minimumValue = 0  //最小值
     42         slider.maximumValue = 100  //最大值
     43         slider.value = 20   //默认值
     44         slider.minimumValueImage = UIImage(named: "start")  //最小值图片
     45         slider.maximumValueImage = UIImage(named: "stop") //最大值图片
     46 //        slider.setMinimumTrackImage(UIImage(named: "left"), forState: UIControlState.Normal)//设置滑块左边的轨道图片
     47 //        slider.setMaximumTrackImage(UIImage(named: "right"), forState: UIControlState.Normal)//设置滑块右边的轨道图片
     48         slider.setThumbImage(UIImage(named: "center"), forState: UIControlState.Normal)//设置滑块图片
     49         slider.minimumTrackTintColor = UIColor.brownColor()  //划过的轨道颜色
     50         slider.maximumTrackTintColor = UIColor.yellowColor()  //未划过的轨道颜色
     51         
     52         self.view.addSubview(slider);
     53     }
     54     func recover(sender:UIButton){
     55         slider.setValue(20, animated: true)
     56     }
     57     
     58     func makeUISwitch(){
     59         switch1 = UISwitch(frame: CGRect(x: 30, y: 150,  400, height: 10))
     60         switch1.on = true //默认ON是否打开
     61         switch1.tintColor = UIColor.redColor()
     62         switch1.onTintColor = UIColor.yellowColor()
     63         switch1.thumbTintColor = UIColor.blackColor()
     64         switch1.addTarget(self, action: Selector("switchChane:"), forControlEvents: UIControlEvents.AllEvents)
     65         self.view.addSubview(switch1)
     66     }
     67     func  switchChane(senger:UISwitch){
     68         if senger.on{
     69           print("is ON")
     70         }else{
     71           print("is OFF")
     72         }
     73     }
     74     
     75     
     76     
     77     
     78     
     79     func makeTextField(){
     80     textField = UITextField(frame: CGRect(x: 30, y: 50,  self.view.frame.size.width-60, height: 70))
     81     //         textField.borderStyle = UITextBorderStyle.RoundedRect
     82     textField.borderStyle = .Line   //边框样式
     83     textField.placeholder = "请输入内容" //占位符
     84     textField.text = "username"  //为textFiled设置默认文字
     85     textField.delegate = self
     86     textField.keyboardType = .EmailAddress   //键盘类型
     87     self.view.addSubview(textField)
     88         }
     89     //UITextFieldDelegate中的方法
     90     func textFieldDidBeginEditing(textField: UITextField) {
     91         print("开始编辑")
     92     }
     93     func textFieldDidEndEditing(textField: UITextField) {
     94         print("结束编辑")
     95     }
     96     //点击return按钮隐藏键盘
     97     func  textFieldShouldReturn(textField: UITextField) -> Bool {
     98         textField.resignFirstResponder()
     99         return true
    100     }
    101     //点击编辑框外部,隐藏键盘
    102     override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    103         textField.resignFirstResponder()
    104     }
    105     
    106     override func didReceiveMemoryWarning() {
    107         super.didReceiveMemoryWarning()
    108         // Dispose of any resources that can be recreated.
    109     }
    110 
    111     
    112     
    113 
    114 }

    源码下载地址:

    UIButton+UILabel

    http://download.csdn.net/detail/shaoting19910730/9471728

    https://github.com/pheromone/UIButton-UILabel

    UITextField+UISwitch+UISlider+UIimageView:

    http://download.csdn.net/detail/shaoting19910730/9471731

    https://github.com/pheromone/UITextField-UISwitch-UISlider-UIimageView

    swift 学习网站

    http://www.helloswift.com.cn/

  • 相关阅读:
    生成8位随机字符串
    Python字符串反转
    dd备份文件系统
    多线程mtr-代码
    Sysctl命令及linux内核参数调整
    解决系统存在大量TIME_WAIT状态的连接
    tcpkill清除异常tcp连接
    graphite
    sed 中带变量的情况
    JAVA的Random类
  • 原文地址:https://www.cnblogs.com/shaoting/p/5316596.html
Copyright © 2020-2023  润新知