Swift - 自由调整图标按钮中的图标和文字位置(扩展UIButton)

前端之家收集整理的这篇文章主要介绍了Swift - 自由调整图标按钮中的图标和文字位置(扩展UIButton)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Swift - 自由调整图标按钮中的图标和文字位置(扩展UIButton)

1,Custom类型的UIButton
我们使用定制类型(Custom)的按钮就可以设置文字前面的图标。但是图片文字的相对位置是固定的(按钮在前,文字在后)。
(1)我们用下面的左图(64*64)制作一个带图标的按钮
1
2
3
4
5
6
7
//创建一个图片文字的按钮
let btn1: UIButton = (frame: CGRect (x: 50,y: 50,width: 180,height: 32))
btn1.setImage( UIImage (named: "alert" ),forState: UIControlState . Normal ) //按钮图标
btn1.titleLabel?.font = UIFont .boldSystemFontOfSize(28) //文字大小
btn1.setTitle( "带图标按钮" ,forState: UIControlState //按钮文字
btn1.setTitleColor( UIColor .orangeColor(),0)!important">//文字颜色
self .view.addSubview(btn1)

(2)图片文字间的间距方式1 - 设置图片偏移量(imageEdgeInsets)
1
btn1.imageEdgeInsets = UIEdgeInsets (top: 0,left: -20,bottom: 0,right: 0)

(3)图片文字间的间距方式2 - 设置文字偏移量(titleEdgeInsets)

1
btn1.titleEdgeInsets= UIEdgeInsets
2,扩展UIButton
如果我们想要把文字图片位置调换下(即文字在前、图片在后),或者文字图片改成上下排列,那么同样通过设置 titleEdgeInsetsimageEdgeInsets即可实现。
为方便快速的设置图片文字的相对位置,以及间距,这里对UIButton进行扩展。
(1)扩展代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import UIKit
extension UIButton {
@objc func set (image anImage: UIImage ?,title: String titlePosition: UIViewContentMode CGFloat UIControlState ){
self .imageView?.contentMode = . Center
.setImage(anImage,forState: state)
positionLabelRespectToImage(title,position: titlePosition,spacing: additionalSpacing)
.titleLabel?.contentMode = . Center
.setTitle(title,forState: state)
}
private func positionLabelRespectToImage(title: String UIViewContentMode spacing: CGFloat ) {
let imageSize = .imageRectForContentRect( .frame)
titleFont = .titleLabel?.font!
titleSize = title.sizeWithAttributes([ NSFontAttributeName : titleFont!])
var titleInsets: UIEdgeInsets
imageInsets: UIEdgeInsets
switch (position){
case . Top :
titleInsets = (top: -(imageSize.height + titleSize.height + spacing),
left: -(imageSize.width),right: 0)
imageInsets = UIEdgeInsets Bottom :
titleInsets = (top: (imageSize.height + titleSize.height + spacing),
imageInsets = Left :
right: -(titleSize.width * 2 + spacing))
Right :
default :
}
.titleEdgeInsets = titleInsets
.imageEdgeInsets = imageInsets
}
}

(2)使用样例
11
12
13
20
21
22
28
29
30
36
37
38
45
class ViewController : UIViewController override viewDidLoad() {
super .viewDidLoad()
btn1: = UIButton (frame: CGRect (x: 0,y: 0,width: 200,height: 32))
btn1.center = CGPointMake (view.frame.size.width/2,60)
btn1.titleLabel?.font = UIFont .boldSystemFontOfSize(28) //文字大小
btn1.setTitleColor( UIColor Normal ) //文字颜色
btn1. (image: (named: "alert" "文字在左侧" additionalSpacing: 10.0,state: . )
view.addSubview(btn1)
btn2: btn2.center = btn2.titleLabel?.font = //文字大小
btn2.setTitleColor( UIColor UIControlState //文字颜色
btn2. (image: UIImage "文字在右侧" )
view.addSubview(btn2)
btn3: = btn3.center = CGPointMake btn3.titleLabel?.font = UIFont //文字大小
btn3.setTitleColor( //文字颜色
btn3. "文字在上方" )
view.addSubview(btn3)
btn4: btn4.center = btn4.titleLabel?.font = //文字大小
btn4.setTitleColor( //文字颜色
btn4. "文字在下方" )
view.addSubview(btn4)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}

原文出自: www.hangge.com 转载请保留原文链接 http://www.hangge.com/blog/cache/detail_960.html 原文链接:https://www.f2er.com/swift/322143.html

猜你在找的Swift相关文章