ios UITextField 的文本插入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2694411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:09:54  来源:igfitidea点击:

Text inset for UITextField?

ioscocoa-touchuitextfield

提问by RunLoop

I would like to inset the textof a UITextField.

我想插页的文字UITextField

Is this possible?

这可能吗?

回答by azdev

Overriding -textRectForBounds:will only change the inset of the placeholder text. To change the inset of the editable text, you need to also override -editingRectForBounds:

覆盖-textRectForBounds:只会更改占位符文本的插图。要更改可编辑文本的插图,您还需要覆盖-editingRectForBounds:

// placeholder position
- (CGRect)textRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 10, 10);
}

// text position
- (CGRect)editingRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 10, 10);
}

回答by chuthan20

I was able to do it through:

我能够做到:

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

Of course remember to import QuartzCore and also add the Framework to your project.

当然记得导入 QuartzCore 并将框架添加到您的项目中。

回答by roberto.buratti

If you need just a left margin, you can try this:

如果你只需要一个左边距,你可以试试这个:

UItextField *textField = [[UITextField alloc] initWithFrame:...];
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, textField.frame.size.height)];
leftView.backgroundColor = textField.backgroundColor;
textField.leftView = leftView;
textField.leftViewMode = UITextFieldViewModeAlways;

It works for me. I hope this may help.

这个对我有用。我希望这可能会有所帮助。

回答by drawnonward

In a class derived from UITextField, override at least this two methods:

在派生自 UITextField 的类中,至少覆盖以下两个方法:

- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;

It might be as simple as this if you have no additional content:

如果您没有其他内容,可能就这么简单:

return CGRectInset(bounds , 10, 10);

UITextField provides several positioning methods you can override.

UITextField 提供了几种可以覆盖的定位方法。

回答by mash

How about an @IBInspectable, @IBDesignableswift class.

怎么样@IBInspectable@IBDesignable快速类。

@IBDesignable
class TextField: UITextField {
    @IBInspectable var insetX: CGFloat = 6 {
       didSet {
         layoutIfNeeded()
       }
    }
    @IBInspectable var insetY: CGFloat = 6 {
       didSet {
         layoutIfNeeded()
       }
    }

    // placeholder position
    override func textRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds , insetX , insetY)
    }

    // text position
    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds , insetX , insetY)
    }
}

You'll see this in your storyboard.

您会在故事板中看到这一点。

enter image description here

在此处输入图片说明

Update - Swift 3

更新 - Swift 3

@IBDesignable
class TextField: UITextField {
    @IBInspectable var insetX: CGFloat = 0
    @IBInspectable var insetY: CGFloat = 0

    // placeholder position
    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: insetX, dy: insetY)
    }

    // text position
    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: insetX, dy: insetY)
    }
}

回答by Chris Nolet

If you have a clear button, the accepted answer won't work for you. We should also guard against Apple changing things in the future by calling super.

如果您有一个清除按钮,则接受的答案对您不起作用。我们还应该通过调用super.

So, to make sure the text doesn't overlap the clear button, let's get the 'default' value from superfirst, then adjust as necessary.

因此,为了确保文本不会与清除按钮重叠,让我们首先获取“默认”值super,然后根据需要进行调整。

This code will add a 10px insets on the top, left and bottom of the text field:

此代码将在文本字段的顶部、左侧和底部添加一个 10 像素的插图:

@interface InsetTextField : UITextField

@end


@implementation InsetTextField

// Placeholder position
- (CGRect)textRectForBounds:(CGRect)bounds {
    CGRect rect = [super textRectForBounds:bounds];
    UIEdgeInsets insets = UIEdgeInsetsMake(10, 10, 10, 0);

    return UIEdgeInsetsInsetRect(rect, insets);
}

// Text position
- (CGRect)editingRectForBounds:(CGRect)bounds {
    CGRect rect = [super editingRectForBounds:bounds];
    UIEdgeInsets insets = UIEdgeInsetsMake(10, 10, 10, 0);

    return UIEdgeInsetsInsetRect(rect, insets);
}

// Clear button position
- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
    CGRect rect = [super clearButtonRectForBounds:bounds];

    return CGRectOffset(rect, -5, 0);
}

@end

Note: UIEdgeInsetsMake takes parameters in the order: top, left, bottom, right.

注:UIEdgeInsetsMake需要在命令参数:顶部

回答by smitt04

Thought I would supply a Swift Solution

以为我会提供一个 Swift 解决方案

import UIKit

class TextField: UITextField {
    let inset: CGFloat = 10

    // placeholder position
    override func textRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds , inset , inset)
    }

    // text position
    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds , inset , inset)
    }

    override func placeholderRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds, inset, inset) 
    }
}

Swift 3+

斯威夫特 3+

import UIKit

class TextField: UITextField {
    let inset: CGFloat = 10

    // placeholder position
    override func textRect(forBounds: CGRect) -> CGRect {
        return forBounds.insetBy(dx: self.inset , dy: self.inset)
    }

    // text position
    override func editingRect(forBounds: CGRect) -> CGRect {
        return forBounds.insetBy(dx: self.inset , dy: self.inset)
    }

    override func placeholderRect(forBounds: CGRect) -> CGRect {
        return forBounds.insetBy(dx: self.inset, dy: self.inset)
    }
}

回答by Sam Soffes

Using textRectForBounds:is the correct approach. I have wrapped this up in my subclass so you can simply use textEdgeInsets. See SSTextField.

使用textRectForBounds:是正确的方法。我已将其封装在我的子类中,因此您可以简单地使用textEdgeInsets. 请参阅SSTextField

回答by lcl

Swift

迅速

 class TextField: UITextField {

    let inset: CGFloat = 8

    // placeholder position
    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: inset, dy: inset)
    }

    // text position
    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: inset, dy: inset)
    }
}

回答by Golden

You can set text inset for UITextField by setting the leftView.

您可以通过设置 leftView 为 UITextField 设置文本插入。

Like this:

像这样:

UITextField *yourTextField = [[UITextField alloc] init];
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
leftView.backgroundColor = [UIColor clearColor];
yourTextField.leftViewMode = UITextFieldViewModeAlways;
yourTextField.leftView = leftView;