【问题标题】:UITextField text overlaps clear buttonUITextField 文本重叠清除按钮
【发布时间】:2011-12-02 05:47:46
【问题描述】:

我正在向我的视图添加一个文本字段,如下所示:

UITextField* tf_email = [[UITextField alloc] initWithFrame:CGRectMake((320-btnImage1.size.width)/2, 170, 175, 35)];
    [tf_email setBackgroundColor:[UIColor clearColor]];
    [tf_email setBorderStyle:UITextBorderStyleRoundedRect];
    [tf_email setClearButtonMode:UITextFieldViewModeWhileEditing];
    [tf_email setReturnKeyType:UIReturnKeyDone];
    [tf_email setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [tf_email setEnablesReturnKeyAutomatically:NO];
    [tf_email setDelegate:self];    
    [tf_email setOpaque:YES];
    tf_email.tag=1;
    tf_email.font = TTSTYLEVAR(font);
    tf_email.layer.cornerRadius = 10;
    tf_email.keyboardType = UIKeyboardTypeEmailAddress;
    [tf_email setAutocorrectionType:UITextAutocorrectionTypeNo];
    tf_email.placeholder = @"your@email.com";
    [self.view addSubview:tf_email];

当我在此字段中输入长文本时,文本和清除按钮重叠。有谁知道如何解决这个问题?

【问题讨论】:

  • 尝试使用标准字体看看效果。

标签: objective-c ios uitextfield


【解决方案1】:

创建UITextField 的子类并覆盖下面给出的方法,

/*< Place holder position >*/
- (CGRect)textRectForBounds:(CGRect)bounds {

    bounds.size.width = bounds.size.width - 20.0f;
    return bounds;
}

/*< Text Posiotion >*/
- (CGRect)editingRectForBounds:(CGRect)bounds {

    bounds.size.width = bounds.size.width - 20.0f;
    return bounds;
}

干杯!!!

【讨论】:

    【解决方案2】:

    我想出了问题所在。在另一个文件中,我为 UITextField 定义了一个类别。此类别,指定文本区域非常靠近左右边框。这导致了重叠。

    经验教训:在定义类别时,我们应该使用单独的文件,以便轻松检测到修改。

    【讨论】:

      【解决方案3】:

      @Augustine P A,我认为更好的方法是调用 super 并像这样修改结果:

      -(CGRect) textRectForBounds:(CGRect)bounds
      {
          CGRect rect = [super textRectForBounds:bounds];
          rect.size.width = rect.size.width - 20.0f;
          return rect;
      }
      
      -(CGRect) editingRectForBounds:(CGRect)bounds
      {
          CGRect rect = [super editingRectForBounds:bounds];
          rect.size.width = rect.size.width - 20.0f;
          return rect;
      }
      

      【讨论】:

        【解决方案4】:

        添加文本字段后,调用

        [button bringSubviewToFront:self.view];

        【讨论】:

        • 我需要把哪个视图放到前面?
        • 请在 iPad 上查看 App Store 应用程序。它有一个搜索按钮,其中文本和清除按钮不会发生冲突。在这种情况下,没有必要将一个视图放在另一个视图之上。
        猜你喜欢
        • 2020-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-27
        • 2010-09-24
        相关资源
        最近更新 更多