textField常用的属性

 UITextField *textF = [[UITextField alloc]initWithFrame:CGRectMake(0, kFitH(100), kScreenWidth, kFitH(60))];

//设置代理

textF.delegate = self;

 //背景   

textF.backgroundColor = [UIColor whiteColor];

textF.placeholder = @" 输入手机号码";

    

    typedef enum {

        

        UIKeyboardTypeDefault,       默认键盘,支持所有字符

        

        UIKeyboardTypeASCIICapable,  支持ASCII的默认键盘

        

        UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符

        

        UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符

        

        UIKeyboardTypeNumberPad,              数字键盘

        

        UIKeyboardTypePhonePad,     电话键盘

        

        UIKeyboardTypeNamePhonePad,   电话键盘,也支持输入人名

        

        UIKeyboardTypeEmailAddress,   用于输入电子 邮件地址的键盘

        

        UIKeyboardTypeDecimalPad,     数字键盘 有数字和小数点

        

        UIKeyboardTypeTwitter,        优化的键盘,方便输入@、#字符

        

        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, 

        

    } UIKeyboardType;

 

[self.textF setKeyboardType:UIKeyboardTypeNumberPad];

但是会报错,目前没有解决办法,哪位大神会解决麻烦告诉小的

报错信息:Can't find keyplane that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad; using 1336863583_PortraitChoco_iPhone-Simple-Pad_Default

//添加clearButton

 textF.clearButtonMode = UITextFieldViewModeWhileEditing;

//设置为加边框的模式,但是鄙人都是使用layer加borderd的方法,例如:

fieldF.layer.cornerRadius = 5;

    

fieldF.layer.masksToBounds = YES;

    

fieldF.layer.borderColor = [UIColor redColor].CGColor;

    

fieldF.layer.borderWidth = 2;

fieldF.borderStyle = UITextBorderStyleRoundedRect;

//设置输入为安全模式

fieldF.secureTextEntry = YES;

 //对齐方式,目前没有什么用

 textF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

//设置placeholder的字体,必须和text字体一致,否则不能对齐

[textF setValue:[UIFont fontWithName:kFontPingFangMedium size:kFitFontSize(32)] forKeyPath:@"_placeholderLabel.font"];

//设置文字的字体   

textF.font = [UIFont fontWithName:kFontPingFangMedium size:kFitFontSize(32)];

//设置光标的颜色    

textF.tintColor = [UIColor grayColor];

//设置光标左移    

UIView *leftVie = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];    

textF.leftView = leftVie;

//设置左边view的显示方式

 textF.leftViewMode = UITextFieldViewModeAlways;

// 监听电话号码的改变

    [textF addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];

 

placeholder的颜色不能直接进行设置,我们可以利用分类的方式进行添加属性

分类.h

 

#import <UIKit/UIKit.h>

 

@interface UITextField (Placeholder)

 

@property (nonatomic, strong)UIColor* placeholderColor;

@end

 分类.m

#import "UITextField+Placeholder.h"

#import <objc/message.h>

@implementation UITextField (Placeholder)

 

 

-(void)setPlaceholderColor:(UIColor *)placeholderColor{

    

    objc_setAssociatedObject(self, @"placeholderColor", placeholderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    UILabel *placeLabel = [self valueForKey:@"placeholderLabel"];

    placeLabel.textColor = placeholderColor;

    

}

 

- (UIColor *)placeholderColor{

    

  return objc_getAssociatedObject(self, @"placeholderColor");

}

在需要设置颜色的类中导进

#import "UITextField+Placeholder.h"

 就可以设置颜色了

 fieldF.placeholderColor = [UIColor redColor];

//设置与之相关的代理方法

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    //成为第一响应者

        [textField becomeFirstResponder];

}

 //判断手机号是不是11位并截取到11位

- (void)textChanged:(UITextField *)field {

 

    if (field == self.textF) {

        if (field.text.length<11) {

            

            self.navigationItem.rightBarButtonItem.enabled = NO;

        }else{

            field.text = [field.text substringToIndex:11];

            self.navigationItem.rightBarButtonItem.enabled = YES;

            [field resignFirstResponder];          

        }

    }    

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [self.textF resignFirstResponder];

}

 

-(BOOL)textFieldShouldClear:(UITextField *)textField{

    return YES;

}

 一下是textField经常用到的相关方法

//只允许输入数字,且只有11位数

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if (textField.text.length == 11) {

        if ([string isEqualToString:@""]) {

            return YES;

        }

        else {

            return NO;

        }

    }

    return [self validateNumber:string];

}

//只允许输入数字

- (BOOL)validateNumber:(NSString*)number{

    BOOL res = YES;

    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

    int i = 0;

    while (i < number.length) {

        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];

        NSRange range = [string rangeOfCharacterFromSet:tmpSet];

        if (range.length == 0) {

            res = NO;

            break;

        }

        i++;

    }

    return res;

}

 

 

 

//检查是否为手机号的方法

-(BOOL)checkPhoneNumInput:(NSString *)phoneStr

{

    NSString *photoRange = @"^1(3[0-9]|4[0-9]|5[0-9]|7[0-9]|8[0-9])\\d{8}$";//正则表达式

    NSPredicate *regexMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",photoRange];

    BOOL result = [regexMobile evaluateWithObject:phoneStr];

    if (result) {

        return YES;

    } else {

        return NO;

    }

   

}

 

posted on 2016-09-19 08:02  敏言慎行  阅读(2597)  评论(0编辑  收藏  举报

导航