7

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

  }
SwiftyJD
  • 5,257
  • 7
  • 41
  • 92

4 Answers4

15

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)
  • 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! – Armand May 14 '17 at 22:05
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
        }()
flutterloop
  • 546
  • 3
  • 15
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

Community
  • 1
  • 1
Avinash12388
  • 1,092
  • 1
  • 9
  • 19
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];

}
O'Neil
  • 3,790
  • 4
  • 16
  • 30