将文本缩进UITextField [英] Indent the text in a UITextField

查看:106
本文介绍了将文本缩进UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题和标题一样简单,但这里有一点:

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

我有一个UITextField,我已经设置了背景图片。问题是,文本拥抱如此接近它的边缘,这也是图像的边缘。我想我的文本字段看起来像苹果,在背景图像和文本开始之间有一些水平空间。

I have a UITextField, on which I've set the background image. The problem is that the text hugs so closely to it's edge, which is also the edge of the image. I want my text field to look like Apple's, with a bit of horizontal space between the background image and where the text starts.

推荐答案

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];

在Swift中:

let spacerView = UIView(frame:CGRect(x:0, y:0, width:10, height:10))
textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = spacerView

这篇关于将文本缩进UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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