Dyl*_*o42 76 iphone cocoa-touch objective-c uitextfield ios
我的问题就像标题一样简单,但这里有一点:
我有一个UITextField,我在其上设置了背景图像.问题是文本紧贴着它的边缘,这也是图像的边缘.我希望我的文本字段看起来像Apple的,在背景图像和文本开始的位置之间有一些水平空间.
adj*_*lli 232
这是我在没有做任何子类的情况下发现的最快的方法:
UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[textfield setLeftViewMode:UITextFieldViewModeAlways];
[textfield setLeftView:spacerView];
Run Code Online (Sandbox Code Playgroud)
在Swift中:
let spacerView = UIView(frame:CGRect(x:0, y:0, width:10, height:10))
textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = spacerView
Run Code Online (Sandbox Code Playgroud)
Jan*_*ano 42
您必须子类化并覆盖textRectForBounds:和editingRectForBounds:.这是一个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
Run Code Online (Sandbox Code Playgroud)
用法:
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];
Run Code Online (Sandbox Code Playgroud)
Bro*_*son 26
向UITextField添加填充的一个好方法是继承UITextField,重写矩形方法并添加edgeInsets属性.然后,您可以设置edgeInsets,并相应地绘制UITextField.这也可以使用自定义leftView或rightView集正常运行.
OSTextField.h
#import <UIKit/UIKit.h>
@interface OSTextField : UITextField
@property (nonatomic, assign) UIEdgeInsets edgeInsets;
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Oxc*_*cug 12
我把它变成了一个很好的小扩展,可以在Storyboard中使用:
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))
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50519 次 |
| 最近记录: |