Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}
like image 774
0x141E Avatar asked Mar 17 '14 07:03

0x141E


2 Answers

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.

like image 113
Tarek Hallak Avatar answered Oct 16 '22 12:10

Tarek Hallak


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.

like image 28
Fahim Parkar Avatar answered Oct 16 '22 10:10

Fahim Parkar