0

I'm currently trying to add "padding" below the text (including placeholder text) of a UITextField without any borders. I've looked around and all I could find was:

// adjust place holder text
let paddingView = UIView(frame: CGRectMake(0, 0, 10, usernameOrEmailField.frame.height))
usernameOrEmailField.leftView = paddingView
usernameOrEmailField.leftViewMode = UITextFieldViewMode.Always

While this does adjust the placement of the text in the UITextField, it does not add it to the bottom of the text. While trying to figure this out, I found that UITextField has the properties .leftView and .rightView, but no .topView or .bottomView. How should I do this?

Ethan
  • 3
  • 3
  • Possible duplicate of http://stackoverflow.com/questions/25367502/how-do-i-create-space-at-beginning-of-uitextfield-swift#answer-27066764 – mkz Sep 12 '15 at 21:29
  • I hadn't seen that post - that worked perfectly! @mikle94 – Ethan Sep 12 '15 at 22:03

1 Answers1

0

This is designable textfield and you can covert this code with : 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
Lokesh Dudhat
  • 449
  • 3
  • 10