Indent the text in a UITextField

后端 未结 8 1299
日久生厌
日久生厌 2020-12-07 08:35

My question is as simple as the title, but here\'s a little more:

I have a UITextField, on which I\'ve set the background image. The problem is that the text hugs so

相关标签:
8条回答
  • 2020-12-07 08:58

    Sometimes the leftView of the textField is a Apple's magnifying glass image. If so, you can set an indent like this:

        UIView *leftView = textField.leftView;
        if (leftView && [leftView isKindOfClass:[UIImageView class]]) {
            leftView.contentMode = UIViewContentModeCenter;
            leftView.width = 20.0f;  //the space you want indented.
        }
    
    0 讨论(0)
  • 2020-12-07 09:02

    I suggest converting your UITextField to a UITextView and setting the contentInset. For example to indent by 10 spaces:

    textview.contentInset=UIEdgeInsetsMake(0, 10, 0, 0);
    
    0 讨论(0)
  • 2020-12-07 09:03

    You have to subclass and override textRectForBounds: and editingRectForBounds:. Here is a UITextfield subclass with custom background and vertical and horizontal padding:

    @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
    

    Usage:

    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];
    
    0 讨论(0)
  • 2020-12-07 09:06

    This is the quickest way I've found without doing any subclasses:

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

    In Swift:

    let spacerView = UIView(frame:CGRect(x:0, y:0, width:10, height:10))
    textField.leftViewMode = UITextFieldViewMode.Always
    textField.leftView = spacerView
    
    0 讨论(0)
  • 2020-12-07 09:07

    I turned this into a nice little extension for use inside Storyboards:

    public extension UITextField {
        @IBInspectable public var leftSpacer:CGFloat {
            get {
                return leftView?.frame.size.width ?? 0
            } set {
                leftViewMode = .Always
                leftView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-07 09:07

    My 2 cents:

    class PaddedTextField : UITextField {
        var insets = UIEdgeInsets.zero
        var verticalPadding:CGFloat = 0
        var horizontalPadding:CGFloat = 0
    
        override func textRect(forBounds bounds: CGRect) -> CGRect {
            return CGRect(x: bounds.origin.x + insets.left, y: bounds.origin.y + insets.top, width: bounds.size.width - (insets.left + insets.right), height: bounds.size.height - (insets.top + insets.bottom));
        }
    
        override func editingRect(forBounds bounds: CGRect) -> CGRect {
            return textRect(forBounds: bounds)
        }
    }
    
    0 讨论(0)
提交回复
热议问题