Отступ текста в UITextField
Мой вопрос так же прост, как и название, но здесь немного больше:
У меня есть UITextField, на котором я установил фоновое изображение. Проблема в том, что текст так близко обнимает его, что также является краем изображения. Я хочу, чтобы мое текстовое поле выглядело как Apple, с небольшим горизонтальным пространством между фоновым изображением и началом текста.
Ответы
Ответ 1
Это самый быстрый способ, который я нашел без каких-либо подклассов:
UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[textfield setLeftViewMode:UITextFieldViewModeAlways];
[textfield setLeftView:spacerView];
В Swift:
let spacerView = UIView(frame:CGRect(x:0, y:0, width:10, height:10))
textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = spacerView
Ответ 2
Вы должны подклассифицировать и переопределить textRectForBounds: и editRectForBounds:. Вот подкласс UITextfield с пользовательским фоном и вертикальным и горизонтальным заполнением:
@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
Использование:
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];
Ответ 3
Хороший подход для добавления дополнения к UITextField заключается в подклассе UITextField, переопределении методов прямоугольника и добавлении свойства edgeInsets. Затем вы можете установить edgeInsets, и UITextField будет нарисован соответствующим образом. Это также будет корректно работать с пользовательским набором leftView или rightView.
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
Ответ 4
Я превратил это в красивое небольшое расширение для использования внутри раскадровки:
public extension UITextField {
@IBInspectable public var leftSpacer:CGFloat {
get {
if let l = leftView {
return l.frame.size.width
} else {
return 0
}
} set {
leftViewMode = .Always
leftView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
}
}
}
Ответ 5
Я предлагаю преобразовать ваш UITextField
в UITextView
и установить contentInset
. Например, для отступов на 10 пробелов:
textview.contentInset=UIEdgeInsetsMake(0, 10, 0, 0);
Ответ 6
Мои 2 цента:
class PaddedTextField : UITextField {
var insets = UIEdgeInsets.zero
var verticalPadding:CGFloat = 0
var horizontalPadding:CGFloat = 0
override func textRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + insets.left, y: bounds.origin.y + insets.top, width: bounds.size.width - (insets.left + insets.right), height: bounds.size.height - (insets.top + insets.bottom));
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return textRect(forBounds: bounds)
}
}
Ответ 7
Иногда leftView текстового поля является изображением увеличительного стекла Apple.
Если это так, вы можете установить отступ следующим образом:
UIView *leftView = textField.leftView;
if (leftView && [leftView isKindOfClass:[UIImageView class]]) {
leftView.contentMode = UIViewContentModeCenter;
leftView.width = 20.0f; //the space you want indented.
}
Ответ 8
Если вы не хотите создавать @IBOutlet, может просто подкласс (ответ в Swift):
class PaddedTextField: UITextField {
override func awakeFromNib() {
super.awakeFromNib()
let spacerView = UIView(frame:CGRect(x: 0, y: 0, width: 10, height: 10))
leftViewMode = .always
leftView = spacerView
}
}