Dasoga
Dasoga

Reputation: 5695

How can I get the text button after it selected?

Hi I'm trying to get the text of button after it selected. I'm doing this to center the image and text of button.

I created a function that is help me to to this. I want to call it when button is selected to render again the sizes and center again dependes of current text button. But I don't get any change in text when is selected. Somebody knows how can I do this?

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);
}

Edited:

I updated my function like this, to keep the image centered

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)
}

And

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);
}

Upvotes: 0

Views: 108

Answers (3)

Buu Bui
Buu Bui

Reputation: 154

In setupButton, change this:

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

Upvotes: 1

Nguyen Hoan
Nguyen Hoan

Reputation: 1693

unction buttonAction is called ? I thinks you should change

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

with :

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

and a improvement:

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

if you use storyboard, it 'll more easy

Upvotes: 0

Nim Thitipariwat
Nim Thitipariwat

Reputation: 788

Add this line to your buttonAction()

let currentTitle = button.titleForState(.Selected)

Upvotes: 0

Related Questions