我想为我的UITextFields使用一个自定义背景。这很好,除了我必须使用UITextBorderStyleNone来使它看起来很漂亮。这将迫使文本保持在左边,没有任何填充。

我可以手动设置填充,使它看起来类似于UITextBorderStyleRoundedRect除了使用我的自定义背景图像?


当前回答

编辑:在iOS 11.3.1中仍然有效

在iOS 6 myTextField。leftView = paddingView;引起了问题

这就解决了问题

myTextField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0)

对于右对齐的文本字段使用CATransform3DMakeTranslation(- 5,0,0),正如latenitecoder在注释中提到的那样

其他回答

到目前为止,我找到的最佳解决方案是分类。这就是我如何在左右两侧添加5点填充:

@implementation UITextField (Padding)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectMake(bounds.origin.x + 5, bounds.origin.y,
                      bounds.size.width - 10, bounds.size.height);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self textRectForBounds:bounds];
}
#pragma clang diagnostic pop

@end

#pragma’s只是用来删除恼人的警告

@Evil trout的回答很棒。我使用这种方法已经有一段时间了。它唯一缺少的是“处理大量文本字段”。我尝试了其他方法,但似乎不奏效。

子类化UITextField只是为了添加填充对我来说没有任何意义。因此,我迭代了所有UITextFields来添加填充。

-(void) addPaddingToAllTextFields:(UIView*)view {

    for(id currentView in [view subviews]){
        if([currentView isKindOfClass:[UITextField class]]) {
            // Change value of CGRectMake to fit ur need
            [currentView setLeftView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)]];
            [currentView setLeftViewMode:UITextFieldViewModeAlways];
        }

        if([currentView respondsToSelector:@selector(subviews)]){
            [textfieldarray addObjectsFromArray:[self addPaddingToAllTextFields:currentView]];
        }
    }
}

最好的方法是使用UITextField的子类并在.m文件中创建一个类

 #import "CustomTextField.h"
 #import <QuartzCore/QuartzCore.h>
 @implementation CustomTextField


- (id)initWithCoder:(NSCoder*)coder 
 {
  self = [super initWithCoder:coder];

  if (self) {

//self.clipsToBounds = YES;
//[self setRightViewMode:UITextFieldViewModeUnlessEditing];

self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,15,46)];
self.leftViewMode=UITextFieldViewModeAlways;
   }

  return self;

 }

通过这样做,去到你的故事板或xib,点击身份检查器,用你自己的“CustomTextField”在类选项替换UITextfield。

注意:如果你只是给文本框自动布局填充,那么你的应用程序将不会运行,只显示空白屏幕。

Swift 3解决方案

class CustomTextField: UITextField {

 override func textRect(forBounds bounds: CGRect) -> CGRect {
  return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y + 8, width: bounds.size.width - 20, height: bounds.size.height - 16)
 }

 override func editingRect(forBounds bounds: CGRect) -> CGRect {
  return self.textRect(forBounds: bounds)
 }
}

用UITextBorderStyleNone: Swift设置UITextField的填充

基于@Evil Trout投票最多的答案,我在我的ViewController类中创建了一个自定义方法,如下所示:

- (void) modifyTextField:(UITextField *)textField
{
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
    textField.leftView = paddingView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.rightView = paddingView;
    textField.rightViewMode = UITextFieldViewModeAlways;

    [textField setBackgroundColor:[UIColor whiteColor]];
    [textField setTextColor:[UIColor blackColor]];
}

现在我可以调用该方法内部(viewDidLoad方法),并发送我的任何TextFields到该方法,并为左右添加填充,并通过只写一行代码给出文本和背景颜色,如下所示:

[self modifyTextField:self.firstNameTxtFld];

这在iOS 7上非常完美! 我知道添加太多的视图可能会使这个类的加载变得更重一些。但当考虑到其他解决方法的难度时,我发现自己更偏向于这种方法,使用这种方法也更灵活。;)

感谢黑客“邪恶鳟鱼”!(鞠躬)

我认为我应该用Swift更新这个答案的代码片段:

既然Swift允许我们为现有的类写扩展,我们就这样写吧。

extension UITextField {
    func addPaddingToTextField() {
        let paddingView: UIView = UIView.init(frame: CGRectMake(0, 0, 8, 20))
        self.leftView = paddingView;
        self.leftViewMode = .Always;
        self.rightView = paddingView;
        self.rightViewMode = .Always;

    
        self.backgroundColor = UIColor.whiteColor()
        self.textColor = UIColor.blackColor()
    }
}

用法:

self.firstNameTxtFld.addPaddingToTextField()

希望这对其他人有帮助! 干杯!