swift基础学习UI(01)[UIView、UILabel、UIButton]

前端之家收集整理的这篇文章主要介绍了swift基础学习UI(01)[UIView、UILabel、UIButton]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//控件的部分使用,以此类推其他属性

//1.UIView

let firstView = UIView()

firstView.isHidden = true

//背景

firstView.backgroundColor = UIColor.red;

//frame大小xy坐标 width height 宽高只要是继承UIViewframe都可以这样设置

firstView.frame = CGRect(x:10,y:100,width:300,height:45)

//能否响应点击事件

firstView.isUserInteractionEnabled = true

//tag

firstView.tag = 101

//边框的颜色

firstView.layer.borderColor = UIColor.black.cgColor

//边框的宽度

firstView.layer.borderWidth = 2

//超过范围截取

firstView.layer.masksToBounds = true

//边框的弧度

firstView.layer.cornerRadius = 5

//

self.view.addSubview(firstView)

//2.UILabel

//定义

let label = UILabel()

label.isHidden = true

//大小xy坐标 width height 宽高

label.frame = CGRect(x:50,y:60,width:200,height:50)

//背景颜色

label.backgroundColor = UIColor.red

//字体颜色

label.textColor = UIColor.white

//文字过长省略方式

label.lineBreakMode = NSLineBreakMode.byClipping

//显示几行

label.numberOfLines = 1

//阴影

label.shadowColor = UIColor.gray

//居中、居左

label.textAlignment = NSTextAlignment.center

//透明度

label.alpha = 0.5

//字体粗心大小

label.font = UIFont.boldSystemFont(ofSize: 20)

//高亮

label.isHighlighted = true

label.highlightedTextColor = UIColor.blue

//自适应

label.adjustsFontSizeToFitWidth = true

self.view .addSubview(label)

//富文本:

let attribute = NSMutableAttributedString(string:"李欢")

attribute.addAttribute(NSForegroundColorAttributeName,value: UIColor.yellow,range: NSMakeRange(0,1))

label.attributedText = attribute

//UILabel本身也是继承与UIView、因此有touch

label.isUserInteractionEnabled = true

//3.UIButton

let btn = UIButton()

btn.isHidden = false

btn.frame = CGRect(x:10,y:200,height:44)

btn.backgroundColor = UIColor.red

//btntitle highlightednormal、等

btn.setTitle("按钮",for: UIControlState.normal)

//颜色

btn.setTitleColor(UIColor.gray,for: UIControlState.normal)

//图片

btn.setImage(UIImage.init(named:""),0)">点击方法

btn.addTarget(self,action:#selector(click(_:)),for:.touchUpInside)

btn.tag = 202

//以及继承UIView所具有的属性

self.view.addSubview(btw)

//点击方法

func click(_ btn:UIButton){

print(btn.tag)

}

猜你在找的Swift相关文章