setDefaultTextAttributes on UITextField does not work

uitextfield editing changed programmatically
uitextfield placeholder
uitextview
uitextfield keyboard
uitextfield appearance
uitextfield accessibility
uitextfield border style
swiftui textfield default text

I have a problem assigning text attributes (font,color, kerning) to an empty UITextField. Here is some sample code:

// testinput is an UITextField created in storyboard
//
 [testinput setDefaultTextAttributes: @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0], NSForegroundColorAttributeName: [UIColor redColor]}  ];

That should - in theory - change font & color, but nothing happens. I've also tried different ways of creating the dictionary, no change. Please do not advise to use an attributed string, the UITextField must be initially empty.

Any help would be appreciated.


Found a solution after reading Gregs comment. To set the initial attributes in an empty UITextField, use the

@property(nonatomic, copy) NSDictionary *typingAttributes

property. It must be set in one of the delegate methods of the UITextField:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
   NSDictionary* d = textField.typingAttributes;
   NSMutableDictionary* md = [NSMutableDictionary dictionaryWithDictionary:d];

   md[NSFontAttributeName] = [UIFont systemFontOfSize:30];
   md[NSKernAttributeName] = @(9.5f);

   textField.typingAttributes = md;
   return YES;
}

ios - setDefaultTextAttributes on UITextField does not work, Found a solution after reading Gregs comment. To set the initial attributes in an empty UITextField, use the @property(nonatomic, copy) NSDictionary *  The UITextField class does not provide built-in support for formatting its string using an Formatter object, but you can use the text field’s delegate to format the content yourself. To do so, use the text field’s delegate methods to validate text and to format it appropriately. For example, use the textField (_:shouldChangeCharactersIn


Swift 4 version

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let currentAttributes = textField.typingAttributes
    if var attributes = currentAttributes {
        attributes[NSAttributedStringKey.foregroundColor.rawValue] = UIColor.white
        attributes[NSAttributedStringKey.font.rawValue] = UIFont.systemFont(ofSize: 26)
        textField.typingAttributes = attributes
    }

    return true
}

defaultTextAttributes, Getting this property returns the previously set attributes, which may have been modified The string that is displayed when there is no other text in the text field. like we do internally, then your original code doesn't work again. It seems to be like what you are doing is kind of hack because you are changing responder and performing side effects while text field is asking "can I stop editing", but can't say I haven't done similar things in past.


If you use setDefaultTextAttributes the changes will apply to textAttribute not text property of UITextField. So if you want to use text property, and I assume you want you should use, try:

testinput.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0];
testinput.textColor = [UIColor redColor]

//Extended

I'm afraid you cannot use it in the way you want. This is from apple documentation:

By default, this property returns a dictionary of text attributes with default values.
Setting this property applies the specified attributes to the entire text of the text field. Unset attributes maintain their default values.

It looks like when you use defaultTextAttributes property you will get only a dictionary and f you want to set it you have to create attribute string with attributes(dictionaries):

UITextField, The UITextField class does not provide built-in support for formatting its string using an Formatter object, but you can use the text field's delegate to format the  If you use setDefaultTextAttributes the changes will apply to textAttribute not text property of UITextField. So if you want to use text property, and I assume you want you should use, try: testinput.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0]; testinput.textColor = [UIColor redColor]


Convert Obj-C to Swift - uitextfield - html, textColor = [UIColor blackColor]; break; } } } Note : This is Not Public API OR You can use class], nil] setDefaultTextAttributes:#{​NSForegroundColorAttributeName:[UIColor For XCode 6 (iOS8 SDK) the following DOESN'T work [[UITextField  The UISearchBar has a UITextField inside, but there’s no property to access it. So, there is no way for doing it in a standard way. But there is a non-stardard way to workaround it.


Place Autocomplete | Places SDK for iOS, You can add the autocomplete UI control to your app in the following ways: In cases where the navigation bar is visible and opaque, UISearchController does not set changes the text color of UITextField classes to green when they are setDefaultTextAttributes:@{NSForegroundColorAttributeName:[  The event "ValueChanged" is not fired on a UITextField when a charactor is typed (or at all). (the Changed event IS fired in a UITextView, so I assume it's a typo?) Steps to reproduce the problem: 1. Add a UITextField to a form. Wire it up 2. In MonoDevelop, attach to the field's ValueChanged event. 3.


Placeholder text not centered for UITextField created , I am creating a UITextField programmatically and placing it inside a UIView. The font size is set to 15.0f (system font). But the placeholder that appears is not centered in the text view. This does not work as expected on iOS7. class], nil] setDefaultTextAttributes: @{ NSForegroundColorAttributeName:[UIColor whiteColor],  The Process is simple, similar to UIKit, we need to listen to Keyboard Events and perform an action accordingly. UIRsponder allows us to listen to the following keyboard-related event: we can make