首页 > 其他 > 详细

UITextFeild的用法

时间:2016-03-28 23:21:57      阅读:370      评论:0      收藏:0      [点我收藏+]

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

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

#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;出现):

          技术分享

 

 

 

======= 方法二 =================================

#import "MyTextField.h"

@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

 

运行效果:

     技术分享

UITextFeild的用法

原文:http://www.cnblogs.com/nxz-diy/p/5331080.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!