IOS UITextView和UITextFiledvisa和银联的区别别

就有关UITextField,UITextView,UIButton基本属性做一些简短的总结 - 简书
就有关UITextField,UITextView,UIButton基本属性做一些简短的总结
这些基本的属性和方法很简单,但是很重要.好多程序员同志有时候就记不起来了,所我在这儿做一个总结,希望能帮助到大家!?
实际App中的登录界?面并?非是由一个一个?色块组成,?而是由标签(UILabel)、输?入框(UITextField)和按钮(UIButton)组成
一.首先UITextFiled:
UITextField是什么:UITextField(输入框):是控制文本输入和显示的控件。在App中UITextField出现频率也比较高。iOS系统借助虚拟键盘实现输入,当点击输入框,系统会自动调出键盘,?便 你进一步操作。在你不需要输入的时候,可以使用收回键盘的方法,收回弹出的键盘。UITextField和UILabel相比,UILabel主要用于文字显示,不能编辑,UITextField允许用户编辑文字(输入)
如何创建UITextField
1、开辟空间并初始化(如果本类有初始化?方法,使?用?自?己的;否则 使?用?父类的(UIButton就是一个本类初始化方法,下面会提到))。?2、设置?文本显?示、输?入相关的属性?3、添加到?父视图上,?用以显?示?4、释放
UITextField代码如下:
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100,100,200,30)];
textField.backgroundColor = [UIColor whiteColor];
//边框样式
//textField.borderStyle = UITextBorderStyleRoundedR//这个属性会跟UIImage起冲突
textField.borderStyle = UITextBorderStyleN
//占位符(在文本框中显示内容,起到一个占位的作用)
//比如要输入密码,那这儿就写:“请输入密码”
textField.placeholder = @"";
textField.tag = 100;
//二次输入,清空上一次的文本内容
textField.clearsOnBeginEditing = YES;
//设置背景图片,需要设置为
UITextBorderStyleNonetextField.background = [UIImage imageName:@""];//输入你要插入的图片的名称
//设置是否可以编辑textField.enabled = YES;
//设置密码(是否为圆点或者星号)
textField.secureTextEntry = YES;
//修改键盘样式textField.keyboardType = UIKeyboardTypeEmailA
//设置键盘颜色
textField.keyboardAppearance = UIKeyboardAppearanceD
//修改return键文字
textField.returnKeyType = UIReturnKeyJ
//成为第一响应者,刚一进来,键盘就会弹出来
[textField becomeFirstResponder];
//下面这段代码是将键盘用一段红色的区域遮盖掉(可以在这片区域上自定义键盘样式)
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.window.frame.size.width,200)];
inputView.backgroundColor = [UIColor redColor];
textField.inputView = inputV
//在键盘的上面部分出现一部分蓝色区域
UIView *inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.window.frame.size.width,100)];
inputAccessoryView.backgroundColor = [UIColor blueColor];
textField.inputAccessoryView = inputAccessoryV
//清除按钮显示模式(当在编译的时候就会出现一个小X号)
textField.clearButtonMode = UITextFieldViewModeWhileE
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,30,30)];
leftView.backgroundColor = [UIColor redColor];
textField.leftView = leftV
textField.leftViewMode = UITextFieldViewModeA
delegate协议(点击return回收键盘)
步骤:1.将AppDelegate作为UITextField的delegate(你的UITextField在那个类里面写的就用它作为UITextField的delegate)2.AppDelegate.h文件接受UITextFieldDelegate协议3.AppDelegate.m文件显示textFieldShouldReturn:方法
#import "AppDelegate.h"
//第二步:遵守协议
@interface AppDelegate ()&UITextFieldDelegate&
#import "AppDelegate.m"
@implementation AppDelegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//写一个UITextField //第一步:将当前类对象设置为输入框代理
textField.delegate =
//第三步:实现协议方法-(BOOL)textFieldShouldReturn:(UITextField *)textField
//释放第一响应者(可以让键盘自动收放)
[textField resignFirstResponder];
return YES;
下面是AppDelegate里面的应用程序代理,相关方法解释
应用程序代理.png
二.UITextView相关属性,方法介绍:
//初始化并定义大小
UITextView*textview = [[UITextViewalloc] initWithFrame:CGRectMake(20, 10,280, 30)];
textview.backgroundColor=[UIColor whiteColor]; //背景色
textview.scrollEnabled= NO;
//当文字超过视图的边框时是否允许滑动,默认为“YES”
textview.editable = YES;
//是否允许编辑内容,默认为“YES”
textview.delegate =
//设置代理方法的实现类
textview.font=[UIFont fontWithName:@"Arial" size:18.0]; //设置字体名字和字体大小;
textview.returnKeyType=UIReturnKeyD//return键的类型
textview.keyboardType=UIKeyboardTypeD//键盘类型
textview.textAlignment=NSTextAlignmentL//文本显示的位置默认为居左
textview.dataDetectorTypes = UIDataDetectorTypeA //显示数据类型的连接模式(如电话号码、网址、地址等)
textview.textColor = [UIColor blackColor];
textview.text = @"UITextView详解";//设置显示的文本内容
[self.view addSubview:textview];
UITextView的代理方法如下:
//textView是否应该开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView*)textV
//textView是否应该结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textV
//开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textV
//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textV
//内容区是否应该改变编辑
- (BOOL)textView:(UITextView *)textViewshouldChangeTextInRange:(NSRange)range replacementText:(NSString *)
//内容发生改变编辑
- (void)textViewDidChange:(UITextView *)textV
//焦点发生改变
- (void)textViewDidChangeSelection:(UITextView*)textV
#有时我们需要控件自适应输入的文本的内容的高度,只要在textViewDidChange的代理方法中加入调整控件的大小的代理即可
- (void)textViewDidChange:(UITextView *)textView
//计算文本的高度
//constraintSize(约束尺寸)
CGSize constraintS
constraintSize.width = textView.frame.size.width - 16;
constraintSize.height = MAXFLOAT;
CGSize sizeFrame = [textView.text sizeWithFont:textView.font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
//重新调整textView的高度
textView.fram = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, sizeFrame.height + 5);
#控制输入文字的长度和内容,可通过调用一下代理方法实现
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
if(range.location &= 150){
//控制输入文本的长度
return NO;
if([text isEqualToString:@"\n"]){
//禁止输入换行
return NO;
return YES;
//UITextView中退出键盘的几种方法
#1.如果在textView中,键盘上的回车键不用,那么就可以把回车键当成退出键盘的响应键.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return YES;
#2.如果工程里面有导航栏,那么就在导航栏上设置一个按钮,用来当做退出键盘的响应键
- (void)textViewDidBeginEditing:(UITextView *)textView{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(backKeyBoard)];
self.navigationItem.rightBarButtonItem =
- (void)textViewDidEndEditing:(UITextView *)textView
self.navigationItem.rightBarButtonItem =
- (void)backKeyBoard
[ self.textView resignFirstResponder];
#3.可以自定义按钮,添加在键盘上面,用来退出.
- (void)viewDidLoad
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
//设置toolbar的样式颜色
[toolbar setBarStyle:UIBarStyleBlack];
UIBarButtonItem *btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone taget:self action:@selector(backKeyBoard)];
NSArray *buttonsArr = @[btnSpace,btnDone];
[toolbar setItems:buttonsArr];
在文本输入框上加toolbar
[textView setInputAccessoryView:topView];
- (void)backKeyBoard
[textView resignFirstResponder];
UIButton相关属性
//初始化的时候有自己的方法,不用父类的
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100,100,200,30);
//设置按钮文字(有好多样式,你可以试试)
[button setTitle:@"登录" forState:UIControlStateNormal];
//一直点着按钮的时候就会变成下面的title
[butto setTitle:@"loading" forState:UIControlStateHighlighted];
//设置点击的时候是否有光照button.showsTouchWhenHighlighted = YES;
//给button(添加)绑定事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//添加了一个方法
-(void) buttonAction:(UIButton *)sender
//摁了button键后,文本框的内容就会显示在上一篇提到的label上
UITextField *textField = (UITextField *)[self.window viewWithTag:100];
UILabel *label = (UILabel *)[self.window viewWithTag:101];
label.text = textFied.推荐这篇日记的豆列
&&&&&&&&&&&&UITextView 边框的设置 & 设置光标的位置
导入QuartzCote框架:
#import&&QuartzCore/QuartzCore.h&
textView.layer.borderColor&= [UIColor&grayColor].CGColor;
textView.layer.borderWidth&=1.0;
textView.layer.cornerRadius&=5.0;
建立一个UITextView,默认启动键盘,并将光标定位到首位置,因为UITextFiled类没有此功能,所以改用UItextView.
代码如下:
UITextView *m_contentTextField = [[[UITextView alloc] init] autorelease];
m_contentTextField = [[[UITextView alloc] init] autorelease];
m_contentTextField.frame = CGRectMake(0, 0, 320, 90) ;
m_contentTextField.backgroundColor = [UIColor whiteColor] ;
m_contentTextField.font = [UIFont systemFontOfSize:14];
m_contentTextField.delegate =
设置此UITextView为第一响应者,即默认打开键盘。
[m_contentTextField becomeFirstResponder];
当UITextView中含有文字时,系统默认将光标定位到最后的位置,下面的语句将光标定位到首位置。
m_contentTextField.selectedRange = NSMakeRange(0,0);
参考文献:https:///message/09784
(点击可进,已试用,可行)
总体来说个性化定制UITextView中的内容有两种方法:
1,从文件中读取内容到UITextView,这个个人感觉使用rtfd和rtf格式文件效果非常好。
2,使用NSAttributeString进行定制
具体方法如下:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineHeightMultiple = 20.f;
paragraphStyle.maximumLineHeight = 25.f;
paragraphStyle.minimumLineHeight = 15.f;
paragraphStyle.firstLineHeadIndent = 20.f;
paragraphStyle.alignment = NSTextAlignmentJ
NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:paragraphStyle, NSForegroundColorAttributeName:[UIColor colorWithRed:76./255. green:75./255. blue:71./255. alpha:1]
textView.attributedText = [[NSAttributedString alloc]initWithString:content attributes:attributes];
当然也可以初始化一个NSMutableAttributedString,然后向里面添加文字样式,最后将它赋给textView的AttributedText即可
NSMutableAttributedString *atr = [[NSMutableAttributedString alloc]initWithString:detail];
[atr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, detail.length)];
textView.attributedText =
另外,对于textview中的链接样式,同样也可以定制
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor],
NSUnderlineColorAttributeName: [UIColor blackColor],
NSUnderlineStyleAttributeName: @(NSUnderlinePatternDash)};
self.linkTextAttributes = linkA
这里只是个简单的例子,具体还有很多属性可以自行参考头文件
UITextView 文本换行
从XML或者json中读取出来的"\n",系统认为是字符串,会默认转换为"\\n",所以当显示的时候就是字符串了,要想显示换行,需要自己手动将"\\n"转换为"\n",这样才能换行.
NSString*b =[a stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
设置显示内容的padding
textView.textContainerInset = UIEdgeInsetsMake(0, 10, 0, 10);
效果是右侧的滚动条距离内容10像素
阅读(...) 评论()textfield 有时会需要设置字数限制,如果只是英文或者符号,可以直接在以下代理方法中判断字数
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
但是,如果是中文且开启联想的情况下,可能选中(高亮)的字也会加入到字数统计,导致最后的几个字输入失败解决方法给textfield 添加一个事件,实时监听text的改变
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
(void)textFieldDidChange:(UITextField*)textField{
NSString *lang = textField.textInputMode.primaryL//键盘输入模式
if ([lang isEqualToString:@"zh-Hans"]) {//中文
if (textField == _nickTextField) {
UITextRange *selectedRange = [textField markedTextRange];
if (!selectedRange) {//无高亮
if (textField.text.length&8) {
textField.text = [textField.text substringToIndex:8];
[ProgressHUD showWithStatus:@"昵称最多8位"];
}else{//非中文
if (textField.text.length&8&&textField == _nickTextField) {
textField.text = [textField.text substringToIndex:8];
[ProgressHUD showWithStatus:@"昵称最多8位"];
if (textField==_nickTextField) {
NSLog(@"_nickTextField:%@",_nickTextField.text);
}else if (textField==_passWordTextField){
NSLog(@"_passWordTextField:%@",_passWordTextField.text);
相对于textfield而言,textView 更先进一些,他直接提供了text更改完成的代理方法
-(void)textViewDidChange:(UITextView *)textView{
if (textView.text.length) {
_placeLabel.hidden = YES;
_placeLabel.hidden = NO;
NSString *lang = textView.textInputMode.primaryL//键盘输入模式
static NSInteger length = 0;
if ([lang isEqualToString:@"zh-Hans"]){
UITextRange *selectedRange = [textView markedTextRange];
if (!selectedRange) {//没有有高亮
length = textView.text.
length = textView.text.
阅读(...) 评论()

我要回帖

更多关于 苹果7和苹果8的区别 的文章

 

随机推荐