– (CGRect)textRectForBounds:(CGRect)边界没有被调用
我正在尝试移动UITextField
文本,以便它正确地垂直alignment。 我在网上search,一切都指向编辑这些方法:
- (CGRect) textRectForBounds:(CGRect)bounds
和
- (CGRect) editingRectForBounds:(CGRect)bounds
虽然我已经这样编辑过了:
- (CGRect) textRectForBounds:(CGRect)bounds { NSLog(@"hi"); return CGRectInset(bounds, 10, 10); }
虽然它没有得到nslog。
我也尝试在文本框中调用方法,但没有成功
这是我如何初始化文本视图并设置它们:
-(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,并创buildMyTextField的对象
#import "MyTextField.h" -(void) viewWillAppear:(BOOL)animated { MyTextField* textfield1 = [[MyTextField alloc] init]; // and so on }
现在每个文本字段将按预期工作。