UITextField文本字段控件的位置

 

若是須要更改默認的UITextField清除按鈕、左右視圖等等控件的位置,能夠經過以下設置:字體

// 控制清除按鈕的位置 (默認 width = 15 = height)
-(CGRect)clearButtonRectForBounds:(CGRect)bounds {
//    NSLog(@"=%f ,=%f",bounds.origin.y,bounds.size.height/2);
    return CGRectMake(bounds.origin.x + bounds.size.width - 50, (bounds.size.height - 15)/2, 15, 15);
}

// 控制placeHolder的位置 默認(0,0,width,height)
-(CGRect)placeholderRectForBounds:(CGRect)bounds {
    
//    return CGRectInset(bounds, 20, 0);
    CGRect inset = CGRectMake(bounds.origin.x + 50, bounds.origin.y, bounds.size.width - 10, bounds.size.height);
    return inset;
}

// 控制顯示文本的位置  默認(0,0,width,height)
-(CGRect)textRectForBounds:(CGRect)bounds {
    
    NSLog(@"顯示文本==%@",NSStringFromCGRect(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 {
    
    NSLog(@"編輯文本==%@",NSStringFromCGRect(bounds));
    //return CGRectInset( bounds, 10 , 0 );
    CGRect inset = CGRectMake(bounds.origin.x + 100, bounds.origin.y, bounds.size.width -10, bounds.size.height);
    return inset;
}

// 控制左視圖位置 (默認 width=30)
- (CGRect)leftViewRectForBounds:(CGRect)bounds {
    
    NSLog(@"左視圖位置==%@",NSStringFromCGRect(bounds));
    
    CGRect inset = CGRectMake(bounds.origin.x + 2, bounds.origin.y, 30, bounds.size.height);
    return inset;
    //return CGRectInset(bounds,50,0);
}

// 控制placeHolder的顏色、字體
- (void)drawPlaceholderInRect:(CGRect)rect {
    
    [[self placeholder] drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0f],
                                                         NSForegroundColorAttributeName:[UIColor darkGrayColor]
                                                         }];
    
}


// 右視圖的位置 (默認整個 width)
- (CGRect)rightViewRectForBounds:(CGRect)bounds {
    
    NSLog(@"右視圖位置==%@",NSStringFromCGRect(bounds));
    
    CGRect inset = CGRectMake(bounds.size.width - 30, bounds.origin.y, 30, bounds.size.height);
    return inset;
}
相關文章
相關標籤/搜索