博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS控件之UITextView字数控制以及占位符的实现
阅读量:4047 次
发布时间:2019-05-24

本文共 2350 字,大约阅读时间需要 7 分钟。

下面介绍 UITextView 的字数控制,占位符,以及键盘回退方法.

1.字数控制实现

#import "ViewController.h"#define kScreenWidth [UIScreen mainScreen].bounds.size.width@interface ViewController ()
/** 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

转载地址:http://nfwci.baihongyu.com/

你可能感兴趣的文章
mysql 主从同步配置
查看>>
为什么很多程序员都选择跳槽?
查看>>
mongdb介绍
查看>>
mongdb安装使用
查看>>
mongdb在java中的应用
查看>>
区块链技术让Yotta企业云盘为行政事业服务助力
查看>>
Yotta企业云盘更好的为媒体广告业服务
查看>>
Yotta企业云盘助力旅游行业新发展
查看>>
Yotta企业云盘助力科技行业创高峰
查看>>
Yotta企业云盘更好地为教育行业服务
查看>>
Yotta企业云盘怎么帮助到能源化工行业
查看>>
企业云盘如何助力商业新发展
查看>>
医疗行业运用企业云盘可以带来什么样的提升
查看>>
教育数字智能化能为现有体系带来新的起点
查看>>
媒体广告业如何将内容资产进行高效地综合管理与利用
查看>>
能源化工要怎么管控核心数据
查看>>
媒体广告业如何运用云盘提升效率
查看>>
企业如何运用企业云盘进行数字化转型-实现新发展
查看>>
司法如何运用电子智能化加快现代化建设
查看>>
iSecret 1.1 正在审核中
查看>>