使用UITextBorderStyleNone为UITextField设置填充 [英] Set padding for UITextField with UITextBorderStyleNone

查看:157
本文介绍了使用UITextBorderStyleNone为UITextField设置填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的 UITextFields 使用自定义背景。这工作正常,除了我必须使用 UITextBorderStyleNone 使它看起来很漂亮。这会强制文本粘在左边而不进行任何填充。

I wanted to use a custom background for my UITextFields. This works fine except for the fact that I have to use UITextBorderStyleNone to make it look pretty. This forces the text to stick to the left without any padding.

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

Can I set a padding manually so that it looks similar to UITextBorderStyleRoundedRect except for using my custom background image?

推荐答案

我发现了一个简洁的小黑客为此设置左边的填充确切的情况。

I found a neat little hack to set the left padding for this exact situation.

基本上,你将 UITextField 的leftView属性设置为填充大小的空视图想要:

Basically, you set the leftView property of the UITextField to be an empty view of the size of the padding you want:

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

对我来说就像魅力一样!

Worked like a charm for me!

Swift 3 中,可以通过这样做来实现

In Swift 3, it can be done by doing that

let paddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 20))
textField.leftView = paddingView
textField.leftViewMode = .always

这篇关于使用UITextBorderStyleNone为UITextField设置填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆