代码之家  ›  专栏  ›  技术社区  ›  Sebastian Wramba

使用UITextBorderStyleNone设置UITextField的填充

  •  380
  • Sebastian Wramba  · 技术社区  · 15 年前

    我想用一个自定义的背景为我的 UITextFields UITextBorderStyleNone

    我可以手动设置一个填充,使它看起来像 UITextBorderStyleRoundedRect

    32 回复  |  直到 7 年前
        1
  •  856
  •   dahiya_boy    7 年前

    我找到了一个巧妙的小技巧来为这种情况设置左边的填充。

    UITextField 要成为所需填充大小的空视图,请执行以下操作:

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

    对我来说就像个魔咒!

    Swift 3/Swift 4

    let paddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 20))
    textField.leftView = paddingView
    textField.leftViewMode = .always
    
        2
  •  180
  •   nickromano    7 年前

    我创建了这个类别实现,并将其添加到 .m

    @implementation UITextField (custom)
        - (CGRect)textRectForBounds:(CGRect)bounds {
            return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 8,
                              bounds.size.width - 20, bounds.size.height - 16);
        }
        - (CGRect)editingRectForBounds:(CGRect)bounds {
            return [self textRectForBounds:bounds];
        }
    @end
    

    UIView . 尽管如此,似乎还是缺少了一些东西,无法控制文本字段中的填充。

    class CustomTextField: UITextField {
        struct Constants {
            static let sidePadding: CGFloat = 10
            static let topPadding: CGFloat = 8
        }
    
        override func textRect(forBounds bounds: CGRect) -> CGRect {
            return CGRect(
                x: bounds.origin.x + Constants.sidePadding,
                y: bounds.origin.y + Constants.topPadding,
                width: bounds.size.width - Constants.sidePadding * 2,
                height: bounds.size.height - Constants.topPadding * 2
            )
        }
    
        override func editingRect(forBounds bounds: CGRect) -> CGRect {
            return self.textRect(forBounds: bounds)
        }
    }
    
        3
  •  141
  •   bandejapaisa    9 年前

    Xcode>6的swift3版本,您可以在Interface Builder/Storyboard中编辑插入值。

    import UIKit
    
    @IBDesignable
    class FormTextField: UITextField {
    
        @IBInspectable var inset: CGFloat = 0
    
        override func textRect(forBounds bounds: CGRect) -> CGRect {
            return bounds.insetBy(dx: inset, dy: inset)
        }
    
        override func editingRect(forBounds bounds: CGRect) -> CGRect {
            return textRect(forBounds: bounds)
        }
    
    }
    

    enter image description here

        4
  •  77
  •   Inder Kumar Rathore user4622654    7 年前

    在iOS 6中 myTextField.leftView = paddingView; 正在引发问题

    这就解决了问题

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

    用于右对齐文本字段 CATransform3DMakeTranslation(-5, 0, 0) 后期编码器 在评论中

        5
  •  70
  •   Brody Robertson    10 年前

    奥斯特克斯菲尔德

    #import <UIKit/UIKit.h>
    
    @interface OSTextField : UITextField
    
    @property (nonatomic, assign) UIEdgeInsets edgeInsets;
    
    @end
    

    #import "OSTextField.h"
    
    @implementation OSTextField
    
    - (id)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            self.edgeInsets = UIEdgeInsetsZero;
        }
        return self;
    }
    
    -(id)initWithCoder:(NSCoder *)aDecoder{
        self = [super initWithCoder:aDecoder];
        if(self){
            self.edgeInsets = UIEdgeInsetsZero;
        }
        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
    
        6
  •  25
  •   Camsoft    12 年前

    只是子类UITextField如下:

    @implementation DFTextField
    
    
    - (CGRect)textRectForBounds:(CGRect)bounds
    {
        return CGRectInset(bounds, 10.0f, 0);
    }
    
    - (CGRect)editingRectForBounds:(CGRect)bounds
    {
        return [self textRectForBounds:bounds];
    }
    
    
    @end
    

    这将在每侧添加10个点的水平填充。

        7
  •  21
  •   Kirit Vaghela    11 年前

    目标C代码

    #import <UIKit/UIKit.h>
    
    @interface MyTextField : UITextField
    
    @property (nonatomic) IBInspectable CGFloat padding;
    
    @end
    

    我的文本域.m

    #import "MyTextField.h"
    
    IB_DESIGNABLE
    @implementation MyTextField
    
    @synthesize padding;
    
    -(CGRect)textRectForBounds:(CGRect)bounds{
        return CGRectInset(bounds, padding, padding);
    }
    
    -(CGRect)editingRectForBounds:(CGRect)bounds{
        return [self textRectForBounds:bounds];
    }
    
    @end
    

    enter image description here

        8
  •  21
  •   777Q    10 年前

    PaddingTextField.swift文件

    import UIKit
    class PaddingTextField: UITextField {
    
    @IBInspectable var paddingLeft: CGFloat = 0
    @IBInspectable var paddingRight: CGFloat = 0
    
    override func textRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectMake(bounds.origin.x + paddingLeft, bounds.origin.y,
            bounds.size.width - paddingLeft - paddingRight, bounds.size.height);
    }
    
    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        return textRectForBounds(bounds)
    }}
    
    1. 设置textfield类为PaddingTextField,并根据需要自定义填充 enter image description here enter image description here

    2. 好好享受吧

    final

        9
  •  18
  •   Paulo Miguel Almeida    12 年前

    根据Evil Trout的答案,您可能需要创建一个类别,以便更容易在多个应用程序中使用。

    头文件:

    @interface UITextField (PaddingText)
    
    -(void) setLeftPadding:(int) paddingValue;
    
    -(void) setRightPadding:(int) paddingValue;
    @end
    

    实现文件:

    #import "UITextField+PaddingText.h"
    
    @implementation UITextField (PaddingText)
    
    -(void) setLeftPadding:(int) paddingValue
    {
        UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, paddingValue, self.frame.size.height)];
        self.leftView = paddingView;
        self.leftViewMode = UITextFieldViewModeAlways;
    }
    
    -(void) setRightPadding:(int) paddingValue
    {
        UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, paddingValue, self.frame.size.height)];
        self.rightView = paddingView;
        self.rightViewMode = UITextFieldViewModeAlways;
    }
    
    @end
    

    用法示例

    #import "UITextField+PaddingText.h"
    
    [self.YourTextField setLeftPadding:20.0f];
    

    希望对你们有帮助

        10
  •  15
  •   Sebastian Wramba    11 年前

    Swift版本:

    extension UITextField {
        @IBInspectable var padding_left: CGFloat {
            get {
                LF.log("WARNING no getter for UITextField.padding_left")
                return 0
            }
            set (f) {
                layer.sublayerTransform = CATransform3DMakeTranslation(f, 0, 0)
            }
        }
    }
    

    所以你可以在IB中赋值

    IBInspectable setting represented in Interface Builder

        11
  •  12
  •   Paras Joshi    13 年前

    UIView 它有你的背景图像和 UITextField 在里面。设置 输入框 宽度为 UIViewWidth-(paddingSize x 2) 同样的高度,然后把它放在点上 paddingSize,paddingSize .

        12
  •  10
  •   King-Wizard    11 年前

    只是子类 Swift版本

    import UIKit
    
    class CustomTextField: UITextField {
    
        override func textRectForBounds(bounds: CGRect) -> CGRect {
           return CGRectInset(bounds, 25.0, 0)
        }
    
        override func editingRectForBounds(bounds: CGRect) -> CGRect {
           return self.textRectForBounds(bounds)
        }
    
    }
    

    这将添加 25 指向两边。

        13
  •  8
  •   Stoto    13 年前

    我是基于Nate的解决方案,但是后来我发现当你使用leftView/rightView属性时,这会导致问题,所以它更好地调整super的实现,因为它会考虑到left/right视图。

    - (CGRect)textRectForBounds:(CGRect)bounds {
        CGRect ret = [super textRectForBounds:bounds];
        ret.origin.x = ret.origin.x + 5;
        ret.size.width = ret.size.width - 10;
        return ret;
    }
    
    - (CGRect)editingRectForBounds:(CGRect)bounds {
        return [self textRectForBounds:bounds];
    }
    
        14
  •  6
  •   Beninho85    9 年前

    @IBDesignable
    class FormTextField: UITextField {
    
        @IBInspectable var paddingLeft: CGFloat = 0
        @IBInspectable var paddingRight: CGFloat = 0
    
        override func textRect(forBounds bounds: CGRect) -> CGRect {
            return CGRect(x: bounds.origin.x + paddingLeft, y: bounds.origin.y, width: bounds.size.width - paddingLeft - paddingRight, height: bounds.size.height)
        }
    
        override func editingRect(forBounds bounds: CGRect) -> CGRect {
            return textRect(forBounds: bounds)
        }
    }
    
        15
  •  6
  •   Community Mohan Dere    5 年前

    使用UITextBorderStyleNone:Swift设置UITextField的填充

    - (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方法),将我的任何文本字段发送到该方法,并为左右两侧添加填充,并通过只编写一行代码来提供文本和背景颜色,如下所示:

    [self modifyTextField:self.firstNameTxtFld];
    

    这在iOS7上运行得非常好!

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

    我想我应该用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()
    


    干杯!

        16
  •  5
  •   DrPatience    10 年前

    下面是如何在SWIFT中实现这一点

    @IBOutlet weak var yourTextField: UITextField!
    
    override func viewDidLoad() {
    super.viewDidLoad()
    let paddingView = UIView(frame: CGRectMake(0, 0, 10, self.yourTextField.frame.height))
    yourTextField.leftView = paddingView
    yourTextField.leftViewMode = UITextFieldViewMode.Always
    }
    }
    

    Resource

        17
  •  5
  •   Phil    10 年前

    Swift 2.0版本:

    let paddingView: UIView = UIView(frame: CGRectMake(0, 0, 5, 20))
    textField.leftView = paddingView
    textField.leftViewMode = UITextFieldViewMode.Always;
    
        18
  •  5
  •   Bhavin_m    7 年前

    Swift 4.0 以下版本 extension 是工作。两者兼有 Left Right 填充 UITextField . 实际上是的 IBInspectable 用于序列图像板配置。您可以直接从Interface Builder/Storyboard设置值。这是在swift4.0版本和xcode9.0中测试的代码

    Clear Button 在同一时间 输入框 那你就得保持右边空白。

    import UIKit
    
    extension UITextField {
    
        @IBInspectable var paddingLeft: CGFloat {
            get {
                return leftView!.frame.size.width
            }
            set {
                let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
                leftView = paddingView
                leftViewMode = .always
            }
        }
    
        @IBInspectable var paddingRight: CGFloat {
            get {
                return rightView!.frame.size.width
            }
            set {
                let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
                rightView = paddingView
                rightViewMode = .always     
            }
        }
    }  
    
        19
  •  4
  •   woody121 ean5533    13 年前

    ^这些建议非常适合那些以编程方式创建接口的人。

    但是对于我们这些使用Xcode接口生成器的人来说,有两种简单的方法:

    • 图像优先于正方形,因此您仍然可以获得正常图像背景所需的填充,而不必实际绘制正方形。

        20
  •  3
  •   Anuj Kumar Rai    11 年前

    最好的方法是使用UITextField的子类和in.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,点击identity inspector并用你自己的“CustomTextField”类选项替换UITextfield。

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

        21
  •  3
  •   David Wang    8 年前

    Swift 3版本:

    class CustomTextField:UITextField{
    
        required init?(coder aDecoder: NSCoder){
            super.init(coder: aDecoder)
        }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
        }
    
        override func textRect(forBounds bounds: CGRect) -> CGRect {
            return CGRect.init(x: bounds.origin.x + 8, y: bounds.origin.y, width: bounds.width, height: bounds.height)
        }
    
        override func editingRect(forBounds bounds: CGRect) -> CGRect {
            return self.textRect(forBounds:bounds)
        }
    }
    
        22
  •  2
  •   Community Mohan Dere    8 年前

    Nate Flink's 例如 UITextField

    override func rightViewRectForBounds(bounds: CGRect) -> CGRect {
        let rightViewBounds = super.rightViewRectForBounds(bounds)
    
        return CGRectMake(CGRectGetMinX(rightViewBounds) - 10, CGRectGetMinY(rightViewBounds), CGRectGetWidth(rightViewBounds), CGRectGetHeight(rightViewBounds))
    }
    

    上面的代码为 rightView 输入框 .

        23
  •  2
  •   Ben Sullivan    9 年前

    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)
     }
    }
    
        24
  •  2
  •   Hardik Thakkar    9 年前

    下面是一个在UITextfield中填充的Swift代码

     func txtPaddingVw(txt:UITextField) {
         let paddingView = UIView(frame: CGRectMake(0, 0, 10, 10))
         txt.leftViewMode = .Always
         txt.leftView = paddingView
     }
    

    并使用

    self.txtPaddingVw(txtPin)
    
        25
  •  2
  •   rikiluo    9 年前

    您可以使用类别。将“padding”设置为“left”和“right”

    UITextField+Padding.h

    @interface UITextField (Padding)
    @property (nonatomic, assign) CGFloat paddingValue;
    @property (nonatomic, assign) CGFloat leftPadding;
    @property (nonatomic, assign) CGFloat rightPadding;
    
    //overwrite
    -(CGRect)textRectForBounds:(CGRect)bounds;
    -(CGRect)editingRectForBounds:(CGRect)bounds;
    @end
    

    UITextField+Padding.m

    #import "UITextField+Padding.h"
    #import <objc/runtime.h>
    
    static char TAG_LeftPaddingKey;
    static char TAG_RightPaddingKey;
    static char TAG_Left_RightPaddingKey;
    
    @implementation UITextField (Padding)
    
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
    -(CGRect)textRectForBounds:(CGRect)bounds {
    
    CGFloat offset_Left=0;
    CGFloat offset_Right=0;
    if (self.paddingValue>0) {
        offset_Left=self.paddingValue;
        offset_Right=offset_Left;
    }else{
        if (self.leftPadding>0){
            offset_Left=self.leftPadding;
        }
        if (self.rightPadding>0){
            offset_Right=self.rightPadding;
        }
    }
    
    if (offset_Left>0||offset_Right>0) {
        return CGRectMake(bounds.origin.x+ offset_Left ,bounds.origin.y ,
                          bounds.size.width- (offset_Left+offset_Right), bounds.size.height-2 );
     }else{
        return bounds;
     }
    }
    
    
    
    -(CGRect)editingRectForBounds:(CGRect)bounds {
        return [self textRectForBounds:bounds];
    }
    #pragma clang diagnostic pop
    
    
    #pragma maek -setter&&getter
    - (CGFloat)paddingValue
    {
        return [objc_getAssociatedObject(self,&TAG_Left_RightPaddingKey) floatValue];
    }
    -(void)setPaddingValue:(CGFloat)paddingValue
    {
        objc_setAssociatedObject(self, &TAG_Left_RightPaddingKey, @(paddingValue), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    -(CGFloat)leftPadding
    {
        return [objc_getAssociatedObject(self,&TAG_LeftPaddingKey) floatValue];
    }
    
    -(void)setLeftPadding:(CGFloat)leftPadding
    {
        objc_setAssociatedObject(self, &TAG_LeftPaddingKey, @(leftPadding), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    -(CGFloat)rightPadding
    {
        return [objc_getAssociatedObject(self,&TAG_RightPaddingKey) floatValue];
    }
    
    -(void)setRightPadding:(CGFloat)rightPadding
    {
        objc_setAssociatedObject(self, &TAG_RightPaddingKey, @(rightPadding), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    @end
    

    你可以这样设置填充 或 self.phoneNumTF.leftPadding=10.f;

        26
  •  1
  •   Kishor Kundan    12 年前

    @邪恶鳟鱼的答案是伟大的。我已经使用这种方法很长一段时间了。它唯一缺少的是“处理大量文本字段”。我尝试了其他方法,但似乎不起作用。

    -(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]];
            }
        }
    }
    
        27
  •  1
  •   Lubakis    12 年前

        28
  •  1
  •   Mongi Zaidi    12 年前

    @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
    

        29
  •  1
  •   Saqib Saud    10 年前
    textField.layer.borderWidth = 3;
    

    将添加边框,作为我的填充。

        30
  •  0
  •   Paras Joshi    13 年前

    UITextField 添加填充的地方是创建一个单独的 UIView