简体   繁体   中英

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

  }

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)

You can try changing the button text by going in the FBLoginViews - How to customize FBLoginVIew?

However, it would be much easier if you just replace the button with your own custom button.

This link can help you better: https://developers.facebook.com/docs/facebook-login/ios#custom-login-button

I was able to edit more than just the title, and if i went into the FBSDK i could also change the back ground colors and a few other properties

this is the code i used for my button to make it rounded and just say FB then i used my size and brought the width down to make it nice and small

 private let facebookLoginButton: FBLoginButton = {
            let button = FBLoginButton()
            button.permissions = ["public_profile", "email"]
            let buttonText = NSAttributedString(string: "FB")
            button.setAttributedTitle(buttonText, for: .normal)
            button.layer.masksToBounds = true
            button.layer.cornerRadius = 12
            return button
        }()

Assuming that you are working in some VC and your loginButton property is FBSDKLoginButton

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self.loginButton setAttributedTitle:[[NSAttributedString alloc] initWithString:@"test"]
                                forState:UIControlStateNormal];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM