textView的提醒文字
Posted 风雨彩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了textView的提醒文字相关的知识,希望对你有一定的参考价值。
如果我们想提交一些备注信息,会想到用textFiled和textView两种控件去实现
1.提醒文字是textFiled的特有属性,但是textFiled显示文本只有一行,不能实现我们输入较多文字的情况
2.textView可以自动适配多文字输入,但是没有提醒文字,这也不符合我们设计的需求
下面就有一种解决方法,话不多说,直接上代码
#import "YSCreateScheduleRemarkView.h"
#import "Masonry.h"
@interface YSCreateScheduleRemarkView ()
@end
@implementation YSCreateScheduleRemarkView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initUI];
}
return self;
}
- (void)initUI {
self backgroundColor = [UIColor whiteColor];
_textView = [[UITextView alloc] init];
_textView.font = [UIFont systemFontOfSize:15];
[self addSubview:_textView];
[_textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.mas_top);
make.left.mas_equalTo(self.mas_left).offset(10);
make.bottom.mas_equalTo(self.mas_bottom);
make.right.mas_equalTo(self.mas_right).offset(-10);
}];
}
- (void)setplaceholder:(NSString *)placeholder {
UILabel *placeholderLabel = [[UILabel alloc] init];
placeholderLabel.text = placeholder;
placeholderLabel.numberOfLines = 1;
placeholderLabel.textColor = [UIColor lightGrayColor];
[placeholderLabel sizeToFit];
placeholderLabel.font = _textView.font;
[_textView addSubview:placeholderLabel];
[_textView setValue:placeholderLabel forKey:@"_placeholderLabel"];
}
@end
#import "ViewController.h"
#import "YSCreateScheduleRemarkView.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UITextViewDelegate>
{
UITableView *table;
}
@property (nonatomic, strong) YSCreateScheduleRemarkView *createScheduleRemarkView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColorwhiteColor];
table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
[table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CreateScheduleCell"];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.01;
}
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 135;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
_createScheduleRemarkView = [[YSCreateScheduleRemarkView alloc] init];
_createScheduleRemarkView.textView.delegate = self;
[_createScheduleRemarkView setplaceholder:@"备注"];
return _createScheduleRemarkView;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CreateScheduleCell"];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CreateScheduleCell"];
cell.textLabel.text = @"12111";
return cell;
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
[self showKeyboardManager:1]:
}
- (void)textViewDidEndEditing:(UITextView *)textView {
[self showKeyboardManager:0];
}
- (void)showKeyboardManager:(NSInteger)integer {
[UIView animateWithDuration:0.2f animations:^{
table.frame = CGRectMake(0, integer == 1 ? -44*4 : 0, 375, 667);
}];
}
以上是关于textView的提醒文字的主要内容,如果未能解决你的问题,请参考以下文章