2016-07-23 105 views
0

您好,我正在做Udacity nanodegree,我正在使用textFields,但我需要使用以UITextField作为参数的方法来减少代码。 我其实我不知道这个方法可能是什么,我也不知道如何减少代码。 我会让你的代码,并希望你能帮助我使用方法减少代码

let memeTextAttributes = [ 
    NSStrokeColorAttributeName : UIColor.blackColor(), NSForegroundColorAttributeName : UIColor.blueColor(), NSFontAttributeName : UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!, NSStrokeWidthAttributeName : -1.0] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    topText.defaultTextAttributes = memeTextAttributes 
    bottomText.defaultTextAttributes = memeTextAttributes 
    topText.textAlignment = NSTextAlignment.Center 
    bottomText.textAlignment = NSTextAlignment.Center 
    shareButton.enabled = false 
+0

你得到它的工作: 例如,文本框底部会在viewDidLoad的叫什么? – thegrandhi

回答

0

您可以使用这样的事情:

func configTextField(defaultText: String, textField: UITextField) 
{ 
    textField.text = defaultText 
    textField.defaultTextAttributes = memeTextAttributes 
    textField.autocapitalizationType = .AllCharacters 
    textField.textAlignment = .Center 
    textField.delegate = self 

} 

既然是梅梅,你可以自动大写整个文本。 然后在viewDidLoad中调用上述函数。

configTextField("BOTTOM", textField: bottomTextField)