Reputation: 826
I have a custom background for an UITextField
. But the first 20 pixels of the UITextField
are taken by the design. The text should start 30 pixels from the left. I have searched quite a bit, but I couldn't find it. I have seen things like overriding properties, but is that really necessary for something this basic?
Upvotes: 1
Views: 1172
Reputation: 1067
I've seen two different solutions:
UIView
as a leftView
property of UITextView
textRectForBounds:
editingRectForBounds:
- just return frame with insets appliedIMHO the second one is more elegant.
Example: .h:
#import <UIKit/UIKit.h>
@interface BlackTextField : UITextField
@property (nonatomic, assign) CGFloat paddingRight, paddingTop, paddingLeft, paddingBottom;
@end
.m:
#import "BlackTextField.h"
#import <QuartzCore/QuartzCore.h>
@implementation BlackTextField
@synthesize paddingTop, paddingBottom, paddingLeft, paddingRight;
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
self.paddingRight = 10;
self.paddingLeft = 10;
self.paddingTop = 5;
self.paddingBottom = 5;
}
return self;
}
- (CGRect)textRectForBounds:(CGRect)bounds
{
return CGRectMake(bounds.origin.x + self.paddingLeft, bounds.origin.y + self.paddingTop, bounds.size.width - self.paddingLeft - self.paddingRight, bounds.size.height - self.paddingTop - self.paddingBottom);
}
- (CGRect)editingRectForBounds:(CGRect)bounds
{
return [self textRectForBounds:bounds];
}
@end
Upvotes: 3
Reputation: 12333
the simplest way to do is, create a UIImageView
and put UITextField
on it.
problem solved.
FYI: you could subclassing UITextField
, but that would be huge and complex job to do. as i would not recommended as not standart view and could have many pitfall in current and future.
Upvotes: -1