swift系统学习控件篇:UIbutton+UIlabel+UITextField+UISwitch+UISlider
2016-03-24 18:13
567 查看
工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码:
UIButton+UILabel
UITextField+UISwitch+UISlider+UIimageView
源码下载地址:
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/
UIButton+UILabel
// // ViewController.swift // manyControl // // Created by shaoting on 16/3/23. // Copyright © 2016年 9elephas. All rights reserved. // /// swift控件篇 // UIButton // UILabel import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() makebtn(); //按钮 makelabel(); //UILabel // Do any additional setup after loading the view, typically from a nib. } func makebtn(){ //按钮button let btn:UIButton = UIButton(type:.Custom) btn.frame = CGRect(x:20,y:20,width: 100,height: 100) //背景色 btn.backgroundColor = UIColor.yellowColor(); //背景图片 btn.setImage(UIImage(named: "520"), forState: UIControlState.Normal) //标题文字 btn.setTitle("按钮", forState: UIControlState.Normal) //标题颜色 btn.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal) //设置按钮标题阴影 btn.setTitleShadowColor(UIColor.blackColor(), forState: UIControlState.Normal) //设置按钮阴影 btn.titleLabel?.shadowOffset = CGSizeMake(2, 3) //设置按钮标题字体样式 btn.titleLabel?.font = UIFont.systemFontOfSize(20) //设置按钮内部内容边距 btn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5) //去掉高亮状态下的图像颜色加深 btn.adjustsImageWhenHighlighted = false //去掉禁用状态下的图像颜色加深 btn.adjustsImageWhenDisabled = false //添加按钮按下的发光效果 btn.showsTouchWhenHighlighted = true //添加点击事件 btn.addTarget(self, action: "btnOnclick:", forControlEvents: UIControlEvents.AllTouchEvents) //为按钮设置个标签 btn.tag = 2 self.view.addSubview(btn); // //设置点击事件 func btnOnclick(sender:UIButton!){ print("点击了标签为\(sender.tag)按钮") } func makelabel(){ // 普通label let label1:UILabel = UILabel(frame: CGRect(x: 50,y: 100,width:100,height:50)) label1.text = "普通label"; label1.transform = CGAffineTransformMakeRotation(0.2) //形变角度 self.view.addSubview(label1) // 圆角label // Label的圆角对其背景不起作用,只对其边框起作用 let label2:UILabel = UILabel(frame: CGRect(x: 50, y: 160, width: 100, height: 50)) label2.text = "圆角label" //设置文字 label2.textColor = UIColor.redColor() //文字颜色 label2.backgroundColor = UIColor.blackColor() //label颜色 label2.textAlignment = NSTextAlignment.Center //文字对齐方式 label2.layer.cornerRadius = 10 //剪切圆的半径 label2.layer.borderWidth = 2 //边框宽度 label2.layer.borderColor = UIColor.redColor().CGColor //边框颜色 self.view.addSubview(label2) // 让label拥有点击事件 let label3:UILabel = UILabel(frame: CGRect(x: 50, y: 220, width: 100, height: 50)) label3.text = "可以点击的label" label3.adjustsFontSizeToFitWidth = true // 根据label的宽度,改变文字的大小 let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "labelOnclick:") //创建个手势 label3.userInteractionEnabled = true //打开用户交互 label3.addGestureRecognizer(tap) //为label添加一个手势 label3.shadowColor = UIColor.purpleColor() self.view.addSubview(label3) // 让label显示html标签 let label4:UILabel = UILabel(frame: CGRect(x: 50, y: 300, width: 100, height: 50)) let html = "this is html <a href=\"http://www.baidu.com\">跳</a>" let data = html.dataUsingEncoding(NSUTF32StringEncoding, allowLossyConversion: false) do{ let atext = try NSAttributedString(data: data!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil) label4.attributedText = atext }catch{ print(error) } label4.multipleTouchEnabled = true self.view.addSubview(label4) //设置多行文本 let label5:UILabel = UILabel(frame: CGRect(x: 50, y: 360, width: 200, height: 200)) label5.backgroundColor = UIColor.yellowColor() label5.text = "好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,好多好多文本,"; label5.lineBreakMode = NSLineBreakMode.ByTruncatingTail //换行模式 label5.numberOfLines = 10 label5.adjustsFontSizeToFitWidth = true //字号随框大小改变 self.view.addSubview(label5) } func labelOnclick(sender:AnyObject){ print("点击了label") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
UITextField+UISwitch+UISlider+UIimageView
// // ViewController.swift // UITextField+UIImage // // Created by shaoting on 16/3/24. // Copyright © 2016年 9elephas. All rights reserved. // // swift 控件学习篇 // UITextFiled // UISwitch // UISlider // UIimageView import UIKit class ViewController: UIViewController,UITextFieldDelegate { var textField:UITextField! var switch1:UISwitch! var slider:UISlider! var imageView:UIImageView! override func viewDidLoad() { super.viewDidLoad() makeTextField(); makeUISwitch(); makeSlide(); let btn = UIButton(frame: CGRect(x: 200, y: 150, width: 100, height: 50)) btn.setTitle("复位", forState: .Normal); btn.addTarget(self, action: Selector("recover:"), forControlEvents: UIControlEvents.AllEvents) btn.backgroundColor = UIColor.redColor() btn.tag = 20 self.view.addSubview(btn) imageView = UIImageView(frame: CGRect(x: 200, y: 400, width: 50, height: 50)) imageView.image = UIImage(named: "stop") self.view.addSubview(imageView); // Do any additional setup after loading the view, typically from a nib. } func makeSlide(){ slider = UISlider(frame: CGRect(x: 20, y: 200, width: 400, height: 200)) slider.minimumValue = 0 //最小值 slider.maximumValue = 100 //最大值 slider.value = 20 //默认值 slider.minimumValueImage = UIImage(named: "start") //最小值图片 slider.maximumValueImage = UIImage(named: "stop") //最大值图片 // slider.setMinimumTrackImage(UIImage(named: "left"), forState: UIControlState.Normal)//设置滑块左边的轨道图片 // slider.setMaximumTrackImage(UIImage(named: "right"), forState: UIControlState.Normal)//设置滑块右边的轨道图片 slider.setThumbImage(UIImage(named: "center"), forState: UIControlState.Normal)//设置滑块图片 slider.minimumTrackTintColor = UIColor.brownColor() //划过的轨道颜色 slider.maximumTrackTintColor = UIColor.yellowColor() //未划过的轨道颜色 self.view.addSubview(slider); } func recover(sender:UIButton){ slider.setValue(20, animated: true) } func makeUISwitch(){ switch1 = UISwitch(frame: CGRect(x: 30, y: 150, width: 400, height: 10)) switch1.on = true //默认ON是否打开 switch1.tintColor = UIColor.redColor() switch1.onTintColor = UIColor.yellowColor() switch1.thumbTintColor = UIColor.blackColor() switch1.addTarget(self, action: Selector("switchChane:"), forControlEvents: UIControlEvents.AllEvents) self.view.addSubview(switch1) } func switchChane(senger:UISwitch){ if senger.on{ print("is ON") }else{ print("is OFF") } } func makeTextField(){ textField = UITextField(frame: CGRect(x: 30, y: 50, width: self.view.frame.size.width-60, height: 70)) // textField.borderStyle = UITextBorderStyle.RoundedRect textField.borderStyle = .Line //边框样式 textField.placeholder = "请输入内容" //占位符 textField.text = "username" //为textFiled设置默认文字 textField.delegate = self textField.keyboardType = .EmailAddress //键盘类型 self.view.addSubview(textField) } //UITextFieldDelegate中的方法 func textFieldDidBeginEditing(textField: UITextField) { print("开始编辑") } func textFieldDidEndEditing(textField: UITextField) { print("结束编辑") } //点击return按钮隐藏键盘 func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() return true } //点击编辑框外部,隐藏键盘 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { textField.resignFirstResponder() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
源码下载地址:
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/
相关文章推荐
- swift约束之SnapKit的使用方式
- Swift与C char*
- Swift 中 callback 调用
- Swift中C的Block的调用
- Swift-'as?','as'用法
- swift 单例
- swift switch
- Swift-'!','?'用法
- Swift-Debug下打印函数名和行数
- 自主学习之RxSwift(一) -----Driver
- 自主学习之RxSwift(一) -----Driver
- swift !?
- Swift语法
- swift2.2的新特性
- RxSwift:ReactiveX for Swift 翻译
- RxSwift:ReactiveX for Swift 翻译
- ios/swift上传代码到CocoaPods
- OC+swift混编
- Swift中根据字符串创建对象
- swift与OC之间不得不知道的21点