2011-04-18 54 views
3

我想將UITextField.text的左邊距對齊到10Px。請建議我最好的辦法?在圓角矩形TesxtField相同,其中的文字從左至右重置UITextField左邊距

啓動10 PX已通過覆蓋幾乎達到 - (CGRect)textRectForBounds:(CGRect)bounds.現在的問題是,當文本字段在進入到編輯模式的左邊距重置爲零.......

@implementation UITextField(UITextFieldCatagory) 

- (CGRect)textRectForBounds:(CGRect)bounds { 
    CGRect theRect=CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width-10, bounds.size.height); 
    return theRect; 
} 

回答

3

你試過

-(CGRect)editingRectForBounds(CGRect)bounds; 
+0

它的工作就像魅力.thanks豪爾赫 – iOSPawan 2011-04-18 07:37:07

4

你可以嘗試的UITextField的實例方法drawTextInRect:

我認爲你可以使用leftView這個屬性。

您可以將leftViewrightView添加到UITextfield。這些視圖可以用來顯示一個圖標,但如果它是一個空視圖,它將佔用空間,這是你想要的。

CGFloat leftInset = 5.0f; 
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, leftInset, self.bounds.size.height)]; 
self.leftView = leftView; 
self.leftViewMode = UITextFieldViewModeAlways; 
[leftView release]; 

是指SO質疑UITextField custom background view and shifting text

+0

你有代碼它。我想在這個我需要處理CGContextRef,我可以設置字體,邊緣一切在這裏。會很好,如果你分享代碼請..... – iOSPawan 2011-04-18 07:42:07

+0

你可以在這裏找到一些示例代碼http://stackoverflow.com/questions/1274168/drop-shadow-on-uitextfield-text – visakh7 2011-04-18 08:09:18

+0

另一個SO問題http:///stackoverflow.com/questions/1754605/how-to-change-the-color-of-first-letter-of-a-textfields-text – visakh7 2011-04-18 08:10:32

2

覆蓋超的實施和調整的結果。這樣你就不需要用leftView和rightView來計算(如果你使用它們的話)。

- (CGRect)textRectForBounds:(CGRect)bounds { 
    CGRect ret = [super textRectForBounds:bounds]; 
    ret.origin.x = ret.origin.x + 10; 
    ret.size.width = ret.size.width - 20; 
    return ret; 
} 

- (CGRect)editingRectForBounds:(CGRect)bounds { 
    return [self textRectForBounds:bounds]; 
} 
0

迅速:

let leftView:UIView = UIView(frame: CGRectMake(0, 0, 30, 1)) 
    leftView.backgroundColor = UIColor.clearColor() 
    my_text_field.leftView = leftView; 
    my_text_field.leftViewMode = UITextFieldViewMode.Always;