Ios 填充占位符文本

Ios 填充占位符文本,ios,uitextfield,Ios,Uitextfield,我想让占位符文本显示在文本字段的中间(填充占位符文本)。占位符文本的大小也需要增加。我的代码如下,如何解决这个问题 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 220,250,55)]; textField.placeholder=@"iiiiiii"; UIView *padView = [[UIView alloc] initWithFrame:CGRectMake(10, 110,

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

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];
更新


我希望占位符文本的字体大小增加
您的姓名
,并且应该有一个左填充。

您可以对
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);
}
upd: 也设置


由于io6与ios 7垂直定位可能会出现一些问题,因此您可以将起始文本对齐(来自xib或通过代码的
文本字段的
)设置为中心对齐。
然后在
-textField shouldBeginediting
中,您可以将
文本字段设置为左对齐。
同样,在
-textfielddendediting
上,检查
textField
是否为空,然后将
textField
设置回中心对齐

基本上:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [textField setTextAlignment:NSTextAlignmentLeft];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if(textField.text.length == 0) {
        [textField setTextAlignment:NSTextAlignmentCenter];
    }
}

编辑:

ViewController
类的.h应该如下所示:

@interface ViewController : UIViewController <UITextFieldDelegate> 
{
    UITextField *myTextField;
}

同样的问题被提出并回答如下


虽然我没有具体测试过,但这应该可以:

self.YourTextField.attributedPlaceholder = NSAttributedString(string: "    YourText")

在字符串中添加空格(填充)

这是我在swift 2和Xcode 7上发现的完成此任务的最简单方法:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var emailTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        let paddingView = UIView(frame: CGRectMake(0, 0, 25, self.emailTextField.frame.height))
        emailTextField.leftView = paddingView
        emailTextField.leftViewMode = UITextFieldViewMode.Always
    }
}

你是在问如何让
占位符
文本集中在
UITextField
中,但一旦用户开始向其中添加
文本
,就会左对齐吗?我已经更新了我的帖子,我还添加了一个图像来描述我的问题。所以我需要这个
[textField setDelegate:self]
,当我添加这一行时,它在不兼容类型“id”的参数上写着“Sending”ViewController*const\uu strong`@sharonHwk:是的,需要委托,但是在
ViewController
的.h中,您需要声明您的类正在使用委托(只需通过
)。。。示例:
@interface ViewController:UIViewController
@sharonHwk:但是。。。但是它不完全是
[textField setDelegate:self]。。。在
-viewDidLoad
中或创建
UITextField
对象的任何位置,说出类似于
UITextField*txtFSomething=[UITextField alloc]init]然后通过
[txtFSomething setDelegate:self]设置委托或通过xib连接执行。我仍然收到相同的警告。@sharonHwk:where添加行
[textField setDelegate:self]?另外,将ou UITextField对象的名称从
textField
更改为类似
myTextField
的其他名称,然后执行
[myTextField setDelegate:self]你所说的问题是什么意思?ios6和ios7中有一个问题垂直对齐与同一代码不同,设置内容垂直对齐帮助。
  UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
  textField.leftView = paddingView;
  textField.leftViewMode = UITextFieldViewModeAlways;
self.YourTextField.attributedPlaceholder = NSAttributedString(string: "    YourText")
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var emailTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        let paddingView = UIView(frame: CGRectMake(0, 0, 25, self.emailTextField.frame.height))
        emailTextField.leftView = paddingView
        emailTextField.leftViewMode = UITextFieldViewMode.Always
    }
}
sampleTextfield.leftView = UIView(frame: CGRectMake(0, 0, 20, self.sampleTextfield.frame.height))
sampleTextfield.leftViewMode = UITextFieldViewMode.Always
func placeholderPadding(textField:UITextField, leftPadding:CGFloat) {
            textField.leftView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: leftPadding, height: textField.frame.height))
            textField.leftViewMode = UITextFieldViewMode.always
        }