Swift学习第六枪-UIButton和UILable
发布时间:2020-12-14 06:58:13 所属栏目:百科 来源:网络整理
导读:UIButton和UILable的学习 从今天开始学习基本控件,先从按钮和标签开始。 1.UIButton相关属性 构造方法(UIButton(type:UIButtonType.InfoDark)) 位置和大小 :frame 背景颜色 :backgroundColor 前景颜色 : tintColor 文字 : setTitle 添加点击事件 :func
UIButton和UILable的学习从今天开始学习基本控件,先从按钮和标签开始。 1.UIButton相关属性
2.代码的实现let btn1 = UIButton(type:UIButtonType.InfoDark)
btn1.frame = CGRectMake(130,80,40,40)
let btn2 = UIButton(type:UIButtonType.RoundedRect)
//设置按钮的位置和大小
btn2.frame = CGRectMake(80,180,150,44)
//设置按钮的背景颜色
btn2.backgroundColor = UIColor.purpleColor()
//设置按钮的前景颜色
btn2.tintColor = UIColor.yellowColor()
//设置按钮的文字
btn2.setTitle("Press ON",forState: .Normal)
//设置按钮的点击事件
btn2.addTarget(self,action: #selector(UIBUttonViewController.buttonTag(_:)),forControlEvents: UIControlEvents.TouchUpInside)
//为按钮设置标记
btn2.tag = 20
let btn3 = UIButton(type:UIButtonType.RoundedRect)
btn3.backgroundColor = UIColor.brownColor()
btn3.tintColor = UIColor.whiteColor()
btn3.setTitle("Press Off",forState: .Normal)
btn3.frame = CGRectMake(80,280,44)
btn3.layer.masksToBounds = true
//设置按钮的圆角半径为10
btn3.layer.cornerRadius = 5
//设置按钮的边框宽度为4
btn3.layer.borderWidth = 1
//设置按钮的边框颜色
btn3.layer.borderColor = UIColor.lightGrayColor().CGColor
self.view.addSubview(btn1)
self.view.addSubview(btn2)
self.view.addSubview(btn3)
/** *按钮的事件处理 **/
func buttonTag(btn:UIButton) {
let alter = UIAlertController(title: "Information",message: "Button Event",preferredStyle: UIAlertControllerStyle.Alert)
let oKAction = UIAlertAction(title: "OK",style: UIAlertActionStyle.Default,handler: nil)
alter.addAction(oKAction)
self.presentViewController(alter,animated: true,completion: nil)
}
3 .UILable的相关属性
4.UILable的代码实现let rect = CGRectMake(20,440,80)
let lable = UILabel(frame: rect)
lable.text = "Hello Lable"
let font = UIFont(name: "宋体",size: 12)
lable.font = font
//设置文字的阴影颜色
lable.shadowColor = UIColor.lightGrayColor()
//设置标签文字的阴影在横向和纵向的偏移距离
lable.shadowOffset = CGSizeMake(2,2)
//设置文字的对其的方式
lable.textAlignment = NSTextAlignment.Center
//设置标签文字的颜色
lable.textColor = UIColor.purpleColor()//紫色
//设置标签的背景颜色为黄色
lable.backgroundColor = UIColor.yellowColor()
今天就学习这两个控件。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |