iOS基础iOSUIview子类

iOS --UITextFiled 深究

2015-11-12  本文已影响664人  iOS_成才录

一、目录

二、UITextFiled特点

三、应用

1. 自定义文本框 JPTextFiled

// 文字颜色
self.textColor = [UIColor whiteColor];
// 文本框的光标颜色
self.tintColor = [UIColor whiteColor];

2. 实现,更改textFiled的占位文字颜色

2.1 分析:
 // default is nil. string is drawn 70% gray
@property(nullable, nonatomic,copy)   NSString   *placeholder;         
@property(nullable, nonatomic,copy)   NSAttributedString *attributedPlaceholder 
- (void)drawTextInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;
2.2 解决方案:
   // 设置带有属性的占位文字(富文本)
   self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName : [UIColor grayColor]}];
    ```
+ 方案二:重写系统的drawPlaceholderInRect:方法
 - 不仅可以设置占位文字,还可以决定将占位文字画在那个地方。

```objc
 - (void)drawPlaceholderInRect:(CGRect)rect
{
   // 占位文字画在哪个矩形框里面
   CGRect placeholderRect = self.bounds;
   placeholderRect.origin.y = (self.height - self.font.lineHeight) * 0.5;

   // 文字属性
   NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
   attrs[NSForegroundColorAttributeName] = [UIColor redColor];
   attrs[NSFontAttributeName] = self.font;
   [self.placeholder drawInRect:placeholderRect withAttributes:attrs];

   // 占位文字画在哪个位置
//    CGPoint point;
//    point.x = 0;
//    point.y = (self.height - self.font.lineHeight) * 0.5;
//
//    // 文字属性
//    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
//    attrs[NSForegroundColorAttributeName] = [UIColor redColor];
//    attrs[NSFontAttributeName] = self.font;
//    [self.placeholder drawAtPoint:point withAttributes:attrs];
}```

+ 方案三: KVC 方式来设置 placeholderLabel文字颜色(设置占位文字的控件UILabel)
  - 它是UITextFiled的私有成员变量,不过通过运行时可以打印出UITextFiled有该属性,至于如何打印UITextFiled的所有成员变量包括私有变量下面会讲
 - 在自定义的UITextField类中写入下面代码,设置占位文字颜色
```objc
// 设置占位文字颜色:为红色
   [self setValue: [UIColor redColor] forKeyPath:@"placeholderLabel.textColor"];
2.3 UITextFile分类的抽取,方便以后快速的设置占位文字颜色
#import <UIKit/UIKit.h>

@interface UITextField (JPExtension)
/** 占位文字颜色 */
@property (nonatomic, strong) UIColor *placeholderColor;
//- (void)setPlaceholderColor:(UIColor *)placeholderColor;
//- (UIColor *)placeholderColor;
@end
#import "UITextField+JPExtension.h"

/** 占位文字颜色 */
static NSString * const JPPlaceholderColorKey = @"placeholderLabel.textColor";

@implementation UITextField (JPExtension)

- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    BOOL change = NO;
    
    // 保证有占位文字
    if (self.placeholder == nil) {
        self.placeholder = @" ";
        change = YES;
    }
    
    // 设置占位文字颜色
    [self setValue:placeholderColor forKeyPath:  JPPlaceholderColorKey];

    // 恢复原状
    if (change) {
        self.placeholder = nil;
    }
}

- (UIColor *)placeholderColor
{
    return [self valueForKeyPath: JPPlaceholderColorKey];
}
@end
 // 设置占位文字颜色 : 设置为红色
    textFiled.placeholderColor = [UIColor redColor];

3. 监听文本框聚焦事件,设置占位文字颜色

3.1 方案一:代理(不推荐)
@interface JPTextField () <UITextFieldDelegate>

@end
@implementation JPTextField

- (void)awakeFromNib
{
    self.delegate = self; // 自己成为自己的代理,不推荐
}

#pragma mark - <UITextFieldDelegate>
   - (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];
3.2 方案二:通知
- (void)awakeFromNib
{
    // 文本框的光标颜色
    self.tintColor = [UIColor whiteColor];
    // 文字颜色
    self.textColor = [UIColor whiteColor];
    // 设置占位文字颜色
    [self setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];

    // 通过通知-》监听文本框的开始和结束编辑
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing) name:UITextFieldTextDidBeginEditingNotification object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing) name:UITextFieldTextDidEndEditingNotification object:self];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)beginEditing
{
    [self setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
}

- (void)endEditing
{
    [self setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];
}
3.3 方案三:addTarget:
    // 通过addTarget:-》监听文本框的开始和结束编辑
    [self addTarget:self action:@selector(beginEditing) forControlEvents:UIControlEventEditingDidBegin];
    [self addTarget:self action:@selector(endEditing) forControlEvents:UIControlEventEditingDidEnd];
    - (void)beginEditing
{
    [self setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
}

- (void)endEditing
{
    [self setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];
}
3.4 方案四:是否是第一响应者处理(最终版)
#import "JPTextField.h"

// 占位文字颜色
#define JPPlaceholderColorKey @"placeholderLabel.textColor"
// 默认的占位文字颜色
#define JPPlaceholderDefaultColor [UIColor grayColor]
// 聚焦的占位文字颜色
#define JPPlaceholderFocusColor [UIColor whiteColor]

@interface JPTextField()

@end

@implementation JPTextField

- (void)awakeFromNib
{
    // 文本框的光标颜色
    self.tintColor = JPPlaceholderFocusColor;
    // 文字颜色
    self.textColor = JPPlaceholderFocusColor;
    // 设置占位文字颜色
    [self resignFirstResponder];
}

/**
 * 文本框聚焦时调用(弹出当前文本框对应的键盘时调用)
 */
- (BOOL)becomeFirstResponder
{
    [self setValue:JPPlaceholderFocusColor forKeyPath:JPPlaceholderColorKey];
    return [super becomeFirstResponder];
}

/**
 * 文本框失去焦点时调用(隐藏当前文本框对应的键盘时调用)
 */
- (BOOL)resignFirstResponder
{
    [self setValue:JPPlaceholderDefaultColor forKeyPath:JPPlaceholderColorKey];
    return [super resignFirstResponder];
}
@end
上一篇下一篇

猜你喜欢

热点阅读