使用 UITextBorderStyleNone 为 UITextField 设置填充 [英] Set padding for UITextField with UITextBorderStyleNone
问题描述
我想为我的 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/Swift 4中,可以这样做
let paddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 20))
textField.leftView = paddingView
textField.leftViewMode = .always
这篇关于使用 UITextBorderStyleNone 为 UITextField 设置填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!