邮箱自动提示代码
//
// AutoFinishViewController.h
//
//
// Created by maple on 13-9-25.
// Copyright (c) 2013年. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol AutoCompleteDelegate <NSObject>
-(void)completeTextData:(NSString *)completeText;
@end
@interface AutoFinishViewController : UITableViewController
@property(nonatomic)NSInteger originY;
@property(nonatomic)NSInteger sizeWidth;
@property(nonatomic,strong)NSString *defaultText;
@property(nonatomic)NSString *autoText;
@property(assign)id<AutoCompleteDelegate> delegate;
-(void)initialWithTextField:(UITextField*)textField;
@end
//
// AutoFinishViewController.m
//
//
// Created by maple on 13-9-25.
// Copyright (c) 2013年 All rights reserved.
//
#import "AutoFinishViewController.h"
@interfaceAutoFinishViewController ()
{
NSMutableArray *_tipTails;
NSMutableArray *tipDictionary;
}
@end
@implementation AutoFinishViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
tipDictionary = [[NSMutableArrayalloc]initWithObjects:@"gmail.com",
@"yahoo.com",
@"hotmail.com",
@"aol.com",
@"comcast.net",
@"me.com",
@"msn.com",
@"live.com",
@"sbcglobal.net",
@"ymail.com",
@"att.net",
@"mac.com",
@"cox.net",
@"verizon.net",
@"hotmail.co.uk",
@"bellsouth.net",
@"rocketmail.com",
@"aim.com",
@"yahoo.co.uk",
@"earthlink.net",
@"charter.net",
@"optonline.net",
@"shaw.ca",
@"yahoo.ca",
@"googlemail.com",
@"mail.com",
@"qq.com",
@"btinternet.com",
@"mail.ru",
@"live.co.uk",
@"naver.com",
@"rogers.com",
@"juno.com",
@"yahoo.com.tw",
@"live.ca",
@"walla.com",
@"163.com",
@"roadrunner.com",
@"telus.net",
@"embarqmail.com",
@"hotmail.fr",
@"pacbell.net",
@"sky.com",
@"sympatico.ca",
@"cfl.rr.com",
@"tampabay.rr.com",
@"q.com",
@"yahoo.co.in",
@"yahoo.fr",
@"hotmail.ca",
@"windstream.net",
@"hotmail.it",
@"web.de",
@"asu.edu",
@"gmx.de",
@"gmx.com",
@"insightbb.com",
@"netscape.net",
@"icloud.com",
@"frontier.com",
@"126.com",
@"hanmail.net",
@"suddenlink.net",
@"netzero.net",
@"mindspring.com",
@"ail.com",
@"windowslive.com",
@"netzero.com",
@"yahoo.com.hk",
@"yandex.ru",
@"mchsi.com",
@"cableone.net",
@"yahoo.com.cn",
@"yahoo.es",
@"yahoo.com.br",
@"cornell.edu",
@"ucla.edu",
@"us.army.mil",
@"excite.com",
@"ntlworld.com",
@"usc.edu",
@"nate.com",
@"outlook.com",
@"nc.rr.com",
@"prodigy.net",
@"wi.rr.com",
@"videotron.ca",
@"yahoo.it",
@"yahoo.com.au",
@"umich.edu",
@"ameritech.net",
@"libero.it",
@"yahoo.de",
@"rochester.rr.com",
@"cs.com",
@"frontiernet.net",
@"swbell.net",
@"msu.edu",
@"ptd.net",
@"proxymail.facebook.com",
@"hotmail.es",
@"austin.rr.com",
@"nyu.edu",
@"sina.com",
@"centurytel.net",
@"usa.net",
@"nycap.rr.com",
@"uci.edu",
@"hotmail.de",
@"yahoo.com.sg",
@"email.arizona.edu",
@"yahoo.com.mx",
@"ufl.edu",
@"bigpond.com",
@"unlv.nevada.edu",
@"yahoo.cn",
@"ca.rr.com",
@"google.com",
@"yahoo.co.id",
@"inbox.com",
@"fuse.net",
@"hawaii.rr.com",
@"talktalk.net",
@"gmx.net",
@"walla.co.il",
@"ucdavis.edu",
@"carolina.rr.com",
@"comcast.com",
@"live.fr",
@"blueyonder.co.uk",
@"live.cn",
@"cogeco.ca",
@"abv.bg",
@"tds.net",
@"centurylink.net",
@"yahoo.com.vn",
@"uol.com.br",
@"osu.edu",
@"san.rr.com",
@"rcn.com",
@"umn.edu",
@"live.nl",
@"live.com.au",
@"tx.rr.com",
@"eircom.net",
@"sasktel.net",
@"post.harvard.edu",
@"snet.net",
@"wowway.com",
@"live.it",
@"hoteltonight.com",
@"att.com",
@"vt.edu",
@"rambler.ru",
@"temple.edu",
@"cinci.rr.com", nil];
_tipTails=[[NSMutableArray alloc]init];
}
returnself;
}
-(void)initialWithTextField:(UITextField*)textField{
self.originY=textField.frame.origin.y+textField.frame.size.height;
self.sizeWidth=textField.frame.size.width;
[self.view setFrame:CGRectMake((self.view.frame.size.width-_sizeWidth)/2, _originY, _sizeWidth, 90)];
IOS7_LAYOUT();
}
- (void)viewWillAppear:(BOOL)animated{
if (self.defaultText) {
NSRange range = [self.defaultText rangeOfString:@"@"];
NSLog(@"rang:%@",NSStringFromRange(range));
if (range.length&&range.location) {
self.defaultText = [self.defaultText substringToIndex:range.location];
}
}
}
-(void)setAutoText:(NSString *)autoText{
self.defaultText = autoText;
_tipTails = [self autoReplaceText:autoText];
[self.tableViewreloadData];
}
-(NSMutableArray *)autoReplaceText:(NSString *)defaultText{
NSMutableArray *tempArray = [[NSMutableArrayalloc]init];
NSRange range = [defaultText rangeOfString:@"@"];
if (range.location == NSNotFound) {
return tempArray;
}
NSString *tailString = [defaultText substringFromIndex:range.location];//后面那段字符串
NSRange rangeOfDot = [tailString rangeOfString:@"."];
if (rangeOfDot.location != NSNotFound) {
return tempArray;
}
NSArray *textComponents = [defaultText componentsSeparatedByString:@"@"];
if ([textComponents count] >2) {//多于两个@
return tempArray;
}
if ([textComponents count] > 1) {
if ([textComponents[1] length] == 0) {
return tipDictionary;
}
NSString *textAfterAtSign = textComponents[1];
NSString *stringToLookFor;
stringToLookFor = [textAfterAtSign lowercaseString];
for (NSString *stringFromReference in tipDictionary) {
if ([stringFromReference hasPrefix:stringToLookFor]) {
[tempArray addObject:stringFromReference];
}
}
}
return tempArray;
}
- (void)viewDidLoad
{
self.tableView.layer.borderWidth = 1;
self.tableView.layer.borderColor = [[UIColorblackColor] CGColor];
[superviewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_tipTails count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId=@"emailCell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId];
}
NSString *tempShowText = self.defaultText;
NSRange range = [tempShowText rangeOfString:@"@"];
tempShowText = [tempShowText substringToIndex:range.location+1];
cell.textLabel.text=[NSString stringWithFormat:@"%@%@",tempShowText,[_tipTails objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFontsystemFontOfSize:13];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *tempShowText = self.defaultText;
NSRange range = [tempShowText rangeOfString:@"@"];
tempShowText = [tempShowText substringToIndex:range.location+1];
NSString* selectedText = [NSString stringWithFormat:@"%@%@",tempShowText,[_tipTails objectAtIndex:indexPath.row]];
[self.delegate completeTextData:selectedText];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end