Ios 如何在故事板中使用自定义UITextField

Ios 如何在故事板中使用自定义UITextField,ios,objective-c,ios7,Ios,Objective C,Ios7,请告诉我如何在我的UIViewController中使用此自定义UITextField 我用的是故事板 #import <UIKit/UIKit.h> @interface MYTextField : UITextField @end @implementation MYTextField - (CGRect)textRectForBounds:(CGRect)bounds { int margin = 10; CGRect inset = CGRectMa

请告诉我如何在我的
UIViewController
中使用此自定义
UITextField

我用的是故事板

#import <UIKit/UIKit.h>

@interface MYTextField : UITextField

@end


@implementation MYTextField

- (CGRect)textRectForBounds:(CGRect)bounds {
    int margin = 10;
    CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
    return inset;
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    int margin = 10;
    CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
    return inset;
}
#导入
@接口MYTextField:UITextField
@结束
@实现MYTextField
-(CGRect)textRectForBounds:(CGRect)bounds{
整数裕度=10;
CGRect inset=CGRectMake(bounds.origin.x+边距,bounds.origin.y,bounds.size.width-边距,bounds.size.height);
返回插图;
}
-(CGRect)编辑rectforbounds:(CGRect)bounds{
整数裕度=10;
CGRect inset=CGRectMake(bounds.origin.x+边距,bounds.origin.y,bounds.size.width-边距,bounds.size.height);
返回插图;
}

您必须使用自定义的
MYTextField,而不是使用普通的
UITextField

代码:

MYTextField *myTextField = [[MYTextField alloc] init];
如果使用界面生成器:

在界面生成器文件中选择
UITextField
,转到
标识检查器
并设置正确的子类:

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;

您必须使用自定义的
MYTextField
,而不是使用普通的
UITextField

代码:

MYTextField *myTextField = [[MYTextField alloc] init];
如果使用界面生成器:

在界面生成器文件中选择
UITextField
,转到
标识检查器
并设置正确的子类:

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;

此代码在文本字段的左侧添加了一些边距。 试试这个对我有用

此代码在文本字段的左侧添加了一些边距。
试试这个对我有用。

这应该不难回答,但我不太明白你到底想做什么?你在用故事板吗?在发布代码之前,请编辑您的问题并留出4个空格,以使其更具可读性。是的,我正在使用故事板,我想在我的文本字段中应用左边距,我遵循此代码,但我无法实现此操作。这应该不难回答,但我不明白您到底想做什么?你在用故事板吗?在发布代码之前,请编辑您的问题并留出4个空格,以使其更具可读性。是的,我使用的是故事板,我想在文本字段中应用左边距,我遵循此代码,但我无法实现此功能。