5

I'm trying to use a custom textfield background. The problem is that the text then appears to be too close on the left.

I don't see any way to shift the text without subclassing UITextField. So I'm attempting to extend and overwrite

- (void)drawTextInRect:(CGRect)rect{
    NSLog(@"draw rect");
    CGRect newRect = CGRectMake(rect.origin.x+20,rect.origin.y,rect.size.width-20,rect.size.height);    
    [super drawTextInRect:newRect]; 
}

But for some reason the log never prints. I know the subclass is being used since I also have a log in the init, and that prints fine.

Whast goig on

EDIT.

I also try

- (CGRect)textRectForBounds:(CGRect)bounds{
    NSLog(@"bounds");
    CGRect b = [super textRectForBounds:bounds];
    b.origin.x += 20;
    return b;
}

That actually traces, but it doesn't seem to be shifting

skaffman
  • 398,947
  • 96
  • 818
  • 769
dizy
  • 7,951
  • 10
  • 53
  • 54

5 Answers5

14

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];
Thomas Müller
  • 15,565
  • 6
  • 41
  • 47
  • 1
    that doesn't solve why what I was doing wasn't working, but sure solves my problem. Thanks! I actually tried the same, but left out the viewModeAlways, thus it didn't work. – dizy Dec 08 '09 at 04:23
7

Another Soultion:

In your appDelegate.m file write the following code at the top

@interface UITextField (myCustomTextField)

@end

@implementation UITextField (myCustomTextField)

- (CGRect)textRectForBounds:(CGRect)bounds {

    return CGRectInset(bounds, 10, 0);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 10, 0);
}

@end
August Lilleaas
  • 54,010
  • 13
  • 102
  • 111
Jimit
  • 652
  • 8
  • 12
5

Just FYI, but I had the same problem and finally realized that there's also a editingRectForBounds: method. The control uses that one when you're initially typing stuff into the field. If it has a value and is no longer in editing mode, it then uses textRectForBounds: to decide where to draw the text.

Sean
  • 1,272
  • 1
  • 13
  • 27
5

I've not tried Thomas's method, but just to clarify Sean's answer, this has worked for me:

MyInsetUITextField.m

#import "MyInsetUITextField.h"
@implementation MyInsetUITextField

- (CGRect)textRectForBounds:(CGRect)bounds {

    return CGRectInset(bounds, 10, 0);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 10, 0);
}

@end

MyInsetUITextField.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface MyInsetUITextField : UITextField {

}

@end

Thanks.

gef
  • 7,025
  • 4
  • 41
  • 48
-1

Just a heads up, there is a LAZY EASY WAY for those of us who use the Xcode interface builder:

  • easier: put a UIImageView behind a text field

  • easiest: change the border style on your UITextView to any style except the far right "graphical circle", then add an image as a background. The image takes precedence over the graphic, but you still get the padding needed for a normal image background.

woody121
  • 358
  • 3
  • 14