titleEdgeInsets應該是最好的方法例驹,如果只是簡單的左對齊柑潦,也可以如下方法帕棉。
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
同樣夫凸,右對齊 居中都有。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//創(chuàng)建有狀態(tài)的按鈕
var btn:UIButton = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 25))
btn.setTitle("點擊按鈕", forState: UIControlState.Highlighted)
btn.setTitle("未點擊", forState: UIControlState.Normal)
btn.backgroundColor = UIColor.blackColor()
self.view.addSubview(btn)
//給按鈕添加點擊事件
btn.addTarget(self, action: "clickEvent:", forControlEvents: UIControlEvents.TouchUpInside)
//創(chuàng)建沒有狀態(tài)的按鈕
var btn2:UIButton = UIButton(frame: CGRect(x: 50, y: 85, width: 100, height: 35))
btn2.setTitle("Normal", forState: UIControlState.Normal)
btn2.backgroundColor = UIColor.blueColor()
self.view.addSubview(btn2)
//創(chuàng)建一個圖片加文字的按鈕
var btn3:UIButton = UIButton(frame: CGRect(x: 50, y: 130, width: 180, height: 35))
btn3.setImage(UIImage(named: "btn1"), forState: UIControlState.Normal)
btn3.titleLabel?.font = UIFont.boldSystemFontOfSize(30)
btn3.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
//btn3.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
btn3.setTitle("圖片按鈕", forState: UIControlState.Normal)
self.view.addSubview(btn3)
//從系統(tǒng)定義的按鈕類型創(chuàng)建Button
var btn4:UIButton = UIButton.buttonWithType(UIButtonType.ContactAdd) as! UIButton
//btn4.backgroundColor = UIColor.brownColor()
btn4.frame.offset(dx: 60, dy: 200)
self.view.addSubview(btn4)
//創(chuàng)建禁止按鈕
var btn5:UIButton = UIButton(frame: CGRect(x: 50, y: 240, width: 100, height: 35))
btn5.setTitle("點擊按鈕", forState: UIControlState.Highlighted)
btn5.setTitle("禁止按鈕", forState: UIControlState.Normal)
btn5.enabled = false //設置按鈕不能點擊
btn5.setTitleColor(UIColor.redColor(), forState: UIControlState.Disabled)//代表已經(jīng)禁止
btn5.backgroundColor = UIColor.purpleColor()
self.view.addSubview(btn5)
//創(chuàng)建一個圓角按鈕
var btn6:UIButton = UIButton(frame: CGRect(x: 50, y: 280, width: 100, height: 35))
btn6.backgroundColor = UIColor.whiteColor()
btn6.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
btn6.setTitle("圓角按鈕", forState: UIControlState.Normal)
btn6.layer.cornerRadius = 5
self.view.addSubview(btn6)
//部分圓角按鈕,主要是利用layer的mask屬性,在通過CAShaperLayer和UIBezierPath來畫
var btn7:UIButton = UIButton(frame: CGRect(x: 50, y: 330, width: 100, height: 35))
btn7.backgroundColor = UIColor.whiteColor()
btn7.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
btn7.setTitle("部分圓角按鈕", forState: UIControlState.Normal)
let shape:CAShapeLayer = CAShapeLayer()
let bepath:UIBezierPath = UIBezierPath(roundedRect: btn6.bounds, byRoundingCorners: UIRectCorner.TopRight|UIRectCorner.TopLeft, cornerRadii: CGSize(width: 15, height: 15))
UIColor.blackColor().setStroke()
shape.path = bepath.CGPath
btn7.layer.mask = shape
self.view.addSubview(btn7)
//創(chuàng)建折角按鈕
var btn8:UIButton = UIButton(frame: CGRect(x: 50, y: 380, width: 100, height: 35))
btn8.backgroundColor = UIColor.whiteColor()
btn8.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
btn8.setTitle("折角按鈕", forState: UIControlState.Normal)
let shape8:CAShapeLayer = CAShapeLayer()
let bepath8:UIBezierPath = UIBezierPath()
bepath8.moveToPoint(CGPoint(x: 0,y: 0))
bepath8.addLineToPoint(CGPoint(x: 80,y: 0))
bepath8.addLineToPoint(CGPoint(x: 100,y: 15))
bepath8.addLineToPoint(CGPoint(x: 100,y: 35))
bepath8.addLineToPoint(CGPoint(x: 0,y: 35))
bepath8.closePath()
shape8.path = bepath8.CGPath
btn8.layer.mask = shape8
self.view.addSubview(btn8)
//創(chuàng)建border按鈕
var btn9:UIButton = UIButton(frame: CGRect(x: 50, y: 420, width: 100, height: 35))
btn9.backgroundColor = UIColor.whiteColor()
btn9.setTitle("邊框按鈕", forState: UIControlState.Normal)
btn9.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
btn9.layer.borderColor = UIColor.blackColor().CGColor
btn9.layer.borderWidth = 1
btn9.layer.cornerRadius = 5
self.view.addSubview(btn9)
//
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func clickEvent(sender:AnyObject){
NSLog("按鈕點擊了事件")
}
}
Snip20160722_10.png