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, 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值