Iphone 如何在UItextfield中设置右边距以及占位符文本?

Iphone 如何在UItextfield中设置右边距以及占位符文本?,iphone,ipad,uitextfield,Iphone,Ipad,Uitextfield,我试图在UItextfield中设置右侧边距,但没有成功 我尝试了以下代码: UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 1, 60, 25) ]; [lbl setText:@"Partenza:"]; [lbl setFont:[UIFont fontWithName:@"Verdana" size:12]]; [lbl setTextColor:[UIColor grayColor]]; [lbl setTextA

我试图在
UItextfield
中设置右侧边距,但没有成功

我尝试了以下代码:

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 1, 60, 25) ];
[lbl setText:@"Partenza:"];
[lbl setFont:[UIFont fontWithName:@"Verdana" size:12]];
[lbl setTextColor:[UIColor grayColor]];
[lbl setTextAlignment:UITextAlignmentLeft];
txtFCarat.rightView = lbl;
txtFCarat.rightViewMode = UITextFieldViewModeAlways;
txtFCarat.delegate = self;

如果有人能解决这个问题,请帮助我

在下面创建textfield..的地方添加一行,并根据需要设置值

    NSString *myString = @"Partenza:";
    UIFont *myFont = [UIFont fontWithName:@"Verdana" size:12];
    CGSize myStringSize = [myString sizeWithFont:myFont];

    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myStringSize.width + 10, 25) ];
    [lbl setText:myString];
    [lbl setFont:[UIFont fontWithName:@"Verdana" size:12]];
    [lbl setTextColor:[UIColor grayColor]];
    [lbl setTextAlignment:UITextAlignmentLeft];
    txtFCarat.rightView = lbl;
    txtFCarat.rightViewMode = UITextFieldViewModeAlways;
textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);
- (void)settingTextField {

[self setTextField];
[self setKeyboardAppearance:UIKeyboardAppearanceAlert];
}



- (void)setTextField {
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self setTextAlignment:NSTextAlignmentCenter];
[self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
[self setFont:[UIFont boldSystemFontOfSize:15.0f]];
}
另一种方法是重写
textRectForBounds

让我完整地描述一下如何做到这一点或实现这一点

  • 首先,创建一个类并命名为“textField”(或任何您想要的名称),并且必须确保它是UITextField的子类

    2.现在打开
    textfield.m
    类并使用此代码或将下面的代码粘贴到此类(textfield)中:

  • 如果您想自定义更多的文本字段,那么您可以仅在此处使用,也可以在调用类中使用,如

    使用以下代码:注意,它仅用于测试目的。这取决于您的要求

    textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);
    
    - (void)settingTextField {
    
    [self setTextField];
    [self setKeyboardAppearance:UIKeyboardAppearanceAlert];
    }
    
    
    
    - (void)setTextField {
    [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [self setTextAlignment:NSTextAlignmentCenter];
    [self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
    [self setFont:[UIFont boldSystemFontOfSize:15.0f]];
    }
    
    3.将textField.h类导入要创建textField的视图控制器中

    你这样称呼它

     #import "textField.h"
    
    //Viewcontroller Class
    - (void)viewDidLoad
      {
       [super viewDidLoad];
       textField *field1 = [[textField alloc] initWithFrame:CGRectMake(50, 50, 200, 20)];
      field1.backgroundColor=[UIColor greenColor];
      field1.placeholder=@"0";
      [self.view addSubview:field1];
    }
    

    根据您的要求尝试调整值
    textRectForBounds

    在下面创建textfield.的地方添加一行,并根据需要设置值

    textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);
    
    - (void)settingTextField {
    
    [self setTextField];
    [self setKeyboardAppearance:UIKeyboardAppearanceAlert];
    }
    
    
    
    - (void)setTextField {
    [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [self setTextAlignment:NSTextAlignmentCenter];
    [self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
    [self setFont:[UIFont boldSystemFontOfSize:15.0f]];
    }
    
    另一种方法是重写
    textRectForBounds

    让我完整地描述一下如何做到这一点或实现这一点

  • 首先,创建一个类并命名为“textField”(或任何您想要的名称),并且必须确保它是UITextField的子类

    2.现在打开
    textfield.m
    类并使用此代码或将下面的代码粘贴到此类(textfield)中:

  • 如果您想自定义更多的文本字段,那么您可以仅在此处使用,也可以在调用类中使用,如

    使用以下代码:注意,它仅用于测试目的。这取决于您的要求

    textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);
    
    - (void)settingTextField {
    
    [self setTextField];
    [self setKeyboardAppearance:UIKeyboardAppearanceAlert];
    }
    
    
    
    - (void)setTextField {
    [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [self setTextAlignment:NSTextAlignmentCenter];
    [self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
    [self setFont:[UIFont boldSystemFontOfSize:15.0f]];
    }
    
    3.将textField.h类导入要创建textField的视图控制器中

    你这样称呼它

     #import "textField.h"
    
    //Viewcontroller Class
    - (void)viewDidLoad
      {
       [super viewDidLoad];
       textField *field1 = [[textField alloc] initWithFrame:CGRectMake(50, 50, 200, 20)];
      field1.backgroundColor=[UIColor greenColor];
      field1.placeholder=@"0";
      [self.view addSubview:field1];
    }
    

    根据您的要求尝试调整值
    textRectForBounds

    在下面创建textfield.的地方添加一行,并根据需要设置值

    textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);
    
    - (void)settingTextField {
    
    [self setTextField];
    [self setKeyboardAppearance:UIKeyboardAppearanceAlert];
    }
    
    
    
    - (void)setTextField {
    [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [self setTextAlignment:NSTextAlignmentCenter];
    [self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
    [self setFont:[UIFont boldSystemFontOfSize:15.0f]];
    }
    
    另一种方法是重写
    textRectForBounds

    让我完整地描述一下如何做到这一点或实现这一点

  • 首先,创建一个类并命名为“textField”(或任何您想要的名称),并且必须确保它是UITextField的子类

    2.现在打开
    textfield.m
    类并使用此代码或将下面的代码粘贴到此类(textfield)中:

  • 如果您想自定义更多的文本字段,那么您可以仅在此处使用,也可以在调用类中使用,如

    使用以下代码:注意,它仅用于测试目的。这取决于您的要求

    textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);
    
    - (void)settingTextField {
    
    [self setTextField];
    [self setKeyboardAppearance:UIKeyboardAppearanceAlert];
    }
    
    
    
    - (void)setTextField {
    [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [self setTextAlignment:NSTextAlignmentCenter];
    [self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
    [self setFont:[UIFont boldSystemFontOfSize:15.0f]];
    }
    
    3.将textField.h类导入要创建textField的视图控制器中

    你这样称呼它

     #import "textField.h"
    
    //Viewcontroller Class
    - (void)viewDidLoad
      {
       [super viewDidLoad];
       textField *field1 = [[textField alloc] initWithFrame:CGRectMake(50, 50, 200, 20)];
      field1.backgroundColor=[UIColor greenColor];
      field1.placeholder=@"0";
      [self.view addSubview:field1];
    }
    

    根据您的要求尝试调整值
    textRectForBounds

    在下面创建textfield.的地方添加一行,并根据需要设置值

    textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);
    
    - (void)settingTextField {
    
    [self setTextField];
    [self setKeyboardAppearance:UIKeyboardAppearanceAlert];
    }
    
    
    
    - (void)setTextField {
    [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [self setTextAlignment:NSTextAlignmentCenter];
    [self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
    [self setFont:[UIFont boldSystemFontOfSize:15.0f]];
    }
    
    另一种方法是重写
    textRectForBounds

    让我完整地描述一下如何做到这一点或实现这一点

  • 首先,创建一个类并命名为“textField”(或任何您想要的名称),并且必须确保它是UITextField的子类

    2.现在打开
    textfield.m
    类并使用此代码或将下面的代码粘贴到此类(textfield)中:

  • 如果您想自定义更多的文本字段,那么您可以仅在此处使用,也可以在调用类中使用,如

    使用以下代码:注意,它仅用于测试目的。这取决于您的要求

    textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);
    
    - (void)settingTextField {
    
    [self setTextField];
    [self setKeyboardAppearance:UIKeyboardAppearanceAlert];
    }
    
    
    
    - (void)setTextField {
    [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [self setTextAlignment:NSTextAlignmentCenter];
    [self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
    [self setFont:[UIFont boldSystemFontOfSize:15.0f]];
    }
    
    3.将textField.h类导入要创建textField的视图控制器中

    你这样称呼它

     #import "textField.h"
    
    //Viewcontroller Class
    - (void)viewDidLoad
      {
       [super viewDidLoad];
       textField *field1 = [[textField alloc] initWithFrame:CGRectMake(50, 50, 200, 20)];
      field1.backgroundColor=[UIColor greenColor];
      field1.placeholder=@"0";
      [self.view addSubview:field1];
    }
    

    尝试根据您的要求调整值
    textRectForBounds

    您可以覆盖以下两种方法

    @implementation rightTextField  
    
           // placeholder position
    
        - (CGRect)textRectForBounds:(CGRect)bounds {
             return CGRectInset( self.bounds , margin position , margin position );
        }
    
        // text position
        - (CGRect)editingRectForBounds:(CGRect)bounds {
             return CGRectInset( self.bounds , margin position ,margin position );
        }
    
    编辑:用于绑定代码,如

    textField.bounds = [self editingRectForBounds:textField.bounds];
    textField.bounds = [self textRectForBounds:textField.bounds];
    
    您期望的答案是:

     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
        textfield.rightView = imageView;
        textfield.rightViewMode = UITextFieldViewModeAlways;
    

    您可以在以下两种方法中覆盖此选项

    @implementation rightTextField  
    
           // placeholder position
    
        - (CGRect)textRectForBounds:(CGRect)bounds {
             return CGRectInset( self.bounds , margin position , margin position );
        }
    
        // text position
        - (CGRect)editingRectForBounds:(CGRect)bounds {
             return CGRectInset( self.bounds , margin position ,margin position );
        }
    
    编辑:用于绑定代码,如

    textField.bounds = [self editingRectForBounds:textField.bounds];
    textField.bounds = [self textRectForBounds:textField.bounds];
    
    您期望的答案是:

     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
        textfield.rightView = imageView;
        textfield.rightViewMode = UITextFieldViewModeAlways;
    

    您可以在以下两种方法中覆盖此选项

    @implementation rightTextField  
    
           // placeholder position
    
        - (CGRect)textRectForBounds:(CGRect)bounds {
             return CGRectInset( self.bounds , margin position , margin position );
        }
    
        // text position
        - (CGRect)editingRectForBounds:(CGRect)bounds {
             return CGRectInset( self.bounds , margin position ,margin position );
        }
    
    编辑:用于绑定代码,如

    textField.bounds = [self editingRectForBounds:textField.bounds];
    textField.bounds = [self textRectForBounds:textField.bounds];
    
    您期望的答案是:

     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
        textfield.rightView = imageView;
        textfield.rightViewMode = UITextFieldViewModeAlways;
    

    您可以在以下两种方法中覆盖此选项

    @implementation rightTextField  
    
           // placeholder position
    
        - (CGRect)textRectForBounds:(CGRect)bounds {
             return CGRectInset( self.bounds , margin position , margin position );
        }
    
        // text position
        - (CGRect)editingRectForBounds:(CGRect)bounds {
             return CGRectInset( self.bounds , margin position ,margin position );
        }
    
    编辑:用于绑定代码,如

    textField.bounds = [self editingRectForBounds:textField.bounds];
    textField.bounds = [self textRectForBounds:textField.bounds];
    
    您期望的答案是:

     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
        textfield.rightView = imageView;
        textfield.rightViewMode = UITextFieldViewModeAlways;
    


    devoloper:您好,上面的代码您提到过,我正在尝试,但无法在我的应用程序中工作。怎么可以?请帮助我@UndercoverDeveloper@UndercoverDeveloper不要从原作者那里复制代码。只是建议他一个答案,而不是复制。参考原作者的链接,给原文章一个适当的声誉。按作者姓名或linkdevoloper提及源代码:您好,上面的代码您提到我正在尝试,但无法在我的应用程序中工作。如何才能?请帮助我@UndercoverDeveloper@UndercoverDeveloper不要从原作者那里复制代码。只是建议他一个答案,而不是复制。参考原作者的链接,给原文章一个适当的声誉。按作者姓名或linkdevoloper提及源代码:您好,上面的代码您提到我正在尝试,但无法在我的应用程序中工作。如何才能?请帮助我@UndercoverDeveloper@UndercoverDeveloper不要从原作者那里复制代码。只是建议他一个答案,而不是复制。参考原作者的链接,给原文章一个适当的声誉。按作者姓名或linkdevoloper提及源代码:您好,上面的代码您提到我正在尝试,但无法在我的应用程序中工作。如何才能?请帮助我@UndercoverDeveloper@UndercoverDeveloper不要从原作者那里复制代码。只是建议他一个答案,而不是复制。参考原作者的链接,给原文章一个适当的声誉。按作者姓名或链接提及来源您想在右侧设置多少边距?我想在uitextfield中设置10dp边距@检查ans