iOS UITextField获取或失去焦点方法

news/2024/7/20 23:10:55 标签: iOS

实现如下效果,点击“账号”或“密码”,横线颜色由灰色变成蓝色
在这里插入图片描述

在这里插入图片描述

代码如下:

UILabel *mNameTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 50, 40)];
mNameTitle.text = @"账号";
mNameTitle.textColor = [mUserUtil hexStringToColor:@"#333333" Alpha:1.0f];
mNameTitle.font = [UIFont systemFontOfSize:15];
    
mNameField = [[UITextField alloc] initWithFrame:CGRectMake(mNameTitle.frame.origin.x + mNameTitle.frame.size.width + 10, mNameTitle.frame.origin.y, KScreen_Width - 50 - mNameTitle.frame.size.width, mNameTitle.frame.size.height)];
mNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
mNameField.font = [UIFont systemFontOfSize:15];
mNameField.returnKeyType = UIReturnKeyDone;
mNameField.delegate = self;
mNameField.textColor = [mUserUtil hexStringToColor:@"#333333" Alpha:1.0f];
mNameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:@{NSForegroundColorAttributeName: [CurrencyUtil hexStringToColor:@"#888888" Alpha:1.0f]}];
[mNameField setSecureTextEntry:NO];

mNameLine = [[UIView alloc] init];
mNameLine.frame = CGRectMake(20, mNameField.frame.origin.y + mNameField.frame.size.height, KScreen_Width - 40, 0.5);
mNameLine.backgroundColor= [mUserUtil hexStringToColor:@"#cccccc" Alpha:1.0f];

UILabel *mPassTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, mNameLine.frame.origin.y + mNameLine.frame.size.height + 30, 50, 40)];
mPassTitle.text = @"密码";
mPassTitle.textColor = [mUserUtil hexStringToColor:@"#333333" Alpha:1.0f];
mPassTitle.font = [UIFont systemFontOfSize:15];

mPassField = [[UITextField alloc] initWithFrame:CGRectMake(mPassTitle.frame.origin.x + mPassTitle.frame.size.width + 10, mPassTitle.frame.origin.y, KScreen_Width - 80 - mPassTitle.frame.size.width, mPassTitle.frame.size.height)];
    mPassField.clearButtonMode = UITextFieldViewModeWhileEditing;
mPassField.font = [UIFont systemFontOfSize:15];
mPassField.placeholder = @"请输入密码";
mPassField.returnKeyType = UIReturnKeyDone;
mPassField.delegate = self;
mPassField.textColor = [mUserUtil hexStringToColor:@"#333333" Alpha:1.0f];
[mPassField setSecureTextEntry:YES];
[mPassField setValue:[mUserUtil hexStringToColor:@"#888888" Alpha:1.0f] forKeyPath:@"placeholderLabel.textColor"];

mPassLine = [[UIView alloc] init];
mPassLine.frame = CGRectMake(20, mPassField.frame.origin.y + mPassField.frame.size.height, KScreen_Width - 40, 0.5);
mPassLine.backgroundColor= [mUserUtil hexStringToColor:@"#cccccc" Alpha:1.0f];

[self.view addSubview:mNameTitle];
[self.view addSubview:mNameField];
[self.view addSubview:mNameLine];
[self.view addSubview:mPassTitle];
[self.view addSubview:mPassField];
[self.view addSubview:mPassLine];

实现UITextFieldDelegate代理方法

// 获得焦点
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if(textField == mNameField)
        mNameLine.backgroundColor = [mUserUtil hexStringToColor:@"#005cac" Alpha:1.0f];
    else if (textField == mPassField) {
        mPassLine.backgroundColor = [mUserUtil hexStringToColor:@"#005cac" Alpha:1.0f];
    }
    return YES;
}
// 失去焦点
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if(textField == mNameField)
        mNameLine.backgroundColor = [mUserUtil hexStringToColor:@"#cccccc" Alpha:1.0f];
    else if (textField == mPassField) {
        mPassLine.backgroundColor = [mUserUtil hexStringToColor:@"#cccccc" Alpha:1.0f];
    }
}

http://www.niftyadmin.cn/n/1549807.html

相关文章

unity游戏开发之entitas框架

组件-实体-系统 (ECS \CES)游戏编程模型: 组件 一个组件可以使用C中的结构体来进行设计。它没有方法,只是用来存储一些数据,并不在它之上进行动作。一个经典的实现方式是,每一个不同的组件都继承至一个抽…

Swift UIAlertController的用法

自 iOS8 起,苹果就建议告警框使用 UIAlertController 来代替 UIAlertView 和 UIActionSheel。下面总结了一些常见的用法。 1. 简单的应用(同时按钮响应Handler使用闭包函数) import UIKitclass ViewController: UIViewController {override func view…

php提示Array to string conversion 解决方案

这是个错误是我们在PHP使用中,把数组当成了字符串使用。有两种情况下会出现这种错误。 场景一 这种场景比较少,大多数都是新手才会犯,也很容易发现解决错误。就是字面意思,将数组当成字符串使用了。 示例: $arr ar…

swift—UIColor十六进制

新建一个文件UIColorhex.swift 2.代码 import Foundation import UIKitextension UIColor{class func colorWithHex(hexStr:String) -> UIColor{return UIColor.colorWithHex(hexStr : hexStr, alpha:1)}class func colorWithHex(hexStr:String, alpha:Float) -> UIColo…

wordpress内存不足问题“Fatal error:out of memory”

方法一: 在 .htaccess 文件中加上 php_value memory_limit 256M 方法二: 在你的 wp-config.php 文件中加上 define (WP_MEMORY_LIMIT, 256M ); 方法三: wp-settings.php,编辑这个文件,修改define(WP_MEMORY_LIM…

iOS-电子签名

有些APP开发中需要用到电子签名,封装成一个View,使用灵活. ZHSignatureView.h里的一些配置参数,可以不传 interface ZHSignatureView : UIView /**已签名的照片,跳转传入*/ property(nonatomic, strong) UIImage *signImage;/**签名笔划颜色,默认blackColor*/ prop…

tp5.1调用多个数据库更新数据时的bug

后台开发时,涉及到多个数据库的调用,遇到一个很妖的问题,更新数据时会把主键自动变成0; 原因是查询的时候是通过 getBy键名(值) 的方式查询数据,此键值不是主键的原因,更新数据时就出现了这个问题 解决方…

iOS签名功能的实现

github地址:https://github.com/xx040145/JPZSign