微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift的UILabel的简单使用总结

swift中的UILabel 与oc中的基本大同小异,但还是有些许的不同,下面简单的介绍UILabel在swift中的使用:

1.创建UILabel

var label: UIlabel = UIlabel()

label.frame = CGRectMake( x,y,width. height)

self.view.addSubview(label)

2.设置UILabel的背景颜色和文字颜色

//设置文字对齐方式

label.textAlignment = NSTextAlignment.Center

//字体颜色

label.textColor = UIColor.blackColor()

//设置字体样式:粗体、大小等

label.font = UIFont.boldSystemFontOfSize(18)

//倾斜度

label.transform = CGAffineTransformMakeRotation(0.2)

//根据label的宽度,改变字体的大小

label.adjustsFontSizetoFitWidth = true

3.设置UILabel的边框,加圆角,只针对边框,不针对背景

//边框设置圆角

label.layer.cornerRadius = 10

label.layer.borderWidth = 2

label.layer.borderColor = UIColor.redColor().CGColor

4.设置UILabel的文字阴影

label.shadowColor = UIColor.purpleColor()

label.shadowOffset = CGSize(width: 2,height: 2)

5.使UILabel可以多行显示

label.numberOfLines = 2

6.设置UILabel是否能与用户交互

label.userInteractionEnabled = true,认是NO不能交互

下面是敲的:

 
        label = UILabel()
        label.text = "普通Label"
        //坐标设置
        label.frame = CGRectMake(50,50,100,35)
        //设置文字对齐方式
        label.textAlignment = NSTextAlignment.Center
        //字体颜色
        label.textColor = UIColor.blackColor()
        //设置字体样式:粗体、大小等
        label.font = UIFont.boldSystemFontOfSize(18)
        //倾斜度
        label.transform = CGAffineTransformMakeRotation(0.2)
        //添加到view上
        self.view.addSubview(label)
        
        //带背景和边框
        label2 = UILabel()
        label2.frame = CGRectMake(50,90,35)
        label2.text = "圆角Label"
        label2.textColor = UIColor.redColor()
        label2.backgroundColor = UIColor.blackColor()
        label2.textAlignment = NSTextAlignment.Center
        //边框设置圆角
        label2.layer.cornerRadius = 10
        label2.layer.borderWidth = 2
        label2.layer.borderColor = UIColor.redColor().CGColor
        self.view.addSubview(label2)
        
        label3 = UILabel()
        label3.text = "拥有点击事件"
        label3.frame = CGRectMake(50,130,35)
        //根据label的宽度,改变字体的大小
        label3.adjustsFontSizetoFitWidth = true
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self,action: "click:")
        label3.addGestureRecognizer(tap)
        //设置能否与用户进行交互 认是NO
        label3.userInteractionEnabled = true
        //阴影部分和阴影颜色
        label3.shadowColor = UIColor.purpleColor()
        label3.shadowOffset = CGSize(width: 2,height: 2)
        self.view.addSubview(label3)
        
        //设置多文本行
        label4 = UILabel()
        label4.frame = CGRectMake(50,210,200,80)
        label4.backgroundColor = UIColor.purpleColor()
        label4.textColor = UIColor.redColor()
        label4.text = "测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试"
        label4.lineBreakMode = NSLineBreakMode.ByTruncatingTail
        //设置多行
        label4.numberOfLines =  0
        label4.adjustsFontSizetoFitWidth = true
        self.view.addSubview(label4)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐