【问题标题】:Resetting UITextField Left margin重置 UITextField 左边距
【发布时间】:2011-04-18 07:10:47
【问题描述】:

我想将 UITextField.text 的左边距对齐到 10Px。请建议我最好的方法??在 roundedRect TesxtField 中相同,其中文本从左侧开始 10 px

几乎已经通过覆盖达到了 - (CGRect)textRectForBounds:(CGRect)bounds. 现在的问题是当 TextField 进入编辑模式时,它们的左边距重置为零......

@implementation UITextField(UITextFieldCatagory)

- (CGRect)textRectForBounds:(CGRect)bounds {
    CGRect theRect=CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width-10, bounds.size.height);
    return theRect;
}

【问题讨论】:

    标签: iphone ipad


    【解决方案1】:

    可以试试UITextField的实例方法drawTextInRect:吗?

    我认为您可以为此使用 leftView 属性。

    您可以将 leftViewrightView 添加到 UITextfield。这些视图可用于显示图标,但如果它是一个空视图,它只会占用空间,这正是您想要的。

    CGFloat leftInset = 5.0f;
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, leftInset, self.bounds.size.height)];
    self.leftView = leftView;
    self.leftViewMode = UITextFieldViewModeAlways;
    [leftView release];
    

    参考 SO 问题UITextField custom background view and shifting text

    【讨论】:

    【解决方案2】:

    你试过了吗

    -(CGRect)editingRectForBounds(CGRect)bounds;
    

    【讨论】:

      【解决方案3】:

      覆盖 super 的实现,并调整结果。这样你就不需要用 leftView 和 rightView 计算(如果你使用它们的话)。

      - (CGRect)textRectForBounds:(CGRect)bounds {
          CGRect ret = [super textRectForBounds:bounds];
          ret.origin.x = ret.origin.x + 10;
          ret.size.width = ret.size.width - 20;
          return ret;
      }
      
      - (CGRect)editingRectForBounds:(CGRect)bounds {
          return [self textRectForBounds:bounds];
      }
      

      【讨论】:

        【解决方案4】:

        迅速:

            let leftView:UIView = UIView(frame: CGRectMake(0, 0, 30, 1))
            leftView.backgroundColor = UIColor.clearColor()
            my_text_field.leftView = leftView;
            my_text_field.leftViewMode = UITextFieldViewMode.Always;
        

        【讨论】:

          猜你喜欢
          • 2011-08-06
          • 2015-04-12
          • 2016-11-24
          • 2013-09-02
          • 2022-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多