我目前正在尝试添加"填充"在UITextField
的文本(包括占位符文本)下方,没有任何边框。我环顾四周,我能找到的只有:
// adjust place holder text
let paddingView = UIView(frame: CGRectMake(0, 0, 10, usernameOrEmailField.frame.height))
usernameOrEmailField.leftView = paddingView
usernameOrEmailField.leftViewMode = UITextFieldViewMode.Always
虽然这会调整UITextField
中文本的位置,但它不会将其添加到文本的底部。在尝试解决此问题时,我发现UITextField
具有.leftView
和.rightView
属性,但没有.topView
或.bottomView
。我该怎么做?
答案 0 :(得分:0)
这是可设计的文本字段,您可以将此代码转换为:http://objectivec2swift.net/
LDTextField.h
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface LDTextField : UITextField
@property (nonatomic) IBInspectable CGFloat bottomBorderHeight;
@property (nonatomic) IBInspectable CGFloat leftViewWidth;
@property (nonatomic) IBInspectable UIColor *bottomBorderColor;
@property (nonatomic) IBInspectable UIColor *placeHolderColor;
@property (nonatomic) IBInspectable UIImage *leftImage;
@property (nonatomic,retain) UIImageView *imageview;
@property (nonatomic,retain) UIView *bottomview;
@end
LDTextField.m
#import "LDTextField.h"
@implementation LDTextField
-(instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
-(instancetype)initWithCoder:(nonnull NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(void)drawRect:(CGRect)rect{
if (self.bottomview) {
self.bottomview.frame = CGRectOffset(rect, 0, rect.size.height - self.bottomBorderHeight);
}
}
-(void)setLeftImage:(UIImage *)leftImage{
_leftImage = leftImage;
if (!self.imageview && leftImage.scale > 0) {
self.imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.leftViewWidth, self.frame.size.height)];
}
if (leftImage) {
self.imageview.image = leftImage;
self.imageview.contentMode = UIViewContentModeCenter;
self.leftViewMode = UITextFieldViewModeAlways;
self.leftView = self.imageview;
}
else {
self.imageview.image = leftImage;
self.leftViewMode = UITextFieldViewModeAlways;
self.leftView = self.imageview;
}
}
-(void)setBottomBorderColor:(UIColor *)bottomBorderColor{
_bottomBorderColor = bottomBorderColor;
if (!self.bottomview) {
self.bottomview = [[UIView alloc] initWithFrame:CGRectOffset(self.frame, 0, self.frame.size.height - self.bottomBorderHeight)];
[self addSubview:self.bottomview];
}
self.bottomview.backgroundColor = bottomBorderColor;
}
-(void)setBottomBorderHeight:(CGFloat)bottomBorderHeight
{
_bottomBorderHeight = bottomBorderHeight;
if (!self.bottomview) {
self.bottomview = [[UIView alloc] initWithFrame:CGRectOffset(self.frame, 0, self.frame.size.height - self.bottomBorderHeight)];
[self addSubview:self.bottomview];
}
else{
self.bottomview.frame = CGRectOffset(self.frame, 0, self.frame.size.height - self.bottomBorderHeight);
}
}
-(void)setLeftViewWidth:(CGFloat)leftViewWidth{
_leftViewWidth = leftViewWidth;
if (self.imageview) {
self.imageview.frame = CGRectMake(0, 0, leftViewWidth, self.frame.size.height);
}
}
-(void)setPlaceHolderColor:(UIColor *)placeHolderColor{
_placeHolderColor = placeHolderColor;
if ([self respondsToSelector:@selector(attributedPlaceholder)]) {
@try {
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName: placeHolderColor}];
}
@catch (NSException *exception) {
}
}
}
@end