UITextView的placeholder属性
2016-03-29 14:36
525 查看
写项目的时候,发现UITextField并没有换行的属性,所以采用UITextView来实现,最后发现一个问题,UITextView并没有placeholder(占位符)属性。
解决方案:直接看代码如下:
#import "DiscussViewController.h"
#import "ProgressHUD.h"
#import "TitleViewController.h"
@interface DiscussViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UITextView *discussView;
@property (nonatomic, strong) UILabel *lable;
@property (nonatomic, strong) UIButton *sendBtn;//发送按钮
@end
@implementation DiscussViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"评论";
[self showBarButtonWithImage:@"back_arrow"];
self.view.backgroundColor = [UIColor colorWithRed:232/255.0 green:233/255.0 blue:232/255.0 alpha:1.0];
[self.view addSubview:self.discussView];
[self.view addSubview:self.lable];
self.sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.sendBtn.frame = CGRectMake(0, 0, 60, 44);
[self.sendBtn setTitle:@"发送" forState:UIControlStateNormal];
//调整btn标题所在的位置,距离btn顶部,左边,底部,右边的距离
[self.sendBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[self.sendBtn addTarget:self action:@selector(sendBtnAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:self.sendBtn];
rightBarBtn.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = rightBarBtn;
}
#pragma mark ------------- lazy Loading
- (UITextView *)discussView{
if (_discussView == nil) {
self.discussView = [[UITextView alloc] initWithFrame:CGRectMake(20, KScreenWidth/10, KScreenWidth-40, KScreenWidth/2)];
self.discussView.backgroundColor = [UIColor whiteColor];
self.discussView.hidden = NO;
self.discussView.delegate = self;
// self.discussView.text = @"_说点什么吧";
self.discussView.textColor = [UIColor lightGrayColor];
self.discussView.font = [UIFont systemFontOfSize:15.0f];
}
return _discussView;
}
- (UILabel *)lable{
if (_lable == nil) {
self.lable = [[UILabel alloc] initWithFrame:CGRectMake(20, KScreenWidth/10, KScreenWidth-40, KScreenWidth/2)];
self.lable.text = @"_说点什么吧~";
self.lable.enabled = NO;
self.lable.numberOfLines = 0;
self.lable.backgroundColor = [UIColor clearColor];
}
return _lable;
}
//实现UITextView的代理
-(void)textViewDidChange:(UITextView *)textView
{
self.discussView.text = textView.text;
if (textView.text.length == 0) {
self.lable.text = @"_说点什么吧";
}else{
self.lable.text = @"";
}
}
#pragma mark --------- 发送按钮的点击方法
- (void)sendBtnAction{
if (self.discussView.text.length <= 0) {
[ProgressHUD showError:@"评论内容不能为空呦"];
}else{
[ProgressHUD showSuccess:@"发送成功"];
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
//点击空白处回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.discussView resignFirstResponder];
}
解决方案:直接看代码如下:
#import "DiscussViewController.h"
#import "ProgressHUD.h"
#import "TitleViewController.h"
@interface DiscussViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UITextView *discussView;
@property (nonatomic, strong) UILabel *lable;
@property (nonatomic, strong) UIButton *sendBtn;//发送按钮
@end
@implementation DiscussViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"评论";
[self showBarButtonWithImage:@"back_arrow"];
self.view.backgroundColor = [UIColor colorWithRed:232/255.0 green:233/255.0 blue:232/255.0 alpha:1.0];
[self.view addSubview:self.discussView];
[self.view addSubview:self.lable];
self.sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.sendBtn.frame = CGRectMake(0, 0, 60, 44);
[self.sendBtn setTitle:@"发送" forState:UIControlStateNormal];
//调整btn标题所在的位置,距离btn顶部,左边,底部,右边的距离
[self.sendBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[self.sendBtn addTarget:self action:@selector(sendBtnAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:self.sendBtn];
rightBarBtn.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = rightBarBtn;
}
#pragma mark ------------- lazy Loading
- (UITextView *)discussView{
if (_discussView == nil) {
self.discussView = [[UITextView alloc] initWithFrame:CGRectMake(20, KScreenWidth/10, KScreenWidth-40, KScreenWidth/2)];
self.discussView.backgroundColor = [UIColor whiteColor];
self.discussView.hidden = NO;
self.discussView.delegate = self;
// self.discussView.text = @"_说点什么吧";
self.discussView.textColor = [UIColor lightGrayColor];
self.discussView.font = [UIFont systemFontOfSize:15.0f];
}
return _discussView;
}
- (UILabel *)lable{
if (_lable == nil) {
self.lable = [[UILabel alloc] initWithFrame:CGRectMake(20, KScreenWidth/10, KScreenWidth-40, KScreenWidth/2)];
self.lable.text = @"_说点什么吧~";
self.lable.enabled = NO;
self.lable.numberOfLines = 0;
self.lable.backgroundColor = [UIColor clearColor];
}
return _lable;
}
//实现UITextView的代理
-(void)textViewDidChange:(UITextView *)textView
{
self.discussView.text = textView.text;
if (textView.text.length == 0) {
self.lable.text = @"_说点什么吧";
}else{
self.lable.text = @"";
}
}
#pragma mark --------- 发送按钮的点击方法
- (void)sendBtnAction{
if (self.discussView.text.length <= 0) {
[ProgressHUD showError:@"评论内容不能为空呦"];
}else{
[ProgressHUD showSuccess:@"发送成功"];
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
//点击空白处回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.discussView resignFirstResponder];
}
相关文章推荐
- iOS开发-UICollectionView实现瀑布流
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
- DuiVision开发教程(17)-对话框
- uestc251导弹拦截【LIS+求出字典序最小】
- 在iOS 8中使用UIAlertController
- Hue介绍入门--Hadoop可视化分析利
- 导入GUIImage时遇到的坑
- MassTransit 学习记录(二) Request/Response
- iosUIpageViewController转屏后里面的子视图没有跟着一起旋转
- 转载 Easyui Tree方法扩展 - getLevel(获取节点级别)
- 转载 Easyui Tree方法扩展 - getLevel(获取节点级别)
- UISearchbar的学习
- C# Random随机数、TimeZone、OperatingSystem信息、StringBuilder这个好用
- 用WidgeDuino创建一个SCADA(监控与数据採集)系统
- 安卓学习笔记之Handler更新UI的几种方法及对比
- HDU 1242 Rescue(bfs+优先队列)
- 通过可变字符串在UILabel上面加横线
- 自定义UICollectionViewFlowLayout
- androidStudio中出现finished with non-zero exit value 2 或者 finished with non-zero exit value 1