swift的UILabel的简单使用总结

来源:互联网 发布:jquery.media.js ie 编辑:程序博客网 时间:2024/05/03 01:22

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, 100, 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, 100, 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)


0 0
原创粉丝点击