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

4

8

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

  }
Gullah answered 7/7, 2016 at 22:24 Comment(0)
M
16

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)
Microscopium answered 6/4, 2017 at 22:43 Comment(1)
I use Objective-C at the moment, but the concept is spot on. I have been looking at the source for FBSDKLoginButton and FBSDKButton. FBSDKLoginButton adds just a few properties, and extends FBSDKButton. FBSDKButton adds no new properties or functions and extends UIButton. So, all concepts of UIButton exist for FBSDKLoginButton. This should have been obvious to me, but sometimes the simplest concepts need to be shown to realize. thanks!Cracksman
A
1

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
        }()
Adman answered 21/8, 2021 at 3:29 Comment(0)
P
0

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

Philippopolis answered 7/7, 2016 at 22:32 Comment(0)
C
0

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];

}
Concourse answered 24/7, 2017 at 22:31 Comment(1)
How is this different from @Andreza Cristina da Silva's answer?Agrostology

© 2022 - 2024 — McMap. All rights reserved.