2015-09-12 30 views
0

我正在嘗試在沒有任何邊框的UITextField的文本(包括佔位符文本)下面添加「填充」。我環顧四周,所有我能找到的:在UITextField |中的文本下方添加「填充」 Swift

// 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

http://stackoverflow.com的可能的複製/ questions/25367502 /我如何創建空間開始的uitextfield-swift#answer-27066764 – mikle94

+0

我還沒有看到那篇文章 - 完美的工作! @ mikle94 – Ethan

+0

不客氣) – mikle94

回答

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 
相關問題