Customize Keyboard ~ Novice Look

Source: Internet
Author: User

Writing this log and some of the simple things in front of you is just a little bit of a recap of your own bits and pieces. From runtime runloop to the bottom of the function it's probably getting old. I have learned a series of languages like Java Python PHP Scala C # js and so on. and to apply for the hope that there will be better scenery ~ ~ This log is very suitable for beginners to see the big God do not spray

Custom keyboard Scenario Password input

Create

Customtextfield inherited from Uitextfield

Keyboardview inherited from UIView

CustomTextField.h

@interface customtextfield:uitextfield/* specified area, reminder text, left view picture name */-(instancetype) initWithFrame: (CGRect) Frame               Withplacholder: (NSString *) holder            withleftimagename: (NSString *) name; @end

Customtextfield.m

#Import"CustomTextField.h"@InterfaceCustomtextfield ()///attribute to record the name of the picture@property (copy, nonatomic) NSString *Leftimagname;///The attribute used to record the reminder text (watermark)@property (copy, nonatomic) NSString *Textholder;///Left View@property (Strong, nonatomic) Uiimageview *Leftimageview, @end @implementation Customtextfield-(Instancetype) initWithFrame: (CGRect) frame withplacholder: (NSString*) Holder Withleftimagename: (NSString*) name{//Record related namesSelf.leftimagname =name; Self.textholder=Holder; return[self initwithframe:frame];}/*to initialize the settings*/-(Instancetype) initWithFrame: (cgrect) frame{if(self = [SuperInitwithframe:frame]) {        ///Basic configuration of the current text box (Leftview)//when editing, the Clear button is displayedSelf.clearbuttonmode =uitextfieldviewmodewhileediting; //set the background image of the current text boxSelf.background = [UIImage imagenamed:@ "Background"]; //Set Reminder TextSelf.placeholder =Self.textholder; //Set left ViewSelf.leftview =Self.leftimageview; //set the form of left view rendering (default does not appear)Self.leftviewmode =uitextfieldviewmodealways; }    returnSelf ;} #pragma mark#pragma Mark Attribute-(Uiimageview *) leftimageview{if(!_leftimageview) {_leftimageview= [[Uiimageview alloc]initwithframe:cgrectmake (10, 5, 35, 35)];        [_leftimageview setimage:[uiimage ImageNamed:self.leftImagName]; //How content fills (the default stretch fill)_leftimageview.contentmode =Uiviewcontentmodecenter; }    return_leftimageview;} @end

KeyBoardView.h

#Import<UIKit/UIKit.h>@classKeyboardview, @protocol keyboardviewdelegate<NSObject>//Proxy method that uses the data on the button to be passed back and forth- (void) Sendmessageinview: (Keyboardview *) bview backcontent: (NSString*) content; @end//1typedefvoid(^block) (Keyboardview *,nsstring *);@InterfaceKeyboardview:uiview///proxy Object@property (assign, nonatomic) ID <KeyBoardViewDelegate>delegate;///2block Property@property (copy, nonatomic) Block Myblock;//method to point to a block of code for Myblock- (void) Viewwithblock: (Block) inblock; @end

keyboardview.m

#Import"KeyBoardView.h"StaticCGFloatConstKmargin = 20;StaticNsuintegerConstKbtncountinrow = 3;StaticCGFloatConstKbtnheight = 35; #define Screen_width [UIScreen mainscreen].bounds.size.width@InterfaceKeyboardview () @property (copy, nonatomic) Nsarray*arrbtntitles, @end @implementation Keyboardview-(Instancetype) initWithFrame: (cgrect) frame{if(self = [SuperInitwithframe:frame]) {CGFloat Btnwidth= (Screen_width-(Kmargin * (Kbtncountinrow + 1))/Kbtncountinrow; //Loop Create button, add to Current View         for(inti = 0; i < Self.arrBtnTitles.count; i++) {UIButton*BTN =[UIButton Buttonwithtype:uibuttontypecustom]; //Set Button caption[btn Settitle:self.arrbtntitles[i] forstate:uicontrolstatenormal]; //Line            intx = i%Kbtncountinrow; //column            inty = I/Kbtncountinrow; //set the area of the button[Btn Setframe:cgrectmake (Kmargin + x * (Kmargin + btnwidth), Kmargin + y * (Kmargin +kbtnheight), Btnwidth, Kbtnheight)]; //set a color for the button[btn Setbackgroundcolor:[uicolor Orangecolor]; //set the color for the button caption[btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; //associating an event with a button[btn addtarget:self Action: @selector (btnpressed:) forcontrolevents:uicontroleventtouchupinside]; //Add a button to the view[self addsubview:btn]; }    }    returnSelf ;}///button Associated event- (void) btnpressed: (UIButton *) btn{[UIView animatewithduration:0.1 animations:^{Btn.alpha= 1.0; Btn.alpha= 0.2; Btn.alpha= 1.0;//btn.transform = Cgaffinetransformmakescale (2.5, 2.5);//Btn.transform = Cgaffinetransformmakescale (1.0, 1.0);    }]; NSLog (@"%@", Btn.titleLabel.text); //determines whether the proxy object responds to the method if the response is called//if ([Self.delegate respondstoselector: @selector (sendmessageinview:backcontent:)]) {//[Self.delegate sendmessageinview:self backContent:btn.titleLabel.text];//    }    //use block for data backhaul (call Myblock)Self.myblock (Self,btn.titlelabel.text); //Animate a button    }///Let Myblock point to a block of code- (void) Viewwithblock: (Block) inblock{Self.myblock=Inblock;} #pragma mark#pragma Mark Attribute///Store button title-(Nsarray *) arrbtntitles{if(!_arrbtntitles) {_arrbtntitles= [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", @ "#", @ "0", @ "*", nil]; }    return_arrbtntitles;}

And then call it on the VIEWCONTROLLER.M.

#Import"ViewController.h"#Import"CustomTextField.h"#Import"KeyBoardView.h"@InterfaceViewcontroller () <KeyBoardViewDelegate,UITextFieldDelegate>///Custom text box@property (Strong, nonatomic) Customtextfield *tfname;///Custom Keyboard@property (Strong, nonatomic) Keyboardview *Cusinputview;///String that records text content@property (Strong, nonatomic) nsmutablestring *textfieldcontents, @end @implementation viewcontroller- (void) Viewdidload {[SuperViewdidload]; Self.view.backgroundColor=[Uicolor Graycolor];    [Self.view AddSubview:self.tfName]; //Accept data from block backhaul[Self.cusinputview viewwithblock:^ (Keyboardview *kview, NSString *content) {        //Append data to the callback[Self.textfieldcontents appendstring:content]; //set the text content of a tfnameSelf.tfName.text =self.textfieldcontents; }];}- (void) didreceivememorywarning {[Superdidreceivememorywarning]; //Dispose of any resources the can be recreated.}- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event{[Self.view Endediting:yes];} #pragma mark#pragma Mark Delegate//methods in the Protocol- (void) Sendmessageinview: (Keyboardview *) bview backcontent: (NSString *) content{//Append data to the callback[Self.textfieldcontents appendstring:content]; //set the text content of a tfnameSelf.tfName.text =self.textfieldcontents;}/*When you click the Clear button, the method callback*/-(BOOL) Textfieldshouldclear: (Uitextfield *) textfield{//null value for the string that originally recorded the contents[Self.textfieldcontents replacecharactersinrange:nsmakerange (0, Self.textFieldContents.length) withstring:@ ""]; returnYES;} #pragma mark#pragma Mark Attribute-(Customtextfield *) tfname{if(!_tfname) {_tfname= [[Customtextfield alloc]initwithframe:cgrectmake (0, Cgrectgetwidth, self.view.frame) withplacholder:@ "input name "Withleftimagename:@" UserName "]; //set the Inputview of a text box (custom keyboard)_tfname.inputview =Self.cusinputview; //set up the proxy (to click the Clear button when the callback)_tfname.delegate =Self ; //InputaccessoryviewUIView*headerview = [[UIView alloc]initwithframe:cgrectmake (0, 0, cgrectgetwidth (self.view.frame), 40)]; Headerview.backgroundcolor=[Uicolor Blackcolor]; _tfname.inputaccessoryview=Headerview; }    return_tfname;}-(Keyboardview *) cusinputview{if(!_cusinputview) {_cusinputview= [[Keyboardview alloc]initwithframe:cgrectmake (0, 0, cgrectgetwidth (self.view.frame), 250)]; //Set proxy Object_cusinputview.delegate =Self ; }    return_cusinputview;}-(Nsmutablestring *) textfieldcontents{if(!_textfieldcontents) {_textfieldcontents=[nsmutablestring string]; }    return_textfieldcontents;} @end

is not Very simple~~

Customize Keyboard ~ Novice Look

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.