电子邮件图标和占位符文本互相拥抱。
我想在它们之间添加一些空格。
以下是我用来创建这些字段的代码。
// view for fields background
var view = UIView(frame: CGRectMake(35, 300, 300, 90));
view.backgroundColor = UIColor.whiteColor();
view.layer.cornerRadius = 5.0
// email text field
var email = UITextField(frame: CGRectMake(0, 0, 300, 50));
email.backgroundColor = UIColor.whiteColor();
var emailIcon = UIImageView();
var emailIconImage = UIImage(named: "email_icon.png");
emailIcon.image = emailIconImage;
email.leftView = emailIcon;
email.leftViewMode = UITextFieldViewMode.Always
email.layer.cornerRadius=5.0;
email.attributedPlaceholder = NSAttributedString(string:"E-mail",
attributes:[NSForegroundColorAttributeName: UIColor.grayColor()])
emailIcon.frame = CGRect(x: 30, y: 15, width: 30, height: 20)
view.addSubview(emailIcon)
谢谢!
答案 0 :(得分:5)
在没有子类化的情况下,最简单的方法是给予 EmailIcon宽度比emailIconImage宽度大10个点。然后根据您的需要将 EmailIcon的contentMode设置为居中或右侧。这是obj C中的代码,希望您能在swift中找到它:
EmailIcon = [[UIImageView alloc] initWithImage: emailIconImage];
EmailIcon.frame = CGRectMake(0.0, 0.0, EmailIcon.image.size.width+10.0, EmailIcon.image.size.height);
EmailIcon.contentMode = UIViewContentModeCenter; //Or UIViewContentModeRight
textField.leftView = EmailIcon;
textField.leftViewMode = UITextFieldViewModeAlways;
答案 1 :(得分:0)
您可以更改其图层中的位置:
txtemail.layer.position = CGPointMake(30, 30)
答案 2 :(得分:0)
将UITextField子类化并实现以下方法,将占位符文本放在您想要的位置 -
placeholderRectForBounds: