iOS-UITextView设置行间距,内容颜色(变相设置类似UITextField的placeholder)
来源:互联网 发布:对人工智能的看法作文 编辑:程序博客网 时间:2024/05/29 07:42
1.在用UITextView的时候一直很苦恼placeholder的设置,最开始是通过添加label,后来觉得麻烦,就通过UITextView本身的一些特性来进行模拟。
2.UITextView的行间距设置方法网上很容易搜的到,但是运用中却发现直接设置根本不行,原因是textView一开始必需要有内容(个中原因,不深究,估计是系统级的),但是有又很奇怪,所以想到个主意,一开始就给textView内容,输入时删除,这样就有了1中的placeholder效果,可谓一举两得。
3.做出了placeholder的效果,颜色也需要区别,发现这句:
_textView.textColor = [UIColor redColor];
完全没有作用,所以也需要通过属性来设置了,具体的要实现以上内容的代码请看下面:
首先要支持UITextViewDelegate;
接着看代码:
#import "ViewController.h"@interface ViewController ()<UITextViewDelegate>{ UITextView *_textView;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.automaticallyAdjustsScrollViewInsets = NO; _textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 100, 300, 80)]; _textView.delegate = self; _textView.text = @"请在这里输入内容"; _textView.layer.cornerRadius = 10; _textView.layer.borderWidth = 1; _textView.layer.borderColor = [UIColor blackColor].CGColor; [self.view addSubview:_textView]; //有内容时该设置才生效,需要切记 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineSpacing = 8; NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:[UIColor grayColor]}; _textView.attributedText = [[NSAttributedString alloc]initWithString:_textView.text attributes:attributes];}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //收起键盘 [_textView resignFirstResponder];}#pragma mark - UITextViewDelegate- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{ //编辑时删除代替placeholder效果的内容 if ([_textView.text isEqualToString:@"请在这里输入内容"]) { textView.text = @""; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineSpacing = 8; NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:[UIColor blackColor]}; _textView.attributedText = [[NSAttributedString alloc]initWithString:_textView.text attributes:attributes]; } return YES;}- (void)textViewDidChange:(UITextView *)textView{ //编辑时把placeholder效果颜色变为正常效果 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineSpacing = 8; NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:[UIColor blackColor]}; _textView.attributedText = [[NSAttributedString alloc]initWithString:_textView.text attributes:attributes];}- (void)textViewDidEndEditing:(UITextView *)textView{ //若编辑结束,内容为空,变最开始placeholder效果 if ([textView.text isEqualToString:@""]) { _textView.text = @"请在这里输入内容"; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineSpacing = 8; NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:[UIColor grayColor]}; _textView.attributedText = [[NSAttributedString alloc]initWithString:_textView.text attributes:attributes]; }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
比较容易看懂,就不多解释了。
下载地址
1 0
- iOS-UITextView设置行间距,内容颜色(变相设置类似UITextField的placeholder)
- UITextView 行间距设置以及placeholder
- iOS开发之UITextView,设置textView的行间距及placeholder
- iOS开发之UITextView,设置textView的行间距及placeholder
- iOS开发之UITextView,设置textView的行间距及placeholder
- iOS开发之UITextView,设置textView的行间距及placeholder
- iOS开发之UITextView,设置textView的行间距及placeholder
- iOS开发之UITextView,设置textView的行间距及placeholder
- iOS开发之UITextView,设置textView的行间距及placeholder
- iOS开发之UITextView,设置textView的行间距及placeholder
- iOS开发之UITextView,设置textView的行间距及placeholder
- UITextField 设置placeholder的颜色
- UITextView设置类似placeholder的效果
- 设置UITextView的行间距
- UITextField设置placeholder颜色
- UITextField设置placeholder颜色
- UITextField设置placeholder颜色
- UITextField 设置placeholder颜色
- Android Studio调试功能使用总结【转】
- Spring MVC基于注解配置入门教程及其代码实例
- 删除U盘记录
- Camera preview 640X480 究竟是什么意思?
- c.toString() 和 String s = new String(c) 区别
- iOS-UITextView设置行间距,内容颜色(变相设置类似UITextField的placeholder)
- at命令的用法
- “java.exe”已退出,代码为 1。
- mysql5.7.13+VS3013 源代码阅读调试
- 【3】 文本挖掘方法论
- 并查集路径压缩方法
- 移花接木 搞定笔记本上的oracle 数据库
- react与框架整合
- AngularJs的路由、模块化与依赖注入