您的位置:首页 > 移动开发 > Swift

Swift UIButton 和 UILabel 的详细解释和创建使用

2015-12-02 09:03 519 查看


/*

在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(), forState:
UIControlState.Normal)

//设置字体的大小
和名字

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,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.

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: