效果如图:
很简单,继承UITextfield,覆盖父类如下两个方法!
- (CGRect)textRectForBounds:(CGRect)bounds
- (CGRect)editingRectForBounds:(CGRect)bounds
代码如下:
WXTextField.h
#import <UIKit/UIKit.h>
@interface WXTextField : UITextField
@end
WXTextField.m
#import "WXTextField.h"
@implementation WXTextField
//控制文本所在的的位置,左右缩 10
- (CGRect)textRectForBounds:(CGRect)bounds
{
return CGRectInset(bounds, 30, 0);
}
//控制编辑文本时所在的位置,左右缩 10
- (CGRect)editingRectForBounds:(CGRect)bounds
{
return CGRectInset(bounds, 30, 0);
}
@end

本文介绍如何通过继承 UITextfield 类并覆盖其方法来自定义文本输入框的位置,实现左右各缩进10像素的效果。
606

被折叠的 条评论
为什么被折叠?



