TextField(实训)

本文介绍了一个iOS登录界面的设计实现过程,包括使用自定义背景、文本框提示文字的颜色设置、边框样式及圆角处理等技巧,并展示了如何在输入框前添加图标提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一直写网络请求,忘了说登录注册界面了。其实主要就是一些按钮,但是感觉这里很多小细节,比如button的圆角啊什么的还是很有用处的,一般app都用得到。不多说,贴代码!!!这里只贴登录界面,注册界面用到的在登录注册里面都有了。

import UIKit

class SignViewController: UIViewController, UITextFieldDelegate {

    var phoneText : UITextField!
    var passwordText : UITextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "登录"
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "sign_bg")!)
        self.view.contentMode = .scaleAspectFill
        
        phoneText = UITextField(frame: CGRect(x: 50, y: 240, width: screenwidth-100, height: 40))
        phoneText.attributedPlaceholder=NSAttributedString(string: "手机号", attributes: [NSForegroundColorAttributeName: kRGBColorFromHex(rgbValue: 0x656565)])
        phoneText.textColor = kRGBColorFromHex(rgbValue: 0xffffff)
        phoneText.layer.borderWidth=1
        phoneText.layer.borderColor=kRGBColorFromHex(rgbValue: 0x656565).cgColor
        phoneText.layer.cornerRadius=10
        phoneText.clearButtonMode=UITextFieldViewMode.whileEditing;
        phoneText.keyboardType = UIKeyboardType.numberPad
        let phoneLeftView = UIView(frame: CGRect(x:10, y:10, width:40, height:40));
        let phonePic = UIImageView(frame: CGRect(x:10,y:10, width:20, height:20));
        phonePic.contentMode = .scaleAspectFill
        phonePic.image=UIImage(named: "sign_man.png");
        phoneLeftView.addSubview(phonePic)
        phoneText.leftView = phoneLeftView
        phoneText.leftViewMode=UITextFieldViewMode.always;
        
        passwordText = UITextField(frame: CGRect(x: 50, y: 300, width: screenwidth-100, height: 40))
        //passwordText.placeholder = "密码"
        passwordText.attributedPlaceholder=NSAttributedString(string: "密码", attributes: [NSForegroundColorAttributeName: kRGBColorFromHex(rgbValue: 0x656565)])
        passwordText.textColor = kRGBColorFromHex(rgbValue: 0xffffff)
        passwordText.layer.borderWidth=1
        passwordText.layer.borderColor=kRGBColorFromHex(rgbValue: 0x656565).cgColor
        passwordText.layer.cornerRadius=10
        passwordText.clearButtonMode=UITextFieldViewMode.whileEditing;
        let passwordLeftView = UIView(frame: CGRect(x:0, y:0, width:40, height:40));
        let passwordPic = UIImageView(frame: CGRect(x:10,y:10, width:20, height:20));
        passwordPic.contentMode = .scaleAspectFill
        passwordPic.image=UIImage(named: "sign_lock.png");
        passwordLeftView.addSubview(passwordPic)
        passwordText.leftView = passwordLeftView
        passwordText.leftViewMode=UITextFieldViewMode.always;
        
        self.view.addSubview(phoneText)
        self.view.addSubview(passwordText)

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func kRGBColorFromHex(rgbValue: Int) -> (UIColor) {
        return UIColor(red: ((CGFloat)((rgbValue & 0xFF0000) >> 16)) / 255.0,green: ((CGFloat)((rgbValue & 0xFF00) >> 8)) / 255.0,blue: ((CGFloat)(rgbValue & 0xFF)) / 255.0,alpha: 1.0)
    }
}

看看这里都用到了什么来设置我们平时用到的各种常用的控件。最基本的背景颜色,虽然是backgroundcolor,但是不只是能设置背景颜色,还可以用来设置背景图片。

如果想要设置在未输入的时候的提示文字,如果是想只设置文字,则直接用placeholder就好,这里由于背景的原因,还要改变一下颜色,所以就用了上面的方法,内容和颜色可以一次性搞定。

然后就是边框的宽度,颜色还有圆角的角度。最后!!!感觉加了这个瞬间app就高大上了一丢丢,就是在输入手机号、密码的框前面可以加一个小图标。这个叫做leftView。我们做好一个想要的大小的UIView,直接加到leftView里就行了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值