添加"填充"在UITextField中的文本下面迅速

时间:2015-09-12 21:20:28

标签: ios swift

我目前正在尝试添加"填充"在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。我该怎么做?

1 个答案:

答案 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