iOS 开发之TextView的提示文字属性使用和代理方法使用
来源:互联网 发布:霏慕淘宝孔雀模特名字 编辑:程序博客网 时间:2024/05/29 16:14
iOS 开发之TextView的属性使用和代理方法使用
iOS开发中遇到要写textview的提示文字 写这些东西要用到textview的代理方法
下面有我和大家分享一下是如何实现的吧
1. 要创建一个UITextView 对象和一个Label对象
import "TextViewController.h"@interface TextViewController ()<UITextViewDelegate>//添加textview的代理@property(nonatomic,strong) UITextView *textView;@property(nonatomic,strong) UILabel *promptTitle;@end
//后面代理方法会用到创建的对象 所以尽量创建一个全局的
self.textView = [[UITextView alloc]initWithFrame:CGRectMake(20, label.frame.size.height +10, 260, 110)]; self.textView.pagingEnabled = YES; self.textView.clipsToBounds = YES; self.textView.layer.borderWidth = 1; self.textView.tag = 100; self.textView.delegate = self;//将代理设置给本类 [self.view addSubview:self.textView]; //创建Label 加提示语 self.promptTitle = [[UILabel alloc]initWithFrame:CGRectMake(5,5,100,20)]; self.promptTitle.text = @"最多可输入140字"; self.promptTitle.font = [UIFont systemFontOfSize:12.0]; [self.promptTitle setTextColor:[UIColor colorWithRed:190/255.0 green:190/255.0 blue:190/255.0 alpha:0.8]]; // [self.textView addSubview:self.promptTitle];
2.textView的代理方法
//判断是否超出最大限额 140- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if ([text isEqualToString:@""] && range.length > 0) { //删除字符肯定是安全的 return YES; } else { if (textView.text.length - range.length + text.length > 140) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"超出最大可输入长度" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];//如果输入的文字大于140 则提示 <span style="font-family: Arial, Helvetica, sans-serif;">"超出最大可输入长度" 并不能继续输入文字</span> [alert show]; return NO; } else { return YES; } }}
3.点击textview的输入框使其文字隐藏
-(void)textViewDidChange:(UITextView *)textView{ if(textview.length>0) { promptTitle.hidden = yes; } else { promptTitle.hidden =NO; }}
//点击屏幕任意一处取消键盘的第一响应项- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES];}
0 0
- iOS 开发之TextView的提示文字属性使用和代理方法使用
- 【iOS开发】设置textView的默认提示文字
- 【iOS开发】设置textView的默认提示文字
- iOS代理方法和block的使用
- 【iOS开发】设置textView的默认提示文字,也就是textView变相的"placeholder"。
- Android编程开发之TextView文字显示和修改方法
- 使用代理的方法 iOS
- [iPhone开发之控件的使用]UIActionSheet的各种属性、方法及代理的使用
- [iPhone开发之控件的使用]UIAlertView的各种属性、方法及代理的使用
- [iPhone开发之控件的使用]UIAlertView的各种属性、方法及代理的使用
- [iPhone开发之控件的使用]UIActionSheet的各种属性、方法及代理的使用
- IOS开发:关于UIPickerView类的代理方法titleForRow和viewForRow的使用问题
- android开发之TextView显示加载的文字和图片
- iOS开发之UitableViewCell中UISwitch的使用,代理实现
- IOS开发UIGestureRecognizer的基本使用方式和代理
- iOS开发-Delegate 代理 使用和注意事项
- iOS开发之transitionFromViewController方法的使用
- IOS开发之 ---- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- Java的进阶过程
- 2014找工作----扎实的基础和开阔的视野是企业最看重的因素
- 3Sum Closest
- 欢迎使用CSDN-markdown编辑器
- iOS NSString字符串反转的问题
- iOS 开发之TextView的提示文字属性使用和代理方法使用
- 网银安全控件远程代码执行漏洞分析
- json格式时间戳转为正常年月日
- abap写发送邮件程序
- ADF组件模型2
- 【第十章】集成其它Web框架 之 10.1 概述 ——跟我学spring3
- 2.5年, 从0->阿里
- 【java中的 嵌套管程锁死】
- iOS 字符串遍历