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;

    }

   

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM