UITextField 游標位置,placeholder樣式

weixin_33716557發表於2017-03-20

一.Placeholder 樣式更改

方法一:重寫drawPlaceholderInRect:(CGRect)rect方法

-(void)drawPlaceholderInRect:(CGRect)rect{
    UIColor *placeholderColor = [UIColor dt_ColorWithString:@"#999999"];//設定顏色
    [placeholderColor setFill];
    CGRect placeholderRect = CGRectMake(rect.origin.x+5, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.lineHeight );//設定Placeholder  位置.尺寸
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode            = NSLineBreakByTruncatingTail;
    style.alignment                = self.textAlignment;
    NSDictionary *attr             = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, self.font, NSFontAttributeName, placeholderColor, NSForegroundColorAttributeName, nil];//設定Placeholder 樣式
    
    [self.placeholder drawInRect:placeholderRect withAttributes:attr];
}

方法二:通過KVC修改Placeholder顏色

    UITextField *textField1 = [[UITextField alloc] init];
    textField1.frame = CGRectMake(textFieldX, CGRectGetMaxY(textField.frame) + padding, viewWidth - 2 * textFieldX, textFieldH);
    textField1.borderStyle = UITextBorderStyleRoundedRect;
    textField1.placeholder = @"請輸入佔位文字";
    textField1.font = [UIFont systemFontOfSize:14];
    // "通過KVC修改佔位文字的顏色"
    [textField1 setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.view addSubview:textField1];

方法三: 修改attributedPlaceholder屬性

NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"請輸入佔位文字" attributes:
    @{NSForegroundColorAttributeName:[UIColor redColor],
                 NSFontAttributeName:textField.font
         }];
    textField.attributedPlaceholder = attrString;

二.控制placeHolder的位置

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

三. 修改文字展示區域,一般跟editingRectForBounds一起重寫

- (CGRect)textRectForBounds:(CGRect)bounds  
{  
    CGRect inset = CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width-25, bounds.size.height);//更好理解些  
    return inset;  
}  

// 重寫來編輯區域,可以改變游標起始位置,以及游標最右到什麼地方,placeHolder的位置也會改變  
-(CGRect)editingRectForBounds:(CGRect)bounds  
{  
    CGRect inset = CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width-25, bounds.size.height);//更好理解些  
    return inset;  
}  

四.UITextField 關於Rect的一些方法介紹

– textRectForBounds:          //重寫來重置文字區域
– drawTextInRect:            //改變繪文字屬性.重寫時呼叫super可以按預設圖形屬性繪製,若自己完全重寫繪製函式,就不用呼叫super了.
– placeholderRectForBounds:  //重寫來重置佔位符區域
– drawPlaceholderInRect:     //重寫改變繪製佔位符屬性.重寫時呼叫super可以按預設圖形屬性繪製,若自己完全重寫繪製函式,就不用呼叫super了
– borderRectForBounds:       //重寫來重置邊緣區域
– editingRectForBounds:      //重寫來重置編輯區域
– clearButtonRectForBounds:  //重寫來重置clearButton位置,改變size可能導致button的圖片失真
– leftViewRectForBounds:      // leftView 的位置
– rightViewRectForBounds:     //  rightView的位置

相關文章