Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the text of a Facebook button programmatically in Swift?

I have a facebook login button from their SDK and would like to change the text. I've tried this code:

facebookButton.setTitle("my text here", forState: .Normal)

but it doesn't work. Is there a way to do it?

This is what the facebook login button code looks like:

//MARK: Facebook Login

  func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
    if error == nil {

      facebookAPILogin(FBSDKAccessToken.currentAccessToken().tokenString, completionHandler: { error in

        if error == nil {

          self.performSegueWithIdentifier("loginToHomeSegue", sender: self)

        } else {



          self.showAlertWithTitle("Sorry".localized(), message: "There was a problem connecting with Facebook".localized() )

          print(error)

        }
        }
      )

    } else {

      showAlertWithTitle("Sorry".localized(), message: "There was a problem connecting with Facebook".localized() )

      print(error.localizedDescription)

    }
  }


  //Facebook Logout

  func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
    print("User logged out")

  }
like image 870
SwiftyJD Avatar asked Jul 07 '16 22:07

SwiftyJD


1 Answers

I just found out, after visiting lots of wrong answers, that it is:

let buttonText = NSAttributedString(string: "your text here")
facebookButton.setAttributedTitle(buttonText, for: .normal)
like image 53
Andreza Cristina da Silva Avatar answered Sep 28 '22 08:09

Andreza Cristina da Silva