[轉]改變UITextField placeHolder顏色、字型

來源:互聯網
上載者:User

標籤:

本文轉載至 http://m.blog.csdn.net/blog/a394318511/8025170

我們有時需要定製化UITextField對象的風格,可以添加許多不同的重寫方法,來改變文字欄位的顯示行為。這些方法都會返回一個CGRect結構,制定了文字欄位每個組件的邊界範圍,甚至修改placeHolder顏色,字型。

 

– textRectForBounds:      //重寫來重設文字地區

– drawTextInRect:         //改變繪文字屬性.重寫時調用super可以按預設圖形屬性繪製,若自己完全重寫繪製函數,就不用調用super了.

– placeholderRectForBounds:  //重寫來重設預留位置地區

– drawPlaceholderInRect:  //重寫改變繪製預留位置屬性.重寫時調用super可以按預設圖形屬性繪製,若自己完全重寫繪製函數,就不用調用super了

– borderRectForBounds:  //重寫來重設邊緣地區

– editingRectForBounds:  //重寫來重設編輯地區

– clearButtonRectForBounds:  //重寫來重設clearButton位置,改變size可能導致button的圖片失真

– leftViewRectForBounds:

– rightViewRectForBounds:

 

通過– drawPlaceholderInRect:方法可改變placeHolder顏色、字型,請看代碼:

首先定義一個類CustomTextField讓它繼承UITextField實現以下方法即可:

//控制清除按鈕的位置

-(CGRect)clearButtonRectForBounds:(CGRect)bounds

{

   return CGRectMake(bounds.origin.x + bounds.size.width - 50, bounds.origin.y + bounds.size.height -20, 16, 16);

}

 

//控制placeHolder的位置,左右縮20

-(CGRect)placeholderRectForBounds:(CGRect)bounds

{

    

    //return CGRectInset(bounds, 20, 0);

   CGRect inset = CGRectMake(bounds.origin.x+100, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些

   return inset;

}

//控制顯示文本的位置

-(CGRect)textRectForBounds:(CGRect)bounds

{

    //return CGRectInset(bounds, 50, 0);

    CGRect inset = CGRectMake(bounds.origin.x+190, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些

    

    return inset;

 

}

//控制編輯文本的位置

-(CGRect)editingRectForBounds:(CGRect)bounds

{

    //return CGRectInset( bounds, 10 , 0 );

    

   CGRect inset = CGRectMake(bounds.origin.x +10, bounds.origin.y, bounds.size.width -10, bounds.size.height);

   return inset;

}

//控制左視圖位置

- (CGRect)leftViewRectForBounds:(CGRect)bounds

{

   CGRect inset = CGRectMake(bounds.origin.x +10, bounds.origin.y, bounds.size.width-250, bounds.size.height);

   return inset;

    //return CGRectInset(bounds,50,0);

}

 

//控制placeHolder的顏色、字型

- (void)drawPlaceholderInRect:(CGRect)rect

{

    //CGContextRef context = UIGraphicsGetCurrentContext();

    //CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);

    [[UIColororangeColor] setFill];

    

    [[selfplaceholder] drawInRect:rectwithFont:[UIFontsystemFontOfSize:20]];

}

 

 

//下面是使用CustomTextField的代碼,可放在viewDidLoad等方法中

    _textField = [[CustomTextField alloc] initWithFrame:CGRectMake(20, 150, 280, 30)];

    _textField.placeholder = @"請輸入帳號資訊";

    _textField.borderStyle = UITextBorderStyleRoundedRect;

    _textField.textAlignment = UITextAlignmentLeft;

    _textField.delegate = self;

    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;

    _textField.text = @"aa";

    UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon-iwant-2.png"]];

    _textField.leftView = imgv;

    _textField.leftViewMode = UITextFieldViewModeAlways;

    [self.view addSubview:_textField];

 

轉自:http://blog.csdn.net/leeyehong_self/article/details/7939147

[轉]改變UITextField placeHolder顏色、字型

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.