Tec*_*ain 2 iphone objective-c ipad ios
我想从左边填充填充到textfield.我已经在textfield中添加了一个背景图像,因为textfield中的文本总是从最左边开始.我尝试了下面的解决方案,但这对于太多文本字段不起作用.
UIView *fieldEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
self.txt_fname.leftViewMode = UITextFieldViewModeAlways;
self.txt_fname.leftView = fieldEmail;
Run Code Online (Sandbox Code Playgroud)
请建议我一些简单的解决方案,其中我不必创建一个单独的视图来给出填充.
问题中接受的答案对我不起作用. 使用UITextBorderStyleNone为UITextField设置填充
您可以像下面的代码一样创建UITextFiled的类别:
.H班
#import <UIKit/UIKit.h>
@interface UITextField (Textpadding)
-(CGRect)textRectForBounds:(CGRect)bounds;
-(CGRect)editingRectForBounds:(CGRect)bounds;
@end
Run Code Online (Sandbox Code Playgroud)
.M班
#import "UITextField+Textpadding.h"
@implementation UITextField (Textpadding)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
-(CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x+20 , bounds.origin.y ,
bounds.size.width , bounds.size.height-2 );
}
-(CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
Run Code Online (Sandbox Code Playgroud)
此类别将在运行时默认设置所有文本字段填充.
如何创建类别
现在就是这个类中的代码作为我的答案.