80 votes

Indenter le texte dans un objet UITextField

Ma question est aussi simple que le titre, mais voici un peu plus:

J'ai un UITextField, sur lequel j'ai mis l'image d'arrière-plan. Le problème est que le texte de câlins de si près à son bord, qui est aussi le bord de l'image. Je veux que mon champ de texte pour ressembler à Apple, avec un peu d'espace horizontal entre l'image de fond et où le texte commence.

235voto

adjwilli Points 4477

C'est le moyen le plus rapide que j'ai trouvé sans faire toutes les sous-classes:

UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[textfield setLeftViewMode:UITextFieldViewModeAlways];
[textfield setLeftView:spacerView];

41voto

Jano Points 37593

Vous avez à la sous-classe et remplacer textRectForBounds: et editingRectForBounds:. Ici est une sous-classe UITextfield avec un arrière-plan personnalisé et verticale et horizontale padding:

@interface MyUITextField : UITextField 
@property (nonatomic, assign) float verticalPadding;
@property (nonatomic, assign) float horizontalPadding;
@end

#import "MyUITextField.h"
@implementation MyUITextField
@synthesize horizontalPadding, verticalPadding;
- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectMake(bounds.origin.x + horizontalPadding, bounds.origin.y + verticalPadding, bounds.size.width - horizontalPadding*2, bounds.size.height - verticalPadding*2);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self textRectForBounds:bounds];
}
@end

Utilisation:

UIImage *bg = [UIImage imageNamed:@"textfield.png"];
CGRect rect = CGRectMake(0, 0, bg.size.width, bg.size.height);
MyUITextField *textfield = [[[MyUITextField alloc] initWithFrame:rect] autorelease];
textfield.verticalPadding = 10; 
textfield.horizontalPadding = 10;
[textfield setBackground:bg];
[textfield setBorderStyle:UITextBorderStyleNone];
[self.view addSubview:textfield];

27voto

Brody Robertson Points 3197

Une bonne approche pour ajouter rembourrage pour objet UITextField est à la sous-classe UITextField, l'ajout d'un edgeInsets de la propriété. Vous définissez ensuite la edgeInsets et l'objet UITextField sera établi en conséquence. Ce sera aussi fonctionner correctement avec un custom leftView ou rightView ensemble.

OSTextField.h

#import <UIKit/UIKit.h>

@interface OSTextField : UITextField

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end

OSTextField.m

#import "OSTextField.h"

@implementation OSTextField

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self){
        self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return self;
}

- (CGRect)textRectForBounds:(CGRect)bounds {
    return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

@end

1voto

user790072 Points 17

Je suggère la conversion de votre UITextField d'un UITextView et réglage de l' contentInset. Par exemple pour le retrait de 10 espaces:

textview.contentInset=UIEdgeInsetsMake(0, 10, 0, 0);

0voto

Lirik Points 531

Parfois, le leftView de la zone de texte est un Apple loupe de l'image. Si oui, vous pouvez définir un tiret comme ceci:

    UIView *leftView = textField.leftView;
    if (leftView && [leftView isKindOfClass:[UIImageView class]]) {
        leftView.contentMode = UIViewContentModeCenter;
        leftView.width = 20.0f;  //the space you want indented.
    }

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X