选中后如何获取文本按钮?

时间:2016-03-18 00:28:21

标签: ios swift uibutton

嗨我正在尝试选择按钮后的文本。 我这样做是为了使按钮的图像和文本居中。

我创建了一个帮助我的功能。我想在选择按钮时再次调用它来调用它,并再次显示当前文本按钮的相关性。但是在选择时我没有对文本进行任何更改。 有人知道我该怎么做?

override func viewDidLoad() {
    super.viewDidLoad()

    let button: UIButton = UIButton()

    button.frame = CGRectMake(100, 100, 150, 60)

    button.setImage(UIImage(named: "img"), forState: .Normal)
    button.setImage(UIImage(named: "imgSelected"), forState: .Selected)


    button.setTitle("Test", forState: .Normal)
    button.setTitle("Prueba", forState: .Selected)

    button.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
    button.setTitleColor(UIColor.lightGrayColor(), forState: .Selected)

    button.addTarget(self, action: #selector(HistoricalViewController.buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
    //Center the image with text
    setupButton(button)
    self.view.addSubview(button)
}

func buttonAction(sender:UIButton){
    let button = sender as UIButton
    if button.selected{
        button.selected = false
    }else{
        button.selected = true
    }
    print("Current title \(button.currentTitle!)")
    setupButton(button)
}

func setupButton(button: UIButton) {
    let spacing: CGFloat = 6.0
    let imageSize: CGSize = button.imageView!.image!.size
    button.titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, -(imageSize.height + spacing), 0.0);
    let labelString = NSString(string: button.titleLabel!.text!)
    print("button title \(button.titleLabel!.text!)")
    let titleSize = labelString.sizeWithAttributes([NSFontAttributeName: button.titleLabel!.font])
    button.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + spacing), 0.0, 0.0, -titleSize.width);
}

编辑:

我像这样更新了我的功能,以保持图像居中

func buttonAction(sender:UIButton){
    let button = sender as UIButton
    if button.selected{
        button.selected = false
        button.setTitle("Test", forState: UIControlState.Normal)
    }else{
        button.selected = true
        button.setTitle("Prueba", forState: UIControlState.Normal)
    }
    setupButton(button)
}

并且

func setupButton(button: UIButton) {
    let spacing: CGFloat = 6.0
    let imageSize: CGSize = button.imageView!.image!.size
    button.titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, -(imageSize.height + spacing), 0.0);
    let labelString = NSString(string: button.titleForState(button.selected ? .Selected : .Normal)!)
    let titleSize = labelString.sizeWithAttributes([NSFontAttributeName: button.titleLabel!.font])
    button.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + spacing), 0.0, 0.0, -titleSize.width);
}

3 个答案:

答案 0 :(得分:1)

在setupButton中,更改此内容:

let labelString = NSString(string: button.titleForState(button.selected ? .Selected : .Normal)!)

答案 1 :(得分:0)

将此行添加到buttonAction()

let currentTitle = button.titleForState(.Selected)

答案 2 :(得分:0)

unction buttonAction被称为? 我认为你应该改变

button.addTarget(self, action: #selector(HistoricalViewController.buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)

with:

button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

并有所改进:

func buttonAction(sender:UIButton){
        sender.selected = !sender.selected
        print("Current title \(sender.currentTitle!)")
        setupButton(sender)
}

如果你使用故事板,它会更容易