美文网首页
修改UITextFeild占位符字符串的颜色

修改UITextFeild占位符字符串的颜色

作者: 海边的遐想 | 来源:发表于2016-03-31 17:00 被阅读74次


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

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

#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


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

#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占位符字符串的颜色

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

  • 修改占位符颜色

    UIColor * placeHolderColor = [UIColor colorWithRed:153/25...

  • iOS设置TextField的placeholder的颜色,位置

    前言 由于项目需要修改TextField的占位符的颜色,位置等,总结下如何设置UITextField的占位符的一些...

  • 2018-07-18 day03

    字符串以及字符串运算符 -%s 字符串占位符(格式符)-%d 整数占位符-%f 浮点数占位符 %.nf小数点后保留...

  • 2019-03-12

    1.格式字符串:在字符串中通过格式占位符来表示字符串中变法的部分语法:包含格式占位符的字符 %(给格式占位符赋值的...

  • iOS开发小记!

    1:Block 循环引用的问题 宏定义 2: 修改textField的占位符(placeholder)的字体颜色、...

  • 随笔

    1.占位符% %s表示占位符类型是str字符串类型%d表示占位符类型是digital数字类型使用占位符的时候,还需...

  • 03 常用技巧介绍-(格式化、编码、while else、运算符

    1 格式化输出 % 在字符串里的占位符;%s:字符串占位符%d:数字占位符;%%:格式化输出中表示单纯的% %(p...

  • 2018-08-08 - Lisa’s Code Standar

    一、字符串 1.1、字符串一定要放在strings.xml里面,不要直接用 1.2、字符串占位符的使用:占位符使用...

  • iOS开发-OC占位符

    oc中各种打印时的占位符 常用的一些占位符: %@:字符串占位符 %d:整型 %ld:长整型 %f:浮点型 %c:...

网友评论

      本文标题:修改UITextFeild占位符字符串的颜色

      本文链接:https://www.haomeiwen.com/subject/ahgelttx.html