ios - 在 UITextField 中的文本下方添加 "padding"| swift

标签 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。我该怎么做?

最佳答案

这是可设计的文本字段,您可以使用以下命令隐藏此代码: 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

关于ios - 在 UITextField 中的文本下方添加 "padding"| swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32543842/

相关文章:

iphone - 每台服务器的最大连接数

ios - 使用 Swift 3 将 iframe 标签(视频)从 html 字符串加载到 Web View 中

swift - 尝试从 TableView 中的其余数据中分别加载来自 firebase 的图像

ios - Swift:如何使选项卡 View Controller 按钮不显示?

ios - 快速排序 NSArray

ios - 如何从 NSArray 访问属性?

swift - 避免在 DispatchQueue 中使用 self

swift - 我对快速误导性文档的看法是错误的吗?

ios - 在格式字符串之外使用 NSPredicate 的 SELF

ios - Apple 的可达性测试究竟需要什么?