UITextField attributedPlaceholder has no effect

柔情痞子 提交于 2019-12-17 19:38:09

问题


I'm trying to make the placeholders in my textfields italic, and since my app is targeting iOS 6.0 or newer, decided to use attributedPlaceholder property instead of rolling something more custom. The code goes as follows:

NSString *plString = @"optional";
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:plString
        attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-LightItalic" size:15]}];
for (UITextField *t in myTextfields){
    t.placeholder = plString;
    t.attributedPlaceholder = placeholder;
}

Yet the styling of the placeholder still is not italic, but the same as regular text, just dimmer. What am I missing to make the NSAttributedString work?


回答1:


As noted by warren, the styling currently can't be accomplished the way you're trying. A good workaround would be to set up your textfield's font attributes the way you would like your placeholder to look and then change the font of the textfield whenever the user begins typing. It will look like the placeholder and text are different fonts.

You can do this by creating a delegate of the textfield and utilizing shouldChangeCharactersinRange like this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{    
    // If there is text in the text field
    if (textField.text.length + (string.length - range.length) > 0) {
        // Set textfield font
        textField.font = [UIFont fontWithName:@"Font" size:14];
    } else {
        // Set textfield placeholder font (or so it appears)
        textField.font = [UIFont fontWithName:@"PlaceholderFont" size:14];
    }

    return YES;
}



回答2:


This is almost certainly a bug. The documentation for the attributedPlaceholder property claims that the string will be drawn using a gray color regardless of the foreground color attribute, but this is not the case: you can set both the foreground and background colors. Unfortunately, the font attribute appears to get stripped out and reverted to the system font.

As a workaround, I recommend overriding drawPlaceholderInRect: and drawing the placeholder yourself. Additionally, you should file a Radar on this and include a minimal sample project that demonstrates the bug.




回答3:


I just stumbled upon this issue myself. Apparently, the placeholder will take whatever font the textfield is being assigned with. Just set the textfield's font and you are good.

For everything else, like the colour of the placeholder, I'd still go back to attributedPlaceholder




回答4:


iOS8/9/Swift 2.0 - working example

func colorPlaceholderText(){
    var multipleAttributes = [String : NSObject]()
    multipleAttributes[NSForegroundColorAttributeName] = UIColor.appColorCYAN()
    //OK - comment in if you want background color
    //multipleAttributes[NSBackgroundColorAttributeName] = UIColor.yellowColor()
    //OK - Adds underline
    //multipleAttributes[NSUnderlineStyleAttributeName] = NSUnderlineStyle.StyleDouble.rawValue


    let titleString = "Search port/country/vessel..."
    let titleAttributedString = NSAttributedString(string: titleString,
        attributes: multipleAttributes)


    self.textFieldAddSearch.attributedPlaceholder = titleAttributedString
}


来源:https://stackoverflow.com/questions/15031678/uitextfield-attributedplaceholder-has-no-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!