簡體   English   中英

在UIAlertView中更改UITextField的字體大小

[英]Change the font size of a UITextField in a UIAlertView

有誰知道如何在UIAlertView中更改UITextField的字體大小? 以下是我的代碼......

- (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];
}

在iOS 7.x之后,您無法自定義警報視圖的外觀,

為什么?

因為它的視圖層次結構是私有的

UIAlertView類參考中清楚地提到:

UIAlertView類旨在按原樣使用,不支持子類化。 此類的視圖層次結構是私有的, 不得修改

所以不幸的是,不可能改變textField字體,按鈕文本顏色等。

唯一的解決方案是使用自定義UIAlertView之一。

您必須使用自定義alertview。 請查看以下鏈接。

DTAlertView

它具有良好的動畫效果,也可以添加文本字段。

一旦你使用它,你不必編寫這么大的代碼。

希望能幫助到你。

您可以使用以下方法之一創建自定義UIAlertView

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

並應用您的自定義textField

對於第二個選項,在CODialog.maddTextFieldWithPlaceholder函數)中,您可以更改字體大小,或修改kCODialogTextFieldHeight常量。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM