iOS UILabel UIButton UIImageView使用方法

三个控件:UILabel ,UIButton ,UIImageView

注意:有些方法或属性 非常简单,本文没有提到。另有些很少使用的方法 没写

import UIKit

class ViewController: UIViewController {

    overridefunc viewDidLoad() {

        super.viewDidLoad()

        let label = UILabel(frame: CGRect(x:20, y:50, width:100, height:40))

        label.text = "Test" 

 

        label.textAlignment = .center

 

        //offset不设置就重合

        label.shadowColor = UIColor(colorLiteralRed: Float(arc4random_uniform(100))/100, green:0.2, blue: 0.2, alpha:0.5)

//一:随机数

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

        label.numberOfLines = 2 // label的行数 0是无限行

        label.layer.cornerRadius = 12

        //这个cornerRadius如果看不见效果的话,加一句:

 

        label.clipsToBounds =true

//或者

        label.layer.masksToBounds =true

//这两句看不懂没事,记住。意思是裁剪

        

        let button = UIButton(frame: CGRect(x:50, y:200, width:50, height:50))       

        button.setBackgroundImage( UIImage(named: "3.png") , for: .normal) // 背景图片

 

        // button 的内容包括文字和图片两部分,各部分可以为空。

        button.setImage(UIImage(named:"3.png"), for: .normal) // 这个图片属于内容,区分背景图 

        // button 整体内容各方向偏移:

        button.contentEdgeInsets = UIEdgeInsets(top: 5, left:5, bottom:5, right:5)

        // button 也支持字或图各自的偏移量

        

//  ImageView:

        let imageView  = UIImageView(image: UIImage(named:"3.png"))

        imageView.frame = CGRect(x: 200, y:400, width:100, height:100)

        imageView.backgroundColor = UIColor.lightGray

        // 默认 frame 有多大,图就缩放到多大,可以通过调节 contentMode 调整:

contentMode 一共有13种填充模式,分别是:

UIViewContentModeScaleToFill:这是图片显示的默认模式。图片进行非等比例缩放,直到填铺满整个View区域。所以往往造成图片的变形。也就是图片的长度上缩放一定的比例填满显示区域,在宽度上缩放一定的比例填满显示区域。

UIViewContentModeScaleAspectFit:这是等比例缩放,所以使用这种缩放模式的图片永远不会变形。图片按一定比例缩放,直到在长度上或者宽度上达到View的边界就停止。没有填满区域就显示View的背景色。

UIViewContentModeScaleAspectFill:这也是等比例缩放,图片也不会变形。图片按一定比例缩放,直到铺满View,超出View的部分被截掉。

UIViewContentModeRedraw:重绘。该模式比较特别,它不是用来说明如何展示图片的,而是告诉视图在每次设置或者更改frame的时候自动调用 drawRect:方法

UIViewContentModeCenter:等比缩放,居中显示。

UIViewContentModeTop:等比缩放,顶部对齐显示。

UIViewContentModeBottom:等比缩放,底部对齐显示。

UIViewContentModeLeft:等比缩放,左侧对齐显示。

UIViewContentModeRight:等比缩放,右侧对齐显示。

UIViewContentModeTopLeft:等比缩放,左上角对齐显示。

UIViewContentModeTopRight:等比缩放,右上角对齐显示。

UIViewContentModeBottomLeft:等比缩放,左下角对齐显示。

UIViewContentModeBottomRight:等比缩放,右下角对齐显示。

 

        // 在创建UIView对象时,UIView内部会自动生成一个图层(即CALayer对象),通过UIView的layer属性可以访问这个对象         

        // 想让一个 view 圆角,设置:

        //imageView.layer.masksToBounds = true

        imageView.layer.cornerRadius = 50

        // layer.masksToBounds 为 true才能圆角        

 

        // 设置任何view的阴影:

        imageView.layer.shadowColor = UIColor.purple.cgColor        //颜色

        imageView.layer.shadowOffset = CGSize(width: 5, height:5)   // 阴影位置

        imageView.layer.shadowOpacity = 0.8// 阴影透明度

   

        //imageView.layer.borderColor = UIColor.red.cgColor // 边界颜色

        //imageView.layer.borderWidth = 3  // 边界宽度

        

/*   image动画数组:

         var arr = [UIImage]() 

        arr.append(UIImage(named: "11.png")!)

        arr.append(UIImage(named: "12.png")!) 

        imageView.animationImages = arr

        imageView.animationRepeatCount = 0 //0是无限次

        imageView.animationDuration = 1

        imageView.startAnimating()

  */ 

        view.addSubview(label)

        view.addSubview(imageView)

        view.addSubview(button)

    }

}

Note:有些很简单的方法 或 很少用的方法本文没有提到

下一篇:UISwitch,UISegmentedControl,UITextField
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值