iOS設置TextField的placeholder的顏色,位置,字體,光標顏色


轉載自:Z了個Y   簡書

 

一.設置placeholder的顏色字體

1.iOS6.0之后蘋果提供了attributedPlaceholder屬性可以設置

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 300, 200, 35)];
textField.borderStyle = UITextBorderStyleRoundedRect;
NSString *holderText = @"這個是placeholder";
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
[placeholder addAttribute:NSForegroundColorAttributeName
                        value:[UIColor greenColor]
                        range:NSMakeRange(0, holderText.length)];
[placeholder addAttribute:NSFontAttributeName
                        value:[UIFont boldSystemFontOfSize:15]
                        range:NSMakeRange(0, holderText.length)];
textField.attributedPlaceholder = placeholder;
[self.view addSubview:textField];

2.通過KVC訪問內部變量直接設置

textField.placeholder = @"手機號碼"; 
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; 
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

二.設置placeholder的文本的位置

蘋果給我們提供了以下方法可以自定義一個TextField,我們可以重寫這些方法定制自己的UITextField。

// drawing and positioning overrides
- (CGRect)borderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- (CGRect)rightViewRectForBounds:(CGRect)bounds;
- (void)drawTextInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;

1.下面是自定義的一個UITextField類,根據自己的需求進行定制
.h文件

#import <UIKit/UIKit.h>
@interface ZYTextField : UITextField
@end

.m文件

#define Default_FontColor ZYRGBColor(77, 150, 132)
#import "ZYTextField.h"
@implementation ZYTextField
//通過代碼創建
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}
//通過xib創建
-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setUpUI];
}

- (void)setUpUI
{
//    設置border
//    self.layer.masksToBounds = YES;
//    self.layer.cornerRadius = 22;
//    self.backgroundColor = Default_FontColor;
//    self.layer.borderColor = [UIColor blackColor].CGColor;
//    self.layer.borderWidth = 1;
    //字體大小
    self.font = [UIFont systemFontOfSize:15];
    //字體顏色
    self.textColor = Default_FontColor;
    //光標顏色
    self.tintColor= self.textColor;
    //占位符的顏色和大小
    [self setValue:ZYRGBColor(167, 167, 167) forKeyPath:@"_placeholderLabel.textColor"];
    [self setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    // 不成為第一響應者
    [self resignFirstResponder];
}
/**
 * 當前文本框聚焦時就會調用
 */
- (BOOL)becomeFirstResponder
{
    // 修改占位文字顏色
    [self setValue:self.textColor forKeyPath:@"_placeholderLabel.textColor"];
    return [super becomeFirstResponder];
}

/**
 * 當前文本框失去焦點時就會調用
 */
- (BOOL)resignFirstResponder
{
    // 修改占位文字顏色
    [self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    return [super resignFirstResponder];
}
//控制placeHolder的位置
-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
    CGRect inset = CGRectMake(bounds.origin.x+15, bounds.origin.y, bounds.size.width -15, bounds.size.height);
    return inset;
}

//控制顯示文本的位置
-(CGRect)textRectForBounds:(CGRect)bounds
{
    CGRect inset = CGRectMake(bounds.origin.x+15, bounds.origin.y, bounds.size.width -15, bounds.size.height);
    return inset;
}

//控制編輯文本的位置
-(CGRect)editingRectForBounds:(CGRect)bounds
{
    CGRect inset = CGRectMake(bounds.origin.x +15, bounds.origin.y, bounds.size.width -15, bounds.size.height);
    return inset;
}

2.使用方法
a.純代碼創建UITextField

UITextField *textField = [[ZYTextField alloc]initWithFrame:CGRectMake(0, 300, 300, 35)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];

b.使用xib或者storyboard創建
修改UITextField的類屬性


使用xib或者storyboard創建
.png


3.實際運行效果圖



作者:Z了個Y
鏈接:http://www.jianshu.com/p/db8773b13388
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM