iOS控件之UITextView字数控制以及占位符的实现
2016-07-12 13:30
477 查看
下面介绍 UITextView 的字数控制,占位符,以及键盘回退方法.
1.字数控制实现
1.字数控制实现
#import "ViewController.h" #define kScreenWidth [UIScreen mainScreen].bounds.size.width @interface ViewController ()<UITextViewDelegate> /** textView */ @property (nonatomic, weak) UITextView *textView; /** placeHoldLabel */ @property (nonatomic, weak) UILabel *placeHoldLabel; /** noticeLabel */ @property (nonatomic, weak) UILabel *noticeLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 200)]; //初始化大小并自动释放 _textView = textView; textView.textColor = [UIColor blackColor];//设置字体颜色 textView.font = [UIFont fontWithName:@"Arial" size:18.0];//设置 字体 和 大小 textView.delegate = self;// 设置控制器为 textView 的代理方法 textView.backgroundColor = [UIColor lightGrayColor];//设置它的背景颜色 textView.returnKeyType = UIReturnKeyDefault;//返回键的类型 textView.keyboardType = UIKeyboardTypeDefault;//键盘类型 textView.scrollEnabled = YES;//是否可以拖动 [self.view addSubview:textView]; UILabel *placeHoldLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, kScreenWidth, 25)]; self.placeHoldLabel = placeHoldLabel; placeHoldLabel.font = [UIFont systemFontOfSize:18]; placeHoldLabel.enabled = NO; placeHoldLabel.text = @"请输入您的内容..."; placeHoldLabel.numberOfLines=0; placeHoldLabel.font = [UIFont systemFontOfSize:12]; [self.textView addSubview:placeHoldLabel]; UILabel *noticeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 280, kScreenWidth * 0.95, 20)]; self.noticeLabel = noticeLabel; noticeLabel.textAlignment = NSTextAlignmentRight; noticeLabel.text = @"您还可以输入200字!"; [self.view addSubview:noticeLabel]; } #pragma mark - 2.textView delegate -(void)textViewDidChange:(UITextView *)textView { if ([self.textView.text length] == 0) { [self.placeHoldLabel setHidden:NO]; }else{ [self.placeHoldLabel setHidden:YES]; } } - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if([text isEqualToString:@"\n"]){ [textView resignFirstResponder]; return NO; } if (range.location>=200){ self.noticeLabel.text=@"还能输入0字"; return NO; } else { self.noticeLabel.text=[NSString stringWithFormat:@"还能输入%lu字",200-range.location]; return YES; } } @end
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- flex 控件的重要属性
- 学习Winform文本类控件(Label、Button、TextBox)
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- C#编写ActiveX网页截图控件
- Android编程之Button控件用法实例分析
- Android控件之CheckBox、RadioButton用法实例分析
- Android的TextView与Html相结合的具体方法
- 在Android开发中使用自定义组合控件的例子
- 一款超酷的Android自定义加载控件
- Android中实现为TextView添加多个可点击的文本