加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:43:00 所属栏目:百科 来源:网络整理
导读:// 控件的部分使用 , 以此类推其他属性 //1.UIView let firstView = UIView () firstView. isHidden = true // 背景 firstView. backgroundColor = UIColor . red ; //frame 大小 x 、 y 坐标 width height 宽高只要是继承 UIView 的 frame 都可以这样设置 f

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

//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)

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读