Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift UIButton 和 UILabel 的详细解释和创建使用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

/*     在App 的开发过程中,我们都不会错过的两个类  UIButton  和  UILabel     其中  UIButton 是用户和客户端的交互连接员 ,处于重要的地位      然而,交互总有效果吧,那效果怎么显示呢,那就的UILabel 上场了     本文重点:      主要介绍  UIbutton  和  UILabel  的创建和类型(Btn);以及它们的      常

/*

App 的开发过程中,我们都不会错过的两个类 UIButton UILabel

其中 UIButton 用户和客户端的交互连接员 ,处于重要的地位

然而,交互总有效果吧,那效果怎么显示呢,那就的UILabel 上场了

本文重点:

主要介绍 UIbutton UILabel 的创建和类型(Btn);以及它们的

常用属性

*/

// Created by 周双建 on 15/12/1.

// Copyright © 2015 周双建. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


override func viewDidLoad() {

super.viewDidLoad()

/***********************************************************/


//创建一个Button

let ZSJBtn = UIButton(type: UIButtonType.Custom) as UIButton

/*

let ZSJBtnOne = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

这种方法已经废弃,创建 Btn 使用上面的方法

*/

//给按钮设置尺寸

ZSJBtn.frame = CGRectMake(50, 20,self.view.frame.size.width-100,100)

//给按钮一个背景

//1、给定色彩

ZSJBtn.backgroundColor = UIColor.redColor()

//2、用图片填色

ZSJBtn.backgroundColor = UIColor(patternImage: UIImage(named: "fadcf1d10901b800bd13b745d48e5755.jpg")!)

//3、对Btn 进行裁角 true 是允许裁剪 false 不允许裁剪

ZSJBtn.layer.masksToBounds = true

// 设置裁剪的半径

ZSJBtn.layer.cornerRadius = 10

/***********************************************************/

/*

contentsScale

contentsScale属性定义了寄宿图的像素尺寸和视图大小的比例,认情况下它是一个值为1.0的浮点数。

contentsScale的目的并不是那么明显。它并不是总会对屏幕上的寄宿图有影响。如果你尝试对我们的例子设置不同的值,你就会发现根本没任何影响。因为contents由于设置了contentsGravity属性,所以它已经被拉伸以适应图层的边界。

*/

ZSJBtn.layer.contentsScale = 20

/***********************************************************/

//给按钮设置图片 背景图片

//1、首先我们要清楚上面的 背景颜色

ZSJBtn.backgroundColor = UIColor.clearColor()

//2图片

ZSJBtn.setImage(UIImage(named: "fadcf1d10901b800bd13b745d48e5755.jpg"),forState:UIControlState.Disabled)

//3、设置背景图片

ZSJBtn.setBackgroundImage(UIImage(named: "fadcf1d10901b800bd13b745d48e5755.jpg"),forState: UIControlState.Normal)

/*

这两种设置Btn 的背景图片图片,的结果是一样的。都能充满Btn 的尺寸

*/

//设置Btn 的字体

//1认状态

ZSJBtn.setTitle("成功",forState: UIControlState.Normal)

//2、选中状态

ZSJBtn.setTitle("富有",forState: UIControlState.Selected)

//3、突出高亮的状态

ZSJBtn.setTitle("奋斗",forState: UIControlState.Highlighted)

//4、不可用的状态

ZSJBtn.setTitle("抱怨",forState: UIControlState.Disabled)

//5、设置字体的颜色 也四种状态

ZSJBtn.setTitleColor(UIColor.redColor(),0)"> //设置字体的大小 和名字

ZSJBtn.titleLabel!.font = UIFont(name: "Zapio",size: 20)

//给字体设置阴影 颜色

ZSJBtn.titleLabel!.shadowColor = UIColor.purpleColor()

//设置阴影字体的偏移量

ZSJBtn.titleLabel!.shadowOffset = CGSizeMake(10,0)

//设置多行字体 0 代表无限制

ZSJBtn.titleLabel!.numberOfLines = 0

/***********************************************************/

//设置标签

ZSJBtn.tag = 100

// 点击事件 不带参数

ZSJBtn.addTarget(self,action: "Click",forControlEvents: UIControlEvents.TouchUpInside)

//带参数的 点击事件

ZSJBtn.addTarget(self,action: "SEL:",forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(ZSJBtn)

// Do any additional setup after loading the view,typically from a nib.

}

func SEL(btn:UIButton){

self.Click()

print("jk",(btn.tag))

}

func Click(){

//创建 UILabel

let label = UILabel(frame: CGRectMake(50,200,50))

// Label 的赋值

label.text = "思念是种痛"

//Label 颜色

label.textColor = UIColor.magentaColor()

//设置字体加粗

label.font = UIFont.boldSystemFontOfSize(20)

// 设置Label的行数

label.numberOfLines = 0

//设置阴影

label.shadowOffset = CGSizeMake(15, 2)

label.shadowColor = UIColor.blackColor()

self.view.addSubview(label)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}



}

大佬总结

以上是大佬教程为你收集整理的Swift UIButton 和 UILabel 的详细解释和创建使用全部内容,希望文章能够帮你解决Swift UIButton 和 UILabel 的详细解释和创建使用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: