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

时间:2014-01-01 04:50:14

标签: 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 setTextAlignment:UITextAlignmentLeft];
txtFCarat.rightView = lbl;
txtFCarat.rightViewMode = UITextFieldViewModeAlways;
txtFCarat.delegate = self;

如果有任何机构有解决方案,请帮助我!

3 个答案:

答案 0 :(得分:3)

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

@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;

答案 1 :(得分:1)

在您创建textfield的位置添加以下行,并根据需要设置值

textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);

通过覆盖textRectForBounds

实现此目的的另一种方法

让我给出完整的描述如何做到这一点或实现它

  1. 首先创建一个类并命名为“textField”(或者你想要的),并且必须确保它是UITextField的子类。

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

     - (CGRect)textRectForBounds:(CGRect)bounds {
       return CGRectMake(bounds.origin.x + 50, bounds.origin.y,
                  bounds.size.width - 20, bounds.size.height);
     }
    
    -(CGRect)editingRectForBounds:(CGRect)bounds {
     return [self textRectForBounds:bounds];
    }
    
  2. 如果您想更多地自定义文本字段,则只能在此处使用或在调用类

    中使用

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

    - (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.improt textField.h类进入你想要创建文本字段的视图控制器

    你这样称呼它。

     #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

答案 2 :(得分:-1)

    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;