UITextField Custom Appearance and Text Shift

I am trying to use my own text box background. The problem is that the text seems too close on the left.

I see no way to shift text without subclassing UITextField. Therefore, I am trying to expand and rewrite

- (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 magazine never prints. I know that a subclass is being used, since I also have a log in init, and this prints fine.

Extremely dangerous

EDIT.

I also try

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

It really tracks, but it doesn't seem to be biasing

+5
source share
5 answers

I think you could use a property leftViewfor this.

leftView rightView UIText. , , , .

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

:

appDelegate.m

@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
+7

FYI, , , , editRectForBounds:. , . , textRectForBounds: , .

+5

, :

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.

+5
source

Just a head, there is a LASEY EASY WAY for those of us who use the Xcode interface builder:

  • simpler: put a UIImageView behind a text box

  • the easiest way: change the border style on your UITextView to any style other than the rightmost “graphic circle”, and then add the image as a background. The image takes precedence over the graphics, but you still get the indentation necessary for a normal image.

-1
source

All Articles