3

I want to be align Left margin of UITextField.text to 10Px. please suggest me best way ?? same in roundedRect TesxtField where text start 10 px from left

have reached almost by overriding - (CGRect)textRectForBounds:(CGRect)bounds. now issue is when TextField goes in to edit mode their left margin reset to Zero .......

@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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
iOSPawan
  • 2,884
  • 2
  • 25
  • 50

4 Answers4

4

Can you try UITextField's instance method drawTextInRect:?

I think you could use the leftView property for this.

You can add a leftView and rightView to a UITextfield. These views can be used to display an icon, but if it's an empty view it'll just take up space, which is what you want.

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];

Refer SO question UITextField custom background view and shifting text

Community
  • 1
  • 1
visakh7
  • 26,380
  • 8
  • 55
  • 69
  • do you have code for it . i guess in this i need to handle CGContextRef and i can set font ,margin everything here . would be good if you share the code please..... – iOSPawan Apr 18 '11 at 07:42
  • You can find some sample code here http://stackoverflow.com/questions/1274168/drop-shadow-on-uitextfield-text – visakh7 Apr 18 '11 at 08:09
  • Another SO question http://stackoverflow.com/questions/1754605/how-to-change-the-color-of-first-letter-of-a-textfields-text – visakh7 Apr 18 '11 at 08:10
3

Have you tried

-(CGRect)editingRectForBounds(CGRect)bounds;
Jorge
  • 2,056
  • 1
  • 16
  • 23
2

Override the super's implementation, and tune the results. This way you don't need to calculate with leftView and rightView (if you use them).

- (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];
}
Stoto
  • 410
  • 4
  • 10
0

swift:

    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;
wan
  • 1
  • 1