缩进UITextField中的文本

Dyl*_*o42 76 iphone cocoa-touch objective-c uitextfield ios

我的问题就像标题一样简单,但这里有一点:

我有一个UITextField,我在其上设置了背景图像.问题是文本紧贴着它的边缘,这也是图像的边缘.我希望我的文本字段看起来像Apple的,在背景图像和文本开始的位置之间有一些水平空间.

adj*_*lli 232

这是我在没有做任何子类的情况下发现的最快的方法:

UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[textfield setLeftViewMode:UITextFieldViewModeAlways];
[textfield setLeftView:spacerView];
Run Code Online (Sandbox Code Playgroud)

在Swift中:

let spacerView = UIView(frame:CGRect(x:0, y:0, width:10, height:10))
textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = spacerView
Run Code Online (Sandbox Code Playgroud)

  • 这比继承子更清晰,并且正确地处理了清晰的按钮.希望我能给+2. (18认同)
  • 我同意,+ 2,如果可以的话.一旦我在很多投票的答案中读到"子类",我内心地呻吟,因为它总是更难以且更容易出错,开始愚弄绘图方法 (8认同)
  • 很好的解决方案,只需将其转换为Swift:`var spacerView = UIView(frame:CGRect(x:0,y:0,width:10,height:10)); textfield.leftViewMode = UITextFieldViewMode.Always textfield.leftView = spacerView` (6认同)
  • @jsiodtb我试过在Swift中使用它,但它阻止了我的segue被执行.知道为什么吗?我_am_在`viewDidLoad()`中调用它.它在`viewWillAppear()`中也不起作用. (2认同)

Jan*_*ano 42

您必须子类化并覆盖textRectForBounds:和editingRectForBounds:.这是一个UITextfield子类,具有自定义背景和垂直和水平填充:

@interface MyUITextField : UITextField 
@property (nonatomic, assign) float verticalPadding;
@property (nonatomic, assign) float horizontalPadding;
@end

#import "MyUITextField.h"
@implementation MyUITextField
@synthesize horizontalPadding, verticalPadding;
- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectMake(bounds.origin.x + horizontalPadding, bounds.origin.y + verticalPadding, bounds.size.width - horizontalPadding*2, bounds.size.height - verticalPadding*2);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self textRectForBounds:bounds];
}
@end
Run Code Online (Sandbox Code Playgroud)

用法:

UIImage *bg = [UIImage imageNamed:@"textfield.png"];
CGRect rect = CGRectMake(0, 0, bg.size.width, bg.size.height);
MyUITextField *textfield = [[[MyUITextField alloc] initWithFrame:rect] autorelease];
textfield.verticalPadding = 10; 
textfield.horizontalPadding = 10;
[textfield setBackground:bg];
[textfield setBorderStyle:UITextBorderStyleNone];
[self.view addSubview:textfield];
Run Code Online (Sandbox Code Playgroud)


Bro*_*son 26

向UITextField添加填充的一个好方法是继承UITextField,重写矩形方法并添加edgeInsets属性.然后,您可以设置edgeInsets,并相应地绘制UITextField.这也可以使用自定义leftView或rightView集正常运行.

OSTextField.h

#import <UIKit/UIKit.h>

@interface OSTextField : UITextField

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end
Run Code Online (Sandbox Code Playgroud)

OSTextField.m

#import "OSTextField.h"

@implementation OSTextField

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self){
        self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return self;
}

- (CGRect)textRectForBounds:(CGRect)bounds {
    return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

@end
Run Code Online (Sandbox Code Playgroud)

  • 整洁的解决方案.+1使用'UIEdgeInsets`,绝对是手头任务的合适工具! (3认同)

Oxc*_*cug 12

我把它变成了一个很好的小扩展,可以在Storyboard中使用:

public extension UITextField {
    @IBInspectable public var leftSpacer:CGFloat {
        get {
            if let l = leftView {
                return l.frame.size.width
            } else {
                return 0
            }
        } set {
            leftViewMode = .Always
            leftView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)