Let's take a look at the effect chart that you want to implement
I. Introduction to the function
When you enter a number of mailboxes, will be the default to match out @qq.com, of course, this default @qq.com can be replaced by other such as @163.com and so on. Here the default is @qq.com, because our products Wang did statistics most users or use QQ mailbox, so the default is @qq.com.
When you enter the @ symbol it will not change, but if you enter the character after @, it will match this character with the mailbox suffix you want to prompt, I need to match @qq.com,@163.com,@126.com, @yahoo. com,@139.com,@ henu.com type. For example: After the @ you enter 1, this time will match out after the @1 63.com. Then, when you enter 2 after the @1, it will match 6.com behind the @12. The priority of these matches is based on the order you want to match the type of mailbox, and the types of mailboxes that you want to match are fully configurable and can be customized according to the needs of the product.
Two. Talk about usage
1. First of all, if you want to implement this function in the xib, first drag and drop a TextField, set a good constraint, and then let the TextField class associated to Xltextfield, the current premise you want to download and import XLTextField.h
and XLTextField.m
, remember not to forget to textField.delegate
set to Viewcontroller,textfield style can be set in Xib, matching mailbox area color can be set up, and then in the code only need to tune the API
/**
*
*1. Created by xib you just need to assign this parameter
* @param mailtypearray mailbox Matching type
*
/@property (Nonatomic,strong) Nsmutablearray*mailtypearray;
Sample code:
self.textfield.mailtypearray= [nsmutablearrayarraywithobjects:@ "@qq. com", @ "@163.com", @ "@126.com", @ "@yahoo. com", @ "@139.com", @ "@henu. com", nil];
XLTextField.h also provides an optional mailMatchColor
attribute This property is Uicolor type, is the color that matches the mailbox type, can set itself according to the requirement.
/**
*optional matching mailbox type suffix is RGB to 170 170 170 color, you can set the
* */@property (Nonatomic,strong) uicolor* Mailmatchcolor;
2. Create two basic parameters by manual frame set size and font size, other settings can be set to the additional self, invoke API
/**
*2. Call
*
* @param frameframe size *
@param fontsize textfield size
* *@ return self
*/-(instancetype) initWithFrame: (CGRect) frame fontsize: (cgfloat) fontsize;
Sample code:
Xltextfield * field = [[Xltextfield alloc] Initwithframe:cgrectmake (MB, m) fontsize:12];
Field.placeholder = @ "Enter the email address";
Field.mailtypearray = [Nsmutablearray arraywithobjects:@ "@qq. com", @ "@163.com", @ "@126.com", @ "@yahoo. com", @ "@139. com ", @" @henu. com ", nil];
Field.mailmatchcolor = [Uicolor Redcolor]; Optional attributes
[Self.view Addsubview:field];
In the example program, I only write gesture closure for Xib TextField, end TextField input operation, hand hole creation TextField not write end first responder action. If you are in the actual use, can be based on your needs such as click on the keyboard completion button, or touch the screen view and other scenes to end the TextField keyboard first responder, so take TextField text for additional requirements.
Can customize their own frame,font size according to their own needs. But do not support init
and new
initialization methods, that is, it does not matter, there will be a warm error alert
-(Instancetype) init__attribute__ (Unavailable ("Init method unavailable, Initwithname:fontszie:"); + (Instancetype) new__ Attribute__ ((Unavailable ("Init method is not available, please use Initwithname:fontszie:"));
Three. Introduce the principle of realization
1. Perhaps you do not see the source code, it is considered to be a textfield to achieve the full functionality, in fact No. I'm here with a label that uses Aulayout to manually add a label when the Xib is implemented. TextField is only used for user input, the label is used to store the matching mailboxes (mailbox number + mailbox type), and display. What you see is actually the text content of the label. When the edit is finally finished, label copies the contents to TextField text, and then clears the label. The label is only here to mention the implementation, the actual use of the value or through textField.text
the value obtained. Here you may spit, why not a TextField, more simple and convenient. But the fact is that I have tried a textfield, in the middle encountered a pit, as if it is TextField a bug, for the time being, because it is really a wonderful work of the problem, the specific is so also forget, here also do not start to say, interested you can use a TextField to try Kazakhstan.
2.textField agent methods are all encapsulated in the interior textField.m
, has been handled in the interior of these agents, more convenient for others to call, do not have to spend a lot of thought in debugging the TextField agent method. Whether created with Xib or manual code, you do not need to be set textField.delegate
to a controller.
3. In the TextField shouldChangeCharactersInRange
proxy method for each character you enter the mailbox matching, the following will have this piece of complete code. The textFieldDidEndEditing
value of the label is assigned to the proxy method textField.text
, and then Label.text
emptied and directly taken textField.text
as our final result.
Key code that matches the mailbox process is attached below, and each row has a comment.
/** * Matching Mailbox Process * * @param rangerange * @param string user input string */-(void) Configmailmatchingrange: (nsrange) Range Replacementstring: (nsstring*) string {//Get complete input text NSSTRING*COMPLETESTR = [ self.textstringbyreplacingcharactersinrange:rangewithstring:string];//the text nsarray*temailarray at the @ symbol = [ completestrcomponentsseparatedbystring:@ "@"];//get mailbox prefix nsstring*emailstring = [temailarrayfirstobject];// The mailbox match does not enter the @ symbol with the @ match nsstring*matchstring =@ "@"; if (temailarray.count>1) {///If you have entered the string after the @ symbol intercepts the @ symbol as a matching string matchstring =
[CompleteStrsubstringFromIndex:emailString.length]; }//matching mailbox gets all the mailbox suffixes that match the current input nsmutablearray*suffixarray = [selfcheckemailstr:matchstring];//boundary control if no suffix matching current input is set to @ " Nsstring*fixstr = suffixarray.count>0? [Suffixarrayfirstobject]: @ ""//Lblemail part of the field is hidden Nsintegercutlenth = suffixarray.count>0? completestr.length:emailstring.length;//the final email address self.email= fixstr.length>0? [nsstringstringwithformat:@ "%@%@", Emailstring,fixstr]: completestr;// Set the Lblemail Attributensmutableattributedstring*attributestring = [[nsmutableattributedstringalloc]initwithstring:[nsstringstringwithformat:@ "%@%@", EmailString,fixStr]]; [Attributestringaddattribute:nsforegroundcolorattributenamevalue:[uicolorclearcolor]range:nsmakerange (0, Cutlenth)];self.maillabel.attributedtext= attributestring;//Empty The contents of the text box to hide Lblemailif (completestr.length==0) {
self.maillabel.text=@ ""; self.email=@ ""; }
}
Iv. Summary
The above is the iOS implementation of the mailbox fuzzy matching function of all the functions, the realization is not very easy to use it? Interested in the quick hands-on practice, hope for everyone's study or work can help.