UITextView的placeholder属性

来源:互联网 发布:足彩数据接口 编辑:程序博客网 时间:2024/05/01 08:51

写项目的时候,发现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];
}


0 0
原创粉丝点击