你的位置:首页 > 操作系统

[操作系统]UITextFeild的用法


一. 修改占位字符串的 颜色:

=======方法一 ======================================

#import "ViewController.h"
#import "MyTextField.h"

@interface ViewController ()
{
    UITextField *_textF;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
   
    MyTextField *textF = [[MyTextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    _textF = textF;
    [self.view addSubview:textF];
    
    textF.backgroundColor = [UIColor whiteColor];
    textF.borderStyle = UITextBorderStyleRoundedRect;
    textF.leftViewMode = UITextFieldViewModeWhileEditing;
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    btn.backgroundColor=[UIColor greenColor];
    textF.leftView = btn;
    
    textF.placeholder = @"哈哈";
    
    UIColor *color = [UIColor colorWithRed:100/255.0 green:200/255.0 blue:100/255.0 alpha:0.7];
    [textF setValue:color forKeyPath:@"_placeholderLabel.textColor"];//修改占位字符串“哈哈”的颜色
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_textF resignFirstResponder];
}

@end

 

运行效果:

      1. 没有进入编辑状态效果(textF.leftView = btn;隐藏):

  

 

 

  2. 进入编辑状态效果(textF.leftView = btn;出现):

         
@implementation MyTextField


- (void)drawPlaceholderInRect:(CGRect)rect{
    UIColor *placeholderColor = [UIColor redColor];//设置颜色
    [placeholderColor setFill];
    
    CGRect placeholderRect = CGRectMake(rect.origin.x+30, (rect.size.height- self.font.pointSize)/5, rect.size.width, self.font.pointSize);//设置距离
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode = NSLineBreakByTruncatingMiddle;
    style.alignment = self.textAlignment;
    NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, self.font, NSFontAttributeName, placeholderColor, NSForegroundColorAttributeName, nil];
    
    [self.placeholder drawInRect:placeholderRect withAttributes:attr];
}

@end

 

#import "ViewController.h"
#import "MyTextField.h"

@interface ViewController ()
{
    UITextField *_textF;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    

    MyTextField *textF = [[MyTextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    _textF = textF;
    [self.view addSubview:textF];
    
    textF.backgroundColor = [UIColor whiteColor];
    textF.borderStyle = UITextBorderStyleRoundedRect;
    textF.leftViewMode = UITextFieldViewModeWhileEditing;
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    btn.backgroundColor=[UIColor greenColor];
    textF.leftView = btn;
    
    textF.placeholder = @"哈哈哈哈哈哈";
    
//    UIColor *color = [UIColor colorWithRed:100/255.0 green:200/255.0 blue:100/255.0 alpha:0.7];
//    [textF setValue:color forKeyPath:@"_placeholderLabel.textColor"];//修改占位字符串“哈哈”的颜色
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_textF resignFirstResponder];
}

@end

 

运行效果: