Ios -(CGRect)textRectForBounds:(CGRect)未调用的边界

Ios -(CGRect)textRectForBounds:(CGRect)未调用的边界,ios,iphone,objective-c,uitextfield,padding,Ios,Iphone,Objective C,Uitextfield,Padding,我正在尝试将文本移动到UITextField中,以使其垂直对齐。我在网上搜索过,所有内容都指向编辑这些方法: - (CGRect) textRectForBounds:(CGRect)bounds 及 虽然我对它们进行了如下编辑: - (CGRect) textRectForBounds:(CGRect)bounds { NSLog(@"hi"); return CGRectInset(bounds, 10, 10); } 虽然它没有到达nslog 我还尝试在textfiel

我正在尝试将文本移动到
UITextField
中,以使其垂直对齐。我在网上搜索过,所有内容都指向编辑这些方法:

- (CGRect) textRectForBounds:(CGRect)bounds

虽然我对它们进行了如下编辑:

- (CGRect) textRectForBounds:(CGRect)bounds {
    NSLog(@"hi");
    return CGRectInset(bounds, 10, 10);
}
虽然它没有到达nslog

我还尝试在textfield上调用该方法,但没有成功

这就是我初始化文本视图并设置它们的方式:

-(void) viewWillAppear:(BOOL)animated {
    textfield1 = [[UITextField alloc] init];
    textfield2 = [[UITextField alloc] init];
    textfield3 = [[UITextField alloc] init];
    textfield4 = [[UITextField alloc] init];
    for (UITextField *text in [NSArray arrayWithObjects:textfield1, textfield2, textfield3, textfield4, nil]) {
        text.delegate = self;
        text.placeholder = [placeholders objectAtIndex:i];
        text.frame = CGRectMake(10, offsetTop, container.frame.size.width - 20, 40);
        [text textRectForBounds:text.bounds];
        [text editingRectForBounds:text.bounds];
        text.borderStyle = UITextBorderStyleRoundedRect;
        offsetTop += 50;
        i++;
        [container addSubview:text];
    }
}

步骤1-首先创建自定义文本字段MyTextField

@interface MyTextField : UITextField
步骤2-根据需要重写MyTextField方法

///place holder position

- (CGRect)placeholderRectForBounds:(CGRect)bounds
 {
   return CGRectInset(bounds, 8, 8);
 }
 // text position
 - (CGRect)textRectForBounds:(CGRect)bounds {

  return CGRectInset(bounds, 8,4);
 }

 // text position while editing
 - (CGRect)editingRectForBounds:(CGRect)bounds {

     return CGRectInset(bounds, 8, 4);
 }
步骤3-在控制器类中导入MyTextField并将对象设置为MyTextField

#import "MyTextField.h"

-(void) viewWillAppear:(BOOL)animated {

    MyTextField* textfield1 = [[MyTextField alloc] init];
    // and so on

}

现在,每个文本字段都将按预期工作。

步骤1-首先创建一个自定义文本字段MyTextField

@interface MyTextField : UITextField
步骤2-根据需要重写MyTextField方法

///place holder position

- (CGRect)placeholderRectForBounds:(CGRect)bounds
 {
   return CGRectInset(bounds, 8, 8);
 }
 // text position
 - (CGRect)textRectForBounds:(CGRect)bounds {

  return CGRectInset(bounds, 8,4);
 }

 // text position while editing
 - (CGRect)editingRectForBounds:(CGRect)bounds {

     return CGRectInset(bounds, 8, 4);
 }
步骤3-在控制器类中导入MyTextField并将对象设置为MyTextField

#import "MyTextField.h"

-(void) viewWillAppear:(BOOL)animated {

    MyTextField* textfield1 = [[MyTextField alloc] init];
    // and so on

}

现在,每个文本字段都将按预期工作。

您需要在中为UITextField和over-ride-(CGRect)textrctforbounds:(CGRect)bounds方法创建子类。如何在@pawanpost代码中实现这一点。您现在是如何做的?您需要在中为UITextField和over-ride-(CGRect)textRectForBounds:(CGRect)bounds方法创建子类。如何在@pawanpost代码中实现这一点。你现在是怎么做的?