閱讀547 返回首頁    go 阿裏雲 go 技術社區[雲棲]


定製textField

//
//  HYBTextField.h
//  CloudShopping
//
//  Created by sixiaobo on 14-7-10.
//  Copyright (c) 2014年 com.Uni2uni. All rights reserved.
//

#import <UIKit/UIKit.h>

/*!
 * @brief 自定義TextField,用於修改默認textfield的屬性為我們工程中需要的屬性
 * @author huangyibiao
 */
@interface HYBTextField : UITextField

@property (nonatomic, strong) UIColor *placeholderColor;
@property (nonatomic, strong) UIFont  *placeholderFont;
@property (nonatomic, assign) CGFloat leftPadding;

// 默認leftPadding = 8.0
- (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font;
- (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font leftPadding:(CGFloat)leftPadding;

@end


//
//  HYBTextField.m
//  CloudShopping
//
//  Created by sixiaobo on 14-7-10.
//  Copyright (c) 2014年 com.Uni2uni. All rights reserved.
//

#import "HYBTextField.h"

@implementation HYBTextField

- (id)initWithFrame:(CGRect)frame placeholderColor:(UIColor *)color font:(UIFont *)font {
    return [self initWithFrame:frame placeholderColor:color font:font leftPadding:8];
}

- (id)initWithFrame:(CGRect)frame
   placeholderColor:(UIColor *)color
               font:(UIFont *)font
        leftPadding:(CGFloat)leftPadding {
    if (self = [super initWithFrame:frame]) {
        self.placeholderColor = color;
        self.placeholderFont = font;
        self.leftPadding = leftPadding;
        
        self.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.autocorrectionType = UITextAutocorrectionTypeNo;
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        self.borderStyle = UITextBorderStyleNone;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)drawPlaceholderInRect:(CGRect)rect {
    [kColorWith16RGB(0xa8a8a8) setFill];
    [[self placeholder] drawInRect:CGRectMake(self.leftPadding, rect.origin.y, rect.size.width, rect.size.height)
                          withFont:self.placeholderFont];
    return;
}

// 控製編輯文本的位置
- (CGRect)editingRectForBounds:(CGRect)bounds {
    CGFloat padding = self.leftPadding;
    if (self.textAlignment == NSTextAlignmentRight) {
        padding = 0;
    }
    CGRect inset = CGRectMake(bounds.origin.x + padding, bounds.origin.y,
                              bounds.size.width, bounds.size.height);
    return inset;
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    NSString *obtainSizeString = self.text;
    CGSize size = [obtainSizeString sizeWithFont:self.placeholderFont];
    return CGRectMake(bounds.origin.x, (bounds.size.height - size.height) / 2,
                      bounds.size.width, bounds.size.height);
}

// 控製顯示文本的位置
- (CGRect)textRectForBounds:(CGRect)bounds {
    CGFloat padding = self.leftPadding;
    if (self.textAlignment == NSTextAlignmentRight) {
        padding = 0;
    }
    CGRect inset = CGRectMake(bounds.origin.x + padding, bounds.origin.y,
                              bounds.size.width, bounds.size.height);
    
    return inset;
}

@end


最後更新:2017-04-03 05:39:42

  上一篇:go 當IT外包遇到暴力破解
  下一篇:go NSFileManager擴展