【问题标题】:Change the font size of a UITextField in a UIAlertView在 UIAlertView 中更改 UITextField 的字体大小
【发布时间】:2014-03-17 07:42:12
【问题描述】:

有谁知道如何在 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];
}

【问题讨论】:

  • 您好,我只是在这里说我对您的问题投了赞成票,因为用户在没有给出任何解释的情况下对它投了反对票。只有在有解释的情况下才允许投反对票。
  • 您使用的是默认 UIAlertView,而不是自定义。我认为你不能这样做。
  • 在ios7之前可以做,之后就不行了。但是,您可以创建自己的自定义视图,类似于 UIAlertView 并进行任何您需要的更改。

标签: ios iphone uitextfield uialertview


【解决方案1】:

在 iOS 7.x 之后,您无法自定义警报视图的外观,

为什么?

因为它的视图层次是私有的。

UIAlertView Class Reference中明确提到:

UIAlertView 类旨在按原样使用,而不是 支持子类化。此类的视图层次结构是私有的,并且 不得修改

所以很遗憾,无法更改 textField 字体、按钮文本颜色等。

唯一的解决方案是使用自定义 UIAlertView 之一。

【讨论】:

    【解决方案2】:

    您必须使用自定义警报视图。只需检查以下链接。

    DTAlertView

    它有很好的动画和文本框也可以添加。

    一旦你使用它,你就不用写这么大的代码了。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      您可以使用以下方法之一创建自定义 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];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        • 1970-01-01
        • 2014-03-17
        相关资源
        最近更新 更多