UITextField 的文本插入?
我想插入 UITextField
的文本。
这可能吗?
I would like to inset the text of a UITextField
.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想插入 UITextField
的文本。
这可能吗?
I would like to inset the text of a UITextField
.
Is this possible?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(30)
覆盖
-textRectForBounds:
只会更改占位符文本的插入。要更改可编辑文本的插入,您还需要覆盖-editingRectForBounds:
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:
我能够通过以下方式做到这一点:
当然记得导入 QuartzCore 并将框架添加到您的项目中。
I was able to do it through:
Of course remember to import QuartzCore and also add the Framework to your project.
如果您只需要左边距,您可以尝试以下操作:
它对我有用。我希望这会有所帮助。
If you need just a left margin, you can try this:
It works for me. I hope this may help.
在从 UITextField 派生的类中,至少重写这两个方法:
如果没有其他内容,可能会像这样简单:
UITextField 提供了几种可以重写的定位方法。
In a class derived from UITextField, override at least this two methods:
It might be as simple as this if you have no additional content:
UITextField provides several positioning methods you can override.
@IBInspectable
、@IBDesignable
swift 类怎么样。您将在故事板中看到这一点。
更新 - Swift 3
How about an
@IBInspectable
,@IBDesignable
swift class.You'll see this in your storyboard.
Update - Swift 3
如果您有明确的按钮,则接受的答案将不适用于您。我们还应该防止苹果将来通过调用 super 来改变事情。
因此,为了确保文本不会与清除按钮重叠,我们首先从
super
获取“默认”值,然后根据需要进行调整。此代码将在文本字段的顶部、左侧和底部添加 10 像素的插图:
注意:UIEdgeInsetsMake 按以下顺序获取参数:top、left、bottom ,对。
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
.So, to make sure the text doesn't overlap the clear button, let's get the 'default' value from
super
first, then adjust as necessary.This code will add a 10px insets on the top, left and bottom of the text field:
Note: UIEdgeInsetsMake takes parameters in the order: top, left, bottom, right.
我想我会提供一个 Swift 解决方案
Swift 3+
Thought I would supply a Swift Solution
Swift 3+
斯威夫特
Swift
对于正在寻找更简单解决方案的人们。
在
UIView
中添加UITextField
。为了模拟文本字段周围的插图,我保留了 10 像素,宽度比视图小 20 像素。对于文本字段周围的圆角边框,请使用视图的边框For people, who are searching for a easier solution.
Add the
UITextField
inside aUIView
. To simulate an inset around text field I keep 10 px left and width is 20px less than the view. For a rounded corner border around the textfield, use the view's border使用
textRectForBounds:
是正确的方法。我已将其包装在我的子类中,因此您可以简单地使用textEdgeInsets
。请参阅 SSTextField。Using
textRectForBounds:
is the correct approach. I have wrapped this up in my subclass so you can simply usetextEdgeInsets
. See SSTextField.您可以通过设置 leftView 来设置 UITextField 的文本插入。
像这样:
You can set text inset for UITextField by setting the leftView.
Like this:
斯威夫特
Swift
向 UITextField 添加填充的一个好方法是子类化 UITextField 并添加 edgeInsets 属性。然后设置 edgeInsets,UITextField 将相应地绘制。这也将在自定义 leftView 或 rightView 集上正常运行。
OSTextField.h
OSTextField.m
A good approach to add padding to UITextField is to subclass UITextField and add an edgeInsets property. You then set the edgeInsets and the UITextField will be drawn accordingly. This will also function correctly with a custom leftView or rightView set.
OSTextField.h
OSTextField.m
Swift 5 版本的 Christopher 的答案 以及额外的用法示例
用法:-
Swift 5 version of Christopher's answer with extra usage sample
Usage: -
Swift 3 / 可在界面构建器中设计 / 单独的水平和水平方向垂直昆虫/开箱即用
的用法:
&
Swift 3 / Designable in Interface builder / Separate horizontal & vertical insects / usable out of the box
usage:
&
我在IB中这样做了,我在textView后面创建了一个UIView,它有点长。将文本字段背景颜色设置为清除。
I did this in IB where I created a UIView Behind the textView that was a little bit longer. With the textField background color set to clear.
Swift 4.2 版本:
Swift 4.2 version:
这是我发现的最快的方法,无需执行任何子类:
在 Swift 中:
It's the quickest way I've found without doing any subclasses:
In Swift:
正如 @Adam Waite 指出的那样,您必须子类化是荒谬的,因为
UITextField
已经实现了这些方法。这是一个公开工厂方法的 swift 扩展,也可以在我们的 类别回购:It's absurd you have to subclass, since
UITextField
already implements the methods, as @Adam Waite points out. Here's a swift extension that exposes a factory method, also available in our categories repo:这是用 Swift 3 编写的相同子类 UITextField。它与 Swift 的早期版本有很大不同,您将看到:
顺便说一句,如果您只想控制一侧的插入,您也可以执行如下操作。如果您将图像放置在 UITextField 的顶部,但您希望它向用户显示在文本字段内,则仅调整左侧插图的这个特定示例会派上用场:
Here is the same subclassed UITextField written in Swift 3. It is quite different from prior versions of Swift, as you'll see:
Incidentally, you can also do something like the following, if you want to control the inset of just one side. This particular example of adjusting only the left inset comes in handy if you place an image on top of the UITextField but you want it to appear to the user to be within the text field:
您可以通过将文本字段设为
UITextField
的子类并重写-textRectForBounds:
方法来调整文本在文本字段中的位置。You can adjust the positioning of the text within a text field by making it a subclass of
UITextField
and overriding the-textRectForBounds:
method.我子类化了 UITextField 来处理这个问题,它支持左、上、右和下插入,以及清晰的按钮定位。
MRDInsetTextField.h
MRDInsetTextField.m
使用示例,其中 *_someTextField* 来自带有 MRDInsetTextField 自定义类的 nib/storyboard 视图
I subclased UITextField to handle this that supports left, top, right and bottom inset, and clear button positioning as well.
MRDInsetTextField.h
MRDInsetTextField.m
Example of usage where *_someTextField* comes from nib/storyboard view with MRDInsetTextField custom class
这并不像其他示例那么短,但采用了完全不同的方法来解决这个问题。请注意,插入符号仍将开始与左边缘齐平,但在键入/显示时文本将正确缩进。如果您只是寻找左边距并且您已经在文本字段中使用
UITextFieldDelegate
,则无需子类化即可实现此功能。您需要设置默认文本属性和键入属性。您可以在创建文本字段时设置默认文本属性。您需要在委托中设置的类型属性。如果您还使用占位符,您也需要将其设置为相同的边距。总而言之,你会得到这样的东西。首先在
UITextField
类上创建一个类别。然后,如果您使用占位符,请确保使用设置相同缩进的属性占位符。创建一个具有适当属性的默认属性字典,如下所示:
然后,导入上述类别,并在创建文本字段时设置默认缩进、委托并使用上面定义的默认占位符属性。例如:
最后,在委托中,实现
textFieldDidBeginEditing
方法,如下所示:This is not as short as the other examples, but takes an entirely different approach to solving this problem. Note, the caret will still begin flush to the left edge but the text will be properly indented when typed/displayed. This works without subclassing if your looking for just a left margin and you are already using
UITextFieldDelegate
for your text fields. You need to set both the default text attributes and the typing attributes. You set the default text attributes when you create the text field. The typing attributes you need to set in the delegate. If you are also using a placeholder you will want to set that to the same margin as well. Putting it altogether you get something like this.First create a category on the
UITextField
class.Then, if you are using placed holders make sure to use an attributed placeholder setting the same indent. Create a default attributed dictionary with the proper attributes, something like this:
Then, import the above category and whenever you create a text field set the default indent, the delegate and use the default placeholder attributes defined above. For example:
Lastly, in the delegate, implement the
textFieldDidBeginEditing
method, something like this:真正有效并涵盖所有情况的解决方案:
offsetBy
而不是insetBy
。示例:
A solution that actually works and covers all cases:
offsetBy
notinsetBy
.Rect
.Sample:
我发现 roberto.buratti 发布的选项是最快的解决方案,这里是 Swift 中的:
I found the option posted by roberto.buratti the fastest solution, here it is in Swift:
我通常会尝试避免子类化,但如果您已经这样做了,那么这会起作用:
I normally try avoid subclassing but this works if you have already:
要引入另一个不需要子类化的解决方案:
已使用 iOS 7 和 iOS 8 进行测试。两者均可正常工作。 Apple 仍然有可能修改 UITextField 的图层层次结构,从而将事情搞砸。
To throw in another solution that has no need for subclassing:
Tested with iOS 7 and iOS 8. Both working. Still there might be the chance of Apple modifying the UITextField's layer hierarchy screwing up things badly.
这是一个全面的 Swift 答案,其中包括 leftView(自定义图标)和自定义清除按钮,两者都在 Interface Builder 中设置并具有可自定义的插图。
Here's a comprehensive Swift answer that includes a leftView (custom icon) and a custom clear button, both set in Interface Builder with customizable insets.
您可能需要这个也支持 leftView 和 rightView 的解决方案。
You might need this solution which also supports leftView and rightView. ????
如果您只想更改顶部和左侧缩进,则
// 占位符位置
// 文本位置
If you want to change TOP and LEFT indent only then
// placeholder position
// text position