0
votes

I'm having a problem with the UITextField placeholder attributes. I defined the text attributes as a global variable, set the UITextField's defaultTextAttributes and attributedPlaceholder in the viewDidLoad method, and implemented the text field's delegate methods to show a default text if the text field is left empty after editing ends.

    // MARK: - Define global meme text attributes
// Set text attributes
let memeTextAttributes = [
    NSStrokeColorAttributeName: UIColor.blackColor(),
    NSForegroundColorAttributeName: UIColor.whiteColor(),
    NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
    NSStrokeWidthAttributeName: -3.0
]

    override func viewDidLoad() {
    super.viewDidLoad()

    // Format image to maintain aspect ratio
    imagePickerView.contentMode = UIViewContentMode.ScaleAspectFit

    // Set text attributes and alignment
    topTextField.defaultTextAttributes = memeTextAttributes
    bottomTextField.defaultTextAttributes = memeTextAttributes
    topTextField.textAlignment = NSTextAlignment.Center
    bottomTextField.textAlignment = NSTextAlignment.Center

    // Set placeholder text for text fields
    topTextField.attributedPlaceholder = NSAttributedString(string: "TOP", attributes: memeTextAttributes)
    bottomTextField.attributedPlaceholder = NSAttributedString(string: "BOTTOM", attributes: memeTextAttributes)

    // Set text field delegates
    topTextField.delegate = self
    bottomTextField.delegate = self

    // Add tap gestures to dismiss keyboard when user taps outside of text field
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    view.addGestureRecognizer(tap)
}

Text field delegate methods:

    // MARK: - Text field delegate methods
func textFieldDidBeginEditing(textField: UITextField) {
    textField.attributedPlaceholder = nil
}

func textFieldDidEndEditing(textField: UITextField) {
    // If textField is empty, show respective default placeholder text
    if textField.text == "" {
        if textField == topTextField {
            textField.attributedPlaceholder = NSAttributedString(string: "TOP", attributes: memeTextAttributes)
        }
        else {
            textField.attributedPlaceholder = NSAttributedString(string: "BOTTOM", attributes: memeTextAttributes)
        }
    }
}

Both the text and the placeholder should have the same text attributes, but the problem is if the user leaves the text field empty after editing it, the next time they go to edit it the text is fill-only and doesn't have the stroke that was defined in the text attributes. But if the user doesn't leave the text field empty then the text shows up the way it should.

If initial edit is empty, text has no stroke: http://i59.tinypic.com/200r1mp.png

If initial edit is not empty, text shows up fine: http://i57.tinypic.com/t8oqkm.png

Any idea why this happens? If the text/placeholder attributes have been set when the view loads, then it shouldn't change unless some method explicitly changes it right?

1
ever figured this out? I have the same issue.. - Timur Ridjanovic

1 Answers

0
votes

You can use textFieldShouldEndEditing: instead of textFieldDidEndEditing: to avoid this behavior. In your case it doesn't really matter.