2016-07-07 14 views
2

私は自分のSDKからFacebookのログインボタンを持っていて、テキストを変更したいと思います。私はこのコードを試しました:SwiftでFacebookボタンのテキストをプログラムで変更するにはどうすればいいですか?

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

しかし、動作しません。それを行う方法はありますか?

これは、Facebookのログインボタンのコードは次のようになります。

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

    } 

答えて

4

私は間違った答えの多くを訪問した後、判明、それがあること:

let buttonText = NSAttributedString(string: "your text here") 
facebookButton.setAttributedTitle(buttonText, for: .normal) 
+0

私は現時点でObjective-Cを使用していますが、そのコンセプトにはスポットがあります。私はFBSDKLoginButtonとFBSDKButtonのソースを見てきました。 FBSDKLoginButtonはわずかなプロパティを追加し、FBSDKButtonを拡張します。 FBSDKButtonは新しいプロパティや関数を追加せず、UIButtonを拡張します。したがって、FIBDKLoginButtonにはUIButtonのすべての概念が存在します。これは私には明らかだったはずですが、時には最も単純な概念を実現するために示す必要があります。ありがとう! – Armand

+0

はtvosで動作しません – reggie

-1

をあなたには、いくつかのVCとで作業していると仮定すると、あなたのloginButtonプロパティはFBSDKLoginButtonです

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

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

} 
+0

これは@Andreza Cristina da Silvaの答えとどのように違いますか? –

関連する問題