简体   繁体   中英

Change the font size of a UITextField in a UIAlertView

Does anyone know how to change the font size of a UITextField within a UIAlertView? The following is my code...

- (void) editTitle
{
    NSString *string = kLocalizedString(@"Edit Title");

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                message:string
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK", nil];

    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField *textField = [alert textFieldAtIndex:0];
    if (!self.title) {
        textField.text = nil;
    }
    else {
        textField.text = self.title;
    }

    textField.clearsOnBeginEditing = NO;
    textField.clearButtonMode = UITextFieldViewModeAlways;
    textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    textField.clearsContextBeforeDrawing = NO;

    // These statements have no effect on the size of the text field's font
    textField.font = [UIFont systemFontOfSize:16.0];
    NSDictionary *attributes = @{
                NSFontAttributeName: [UIFont systemFontOfSize:16.0]}; 
    textField.typingAttributes = attributes;

    [alert show];
}

After iOS 7.x you cannot customize the appearance of alert views,

Why?

Because its view hierarchy is private.

It is mentioned clearly in UIAlertView Class Reference :

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified .

So unfortunately it is impossible to change the textField font, buttons text color .. etc.

The only solution is using one of the custom UIAlertView's.

You have to use custom alertview. Just check below link.

DTAlertView

It has good animation and textfield can be added too.

Once you use this, you don't have to write such big code.

Hope it helps.

You can create a custom UIAlertView using one of this:

  1. http://code.tutsplus.com/tutorials/ios-sdk-uialertview-custom-graphics--mobile-8886
  2. https://github.com/eaigner/CODialog

and apply your custom textField .

For the second option, in CODialog.m ( addTextFieldWithPlaceholder function) you can change the font size, or modify kCODialogTextFieldHeight constant.

field.font = [UIFont systemFontOfSize:kCODialogTextFieldHeight - 8.0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM