国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOSUITextField限制輸入長度

2019-11-14 18:37:43
字體:
來源:轉載
供稿:網友

這篇博客主要講限制輸入長度的問題,前幾天有人問我這個問題,說限制長度會出現無法刪除問題,于是正好一塊發出來給大家看看。textField的縮進,一張背景圖片搞定的事,我這里用了leftView純屬附帶。

好了廢話少說,貼代碼,很簡單,大家一看便知。

//先創建一個textField 和 一個button。

#import "ViewController.h"@interface ViewController ()<UITextFieldDelegate> {        UITextField *currentTextFeild;    UIButton    *touchButton;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        UITextField *textFields = [[UITextField alloc] initWithFrame:CGRectMake(15, 50, self.view.bounds.size.width-15*2, 40)];    textFields.backgroundColor = [UIColor brownColor];    textFields.layer.cornerRadius = 5;    textFields.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];    textFields.leftViewMode = UITextFieldViewModeAlways;//這兩行是為了不讓Text太貼textField的左邊    textFields.placeholder = @"請輸入手機號";    textFields.delegate = self;    [self.view addSubview:textFields];    currentTextFeild = textFields;        UIButton *enableButton = [UIButton buttonWithType:UIButtonTypeCustom];    enableButton.frame = CGRectMake(15, 100, self.view.bounds.size.width-15*2, 40);    enableButton.layer.cornerRadius = 5;    enableButton.backgroundColor = [UIColor grayColor];    [enableButton setTitle:@"沒內容不可點擊" forState:UIControlStateNormal];    [enableButton setTitle:@"可以按了" forState:UIControlStateSelected];    [enableButton setTitle:@"按下去了" forState:UIControlStateHighlighted];    enableButton.enabled = NO;    [enableButton addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:enableButton];    touchButton = enableButton;}- (void)btnClick {        }

//設置textField代理 

#PRagma mark -  UITextFieldDelegate- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {        return YES;}- (void)textFieldDidEndEditing:(UITextField *)textField {        }- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {        //用來判斷是否可以繼續輸入, - range.length是為了判斷是否可以刪除    NSInteger currentLength = textField.text.length - range.length + string.length;    if (currentLength > 11) {                return NO;    }        //判斷按鈕是否可以enable = YES    if (currentTextFeild.text && currentTextFeild.text.length > 0 && currentLength > 0) {                touchButton.enabled = YES;        touchButton.selected = YES;    }else {                touchButton.enabled = NO;        touchButton.selected = NO;    }        if (currentLength <= 0) {                touchButton.enabled = NO;        touchButton.selected = NO;    }        return YES;}- (BOOL)textFieldShouldClear:(UITextField *)textField {        if (currentTextFeild.tag == 11 || currentTextFeild.tag == 12) {        //手機號        touchButton.enabled = NO;        touchButton.selected = NO;;    }        return YES;}- (BOOL)textFieldShouldReturn:(UITextField *)textField {        [textField resignFirstResponder];        return YES;}

 大家看了代碼,差不多就明白了。

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 光山县| 绵阳市| 台北县| 寻乌县| 红河县| 兰考县| 凤冈县| 渝北区| 哈巴河县| 崇左市| 河北省| 鄂州市| 广汉市| 永善县| 江阴市| 镶黄旗| 攀枝花市| 北票市| 兴海县| 波密县| 兴安县| 东乌珠穆沁旗| 成安县| 汕头市| 金平| 蚌埠市| 张家口市| 陇西县| 乳源| 寿阳县| 南通市| 靖安县| 资兴市| 安乡县| 宝丰县| 大兴区| 乐平市| 蒲江县| 潼关县| 黔西县| 平南县|