UITextField-郵箱尾碼聯想匹配,uitextfield-尾碼
最近做項目,有一個功能,百度了一下 結果沒有 就研究了一下。
當使用者輸入郵箱形式的帳號時,輸入完“@”符號後,聯想出常用的郵箱
點擊某一行,將改行代表郵箱自動輸入到帳號輸入框內
如果控制項屬性不懂或者不認識 ,請百度!
說一下原理,首先我們要判斷輸入的是否是“@”,之後在在進行範圍截取,最後匹配
- (BOOL)hasPrefix:(NSString *)aString //系統 已經提供了匹配方法,用不著正則! 直接上代碼!
#import "UserLoginViewController.h"
@interface UserLoginViewController ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>
{
BOOL _showList;
}
@property (nonatomic)UITextField *accountTextField;
@property (nonatomic)UITableView *listTableView;
@property (nonatomic)NSArray *emalArray;//郵箱尾碼
@property (nonatomic)NSMutableArray *tabviewData; //伺服器資料
- (void)dealloc
{
[selfunregisterNotifications];
}
- (void)viewDidLoad {
[superviewDidLoad];
[selfregisterNotifications];
_showList = NO;//預設不顯示
self.emalArray = [[NSArray alloc] initWithObjects:@"sohu.com",@"sina.com",@"sina.cn",@"163.com",@"126.com",@"qq.com",@"hotmail.com",@"gmail.com", nil];
self.tabviewData = [NSMutableArray array];
_accountTextField= [selfcreateLoginField:@"手機號/使用者名稱/郵箱/"]; //此處自訂控制項 不會請百度
_accountTextField.frame =CGRectMake(0,0,220,49);
[self.view addSubview:_accountTextField];
//下拉式清單
_listTableView = [[UITableViewalloc]initWithFrame:
CGRectMake(0,0,280,120)];
_listTableView.top =50;
_listTableView.left =20;
_listTableView.dataSource=self;
_listTableView.delegate=self;
_listTableView.bounces =NO;
_listTableView.backgroundColor= [UIColorwhiteColor];
_listTableView.separatorColor= [UIColorlightGrayColor];
_listTableView.hidden=!_showList;//一開始listView是隱藏的,此後根據showList的值顯示或隱藏
[self.viewaddSubview:_listTableView];
// Do any additional setup after loading the view.
}
-(BOOL)showList{//setShowList:No為隱藏,setShowList:Yes為顯示
return_showList;
}
-(void)setShowList:(BOOL)iShow{
_showList=iShow;
_listTableView.hidden=!iShow;
}
核心代碼
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[[[UIApplicationsharedApplication] keyWindow]endEditing:YES];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//判斷text是否輸入過@ 如果輸入過則不出現下啦菜單
NSString *text = [textField.textstringByReplacingCharactersInRange:range withString:string];
if (textField ==_accountTextField) {
//是否包含@
if ([text containsString:@"@"]) {
[selfsetShowList:YES];
[self.tabviewDataremoveAllObjects];
//範圍
NSRange range = [text rangeOfString:@"@"];
if ((range.location + range.length) == text.length) {
for (NSString *strin self.emalArray) {
[self.tabviewDataaddObject:[NSStringstringWithFormat:@"%@%@",text,str]];
}
}else{
NSString *suffix = [text substringWithRange:NSMakeRange(range.location+range.length, text.length-(range.location+range.length))];
NSString *headText = [text substringWithRange:NSMakeRange(0,range.location+range.length)];
for (NSString *strin self.emalArray) {
//匹配
if ([str hasPrefix:suffix]) {
[self.tabviewDataaddObject:[NSStringstringWithFormat:@"%@%@",headText,str]];
}
}
if (self.tabviewData.count<=0) {
[selfsetShowList:NO];
}
}
[self.listTableViewreloadData];
}else
{
[selfsetShowList:NO];
}
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
//返回一個BOOL值指明是否允許根據使用者請求清除內容
//可以設定在特定條件下才允許清除內容
[selfsetShowList:NO];
return YES;
}
#pragma mark 監聽鍵盤
- (void)registerNotifications
{
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(textFiledEditChanged:)
name:@"UITextFieldTextDidChangeNotification"
object:nil];
}
- (void)unregisterNotifications
{
//移除通知
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}
- (void)textFiledEditChanged:(NSNotification *)obj
{
//此處可以拿到 正在輸入的值 做一些處理
}
#pragma mark listViewdataSource method and delegate method
-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
returnself.tabviewData.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellid=@"listviewid";
UITableViewCell* cell=[tableViewdequeueReusableCellWithIdentifier:cellid];
if(cell==nil){
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellid];
cell.layer.borderColor = [UIColorgrayColor].CGColor;
cell.layer.borderWidth =1;
}
cell.textLabel.frame =CGRectMake(0,0, 220,40);
cell.textLabel.text = [self.tabviewDataobjectAtIndex:indexPath.row];
cell.textLabel.font =_accountTextField.font;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
//當選擇下拉式清單中的一行時,設定文字框中的值,隱藏下拉式清單
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//顯示值
NSString *string = [self.tabviewDataobjectAtIndex:indexPath.row];
_accountTextField.text = string;
[selfsetShowList:NO];
}
郵箱尾碼
這是不能設定的,基本是伱在那個網站申請郵箱那麼尾碼就是那個網站的
比如QQ的尾碼就是qq,網易的尾碼就是163等
這個好、郵箱尾碼有什問題
1、可能是E-mail地址寫錯了。
2、可能是對方對發過來的郵件大小有限制。
3、可能是對方的郵箱滿了。
4、可能是你的郵址是在對方設定了攔截的名單內。
不是以上四點問題的話就有這些可能
語言亂碼:
與雙方使用的編碼方式有關。
不同語言之間發信時,優先考慮用UTF-8編碼。
如果經常出現亂碼,用附件也是一個思路。
跨國家伺服器問題:
1)對方的伺服器自動退回未經充許的電子郵件。
a. 即你必須要在收信人的白名單裡。
b. 需通過對方伺服器驗證
解決方案是通過其它方式聯絡收信人
2)你的郵件附件過大,或者被收信人拒收,或者你在黑名單中。
還有種鬱悶的事。
法國,德國,意大利還有一些其它國家的郵件伺服器會不定時自動屏蔽來自中國的郵件。
嘗試使用gmail,hotmail等郵箱進行初步聯絡吧。
實在不行就只有自己換郵箱了
推薦使用Gmail!Gmail是一種新型的網路郵件,它的設計理念在於讓電子郵件更直觀、更有效且更實用,甚至可以比較有趣。個Google公司的Gmail相信很多人早就想註冊了,只是以前註冊都必須要有邀請才可以,不過現在Gamil升級後,直接存取www.googlemail.com就可以註冊了。現在空間也增大到了7123MB,並且還在不斷增加中。Gmail的功能非常齊全:1.只要將手機的網路瀏覽器指向 googlemail.com,便能從手機或行動裝置訪問Gmail。它簡單易用,而且完全免費(但是請注意,您可能還是需要支付使用無線服務的資費)。2. @googlemail.com、@yourschool.edu 或 @yourcustomdomain.com 隨您選用。現在您可以自訂外發郵件上的“寄件者:”地址,改以顯示您的其他地址。您可以使用自己所擁有的任意別名或電子郵件地址,但發送過程仍需通過Gmail 進行。3. 只需點擊一下,便可與您的電子郵件發送對象在在Gmail 中聊天(有點類似MSN),甚至還可以通過聊天回複電子郵件。Gmail 可以對所有聊天記錄存檔並進行相應設定,使其可供搜尋,因此您絕不會丟失任何有價值的資訊。4. 現在通過 Gmail 可以檢查您在其他電子郵件帳戶中接收到的郵件。您可以最多檢索五個其他電子郵件帳戶中的新舊郵件,並將這些郵件全部集中在Gmail 裡。然後,您甚至可以建立一個自訂的“寄件者:”地址,這樣您就可以從Gmail 發送郵件,但看起來卻像是從您的其他電子郵件帳戶中送出的。請注意,您只能從啟用了POP3 訪問的帳戶中檢索郵件。其他還有自動回覆,自動封存等個人化的功能請登陸Gmail網站查詢。 參考資料: www.9eshu.cn/article/5c/194.html如果樓主有重要的事情,建議不要用163.也不太清楚是什麼原因,163,126,sohu等國內郵箱經常常向國外很多郵箱發郵件失敗或者被當作垃圾......餘下全文>>