0

Scenario: I'm setting the text font and size for my UITextField. They also have placeholders. The code bellow is in my UIViewController:

        let font = UIFont(name: "FlamaSemicondensed-Book", size: 18)

        let attributesDictionary = [NSForegroundColorAttributeName: DefaultSystemColor.DarkRed]

        // Textfileds without borders (and placeholders)
        UsernameTextField.borderStyle = UITextBorderStyle.None
        UsernameTextField.font = font
        UsernameTextField.textColor = DefaultSystemColor.DarkRed
        UsernameTextField.attributedPlaceholder = NSAttributedString(string: "Email", attributes: attributesDictionary)

I'm configuring (in AppDelegate) a global UI setting, that formats all of my UILabels for a certain font size. The code bellow is in my GlobalUISettings class:

    let font = UIFont(name: "FlamaSemicondensed-Book", size: 13)!
    var labelAppearace = UILabel.appearance()
    labelAppearace.font = font

What's weird in here:

When this UITextField is selected and I'm typing the format is the one I set for the text field (the placeholder are OK too).

But when I leave the field it assumes the behaviour of the UILabel.

I know that because if I comment the UILabel format the textField works properly.

Does anybody have any idea why does it happens?

brduca
  • 3,573
  • 2
  • 22
  • 30
  • 1
    For some strange reason, none of UILabel's properties are tagged as `UIAppearance_Selector`, which means you can get unexpected behaviour when using them. Just a heads up. – EmilioPelaez May 08 '15 at 14:02
  • So it's probably a bug on UIKit? I'm quite new to swift. I was just trying to figure out what I was doing wrong :-) – brduca May 08 '15 at 14:07
  • No, it's not a bug. `UITextfield` is basically a `UILabel` while not editing, and it follows the appearance traits of it, if you are using an appearance selector, narrow it down to your requirements using `[UILabel appearanceWhenContainedIn:(__unsafe_unretained Class)>, nil];` instead of the more generic one. – M. Porooshani May 24 '15 at 04:41

2 Answers2

0

Try changing the let font to let labelFont because 'font' is global. It might be affecting.

Omer Waqas Khan
  • 2,423
  • 4
  • 33
  • 62
0

This is correct behavior.

To change the font of the placeholder you have to add the NSFontAttributeName attribute with the desired font as value to the NSAttributedString you assign to attributedPlaceholder.

Example:

let attributesDictionary = [
    NSFontAttributeName: font,
    NSForegroundColorAttributeName: DefaultSystemColor.DarkRed
]
fluidsonic
  • 4,655
  • 2
  • 24
  • 34
  • I have no problems setting the font. My problem is when I leave the TextField it assumes the behaviour of the UILabel. – brduca May 08 '15 at 13:45
  • That's because changing the appearance of ALL UILabels is not a good idea in any case. This also affects system UI in the app and internal views of other controls like UITextField and UIButton. – fluidsonic May 08 '15 at 20:31
  • It kind of makes sense. I would expect a different behaviour when I set the appearence of a control, but thanks anyway – brduca May 08 '15 at 21:16