欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS入门教程之UITextField解析

程序员文章站 2022-05-01 17:43:20
ios入门教程之uitextfield解析。 // // viewcontroller.m // uitextfield // // created by hhg on 15/9/...

ios入门教程之uitextfield解析。

//
//  viewcontroller.m
//  uitextfield
//
//  created by hhg on 15/9/22.
//  copyright (c) 2015年 mac. all rights reserved.
//

#import "viewcontroller.h"

@interface viewcontroller ()

@end

@implementation viewcontroller

- (void)viewdidload {
 [super viewdidload];

 uitextfield *textfield = [[uitextfield alloc]initwithframe:cgrectmake(100, 100, 150,100)];

 textfield.backgroundcolor = [uicolor cyancolor];

 [self.view addsubview:textfield];

 textfield.layer.borderwidth = 1;
 [textfield setborderstyle:uitextborderstyleroundedrect];  // 文字是否贴边框

 [textfield setplaceholder:@"请输入内容"];

 [textfield setsecuretextentry:yes];

 textfield.delegate = self;

 // 再次编辑是否清空
 textfield.clearsonbeginediting = yes;

 // 字体大小 = textfield的有效宽度/内容的字节数。
 textfield.font = [uifont systemfontofsize:60]; // 最大值

 textfield.minimumfontsize = 20; // 最小值

 textfield.adjustsfontsizetofitwidth = yes;


 // 清空按键
 [textfield setclearbuttonmode:uitextfieldviewmodewhileediting];

 //类型
 //[textfield setkeyboardtype:uikeyboardtypedecimalpad];

 //风格(样式)
 [textfield setkeyboardappearance:uikeyboardappearancedefault];

 self.view.userinteractionenabled = yes;

 uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc]init];
 [self.view addgesturerecognizer:tap];
 [tap addtarget:self action:@selector(tapclick)];
}

#pragma mark -  uitextfield协议方法
-(void)textfielddidbeginediting:(uitextfield *)textfield {
 nslog(@"已经进入编辑");
}

-(void)textfielddidendediting:(uitextfield *)textfield {
 nslog(@"已经结束编辑");
}

-(bool)textfieldshouldbeginediting:(uitextfield *)textfield {
 nslog(@"是否允许再次编辑");
 return  yes;
}

-(bool)textfieldshouldclear:(uitextfield *)textfield {
 nslog(@"是否允许清空");
 return  yes;
}

-(bool)textfieldshouldendediting:(uitextfield *)textfield {
 nslog(@"是否结束编辑");
 return yes;
}

-(bool)textfieldshouldreturn:(uitextfield *)textfield {
 nslog(@"是否使用return");
 [textfield resignfirstresponder];
 return yes;
}

-(void)tapclick {
 [self.view endediting:yes];
}

- (void)didreceivememorywarning {
 [super didreceivememorywarning];
}

@end