填充占位符文本ios

sha*_*Hwk 6 uitextfield ios

我想让占位符文本显示在文本字段的中间(填充占位符文本).占位符文本的大小也需要增加.我的代码如下,我该如何解决?

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 220,250,55)];

textField.placeholder=@"iiiiiii";

UIView *padView = [[UIView alloc] initWithFrame:CGRectMake(10, 110, 10, 0)];
textField.leftView = padView;
    textField.leftViewMode = UITextFieldViewModeAlways;


[self.view addSubview:textField];
Run Code Online (Sandbox Code Playgroud)

UPDATE

在此输入图像描述

我希望占位符文本的字体大小增加your name,它应该有一个左边填充.

Evg*_*y S 13

您可以子类化UITextFiled和覆盖方法:

MyTextField.m

- (CGRect)textRectForBounds:(CGRect)bounds {
    return [self rectForBounds:bounds];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self rectForBounds:bounds];
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    return [self rectForBounds:bounds];
}

//here 40 - is your x offset
- (CGRect)rectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 40, 3);
}
Run Code Online (Sandbox Code Playgroud)

upd: 也设定

textFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Run Code Online (Sandbox Code Playgroud)

因为io6与ios 7垂直定位可能存在一些问题


kam*_*egi 5

同样的问题被提出并回答如下:使用 UITextBorderStyleNone 设置 UITextField 的填充

  UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
  textField.leftView = paddingView;
  textField.leftViewMode = UITextFieldViewModeAlways;
Run Code Online (Sandbox Code Playgroud)