前言
在開發(fā)中經(jīng)常遇到各種需要輸入手機號的地方肌厨,而每遇到一個地方就要專門寫一套代碼來驗證手機號輸入的位數(shù)及開始幾位是否正確培慌,有時為了顯示的好看明顯,還需要把手機號進行3-4-4這種方式顯示柑爸,現(xiàn)在特寫了LHPhoneTextFiled來支持這種特定的需求吵护,非常感謝phoneTextField提供非常好的思路
1、 使用LHPHoneTextField非常簡單表鳍,支持設(shè)置3-4-4中間的這個分隔符lh_seperatorText
馅而,設(shè)置不標準手機字符串自動標準顯示
2、而用lh_text來設(shè)置默認顯示的手機號
3譬圣、而用lh_text來獲取的手機號時標準的全數(shù)字手機號瓮恭,不需要進行任何判斷,使用非常簡單厘熟,效果如下圖:
4屯蹦、提供一個方法判斷手機號是否是標準手機號lh_IsValidatePhone
5、提供方法直接撥打電話lh_callCurrentPhone
LHPhoneTextField.gif
使用方式
因為本身是繼承自UITextField,所以使用起來是一樣的绳姨,唯一的區(qū)別是使用lh_seperatorText
來設(shè)置3-4-4的分隔符登澜,默認分隔符是-
,
要設(shè)置默認的手機號時使用lh_text
設(shè)置要顯示的手機號,而要獲取完整的全數(shù)字的手機號也需要使用lh_text
獲取就缆,具體使用如下:
@interface LHPhoneTextFieldSampleViewController ()
@property (nonatomic,strong) LHPhoneTextField * phoneTextField2;
@end
@implementation LHPhoneTextFieldSampleViewController
- (void)viewDidLoad {
self.phoneTextField2 = [[LHPhoneTextField alloc] initWithFrame:CGRectMake(90, 175, 200, 30)];
self.phoneTextField2.layer.masksToBounds = YES;
self.phoneTextField2.layer.borderColor = [UIColor redColor].CGColor;
self.phoneTextField2.layer.borderWidth = 1;
self.phoneTextField2.clearButtonMode = UITextFieldViewModeWhileEditing;
self.phoneTextField2.placeholder = @"請輸入手機號";
self.phoneTextField2.lh_seperatorText = @" ";
[self.view addSubview:self.phoneTextField2];
// 值發(fā)現(xiàn)變化時調(diào)用
[self.phoneTextField2 addTarget:self action:@selector(textFild_textValueChange2) forControlEvents:UIControlEventValueChanged];
// 只有注冊了這個通知帖渠,valueChange才會改變
//UITextFieldTextDidChangeNotification通知事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFild_textValueChange2) name:UITextFieldTextDidChangeNotification object:nil];
// 默認手機號,不限制格式
self.phoneTextField2.lh_text = @"131-12339323";
}
@end
具體demo:LH.LoveKit
知識點
1竭宰、對代理進行攔截二次觸發(fā)
2空郊、使用APIsendActionsForControlEvents
主動觸發(fā)某個事件的所有target-action
3份招、獲取UITextField的光標及對光標的移動