iOS-UITextView设置行间距,内容颜色(变相设置类似UITextField的placeholder)

简介: iOS-UITextView设置行间距,内容颜色(变相设置类似UITextField的placeholder)

1.在用UITextView的时候一直很苦恼placeholder的设置,最开始是通过添加label,后来觉得麻烦,就通过UITextView本身的一些特性来进行模拟。


2.UITextView的行间距设置方法网上很容易搜的到,但是运用中却发现直接设置根本不行,原因是textView一开始必需要有内容(个中原因,不深究,估计是系统级的),但是有又很奇怪,所以想到个主意,一开始就给textView内容,输入时删除,这样就有了1中的placeholder效果,可谓一举两得。


3.做出了placeholder的效果,颜色也需要区别,发现这句:

    _textView.textColor = [UIColor redColor];

完全没有作用,所以也需要通过属性来设置了,具体的要实现以上内容的代码请看下面

1.png

首先要支持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

比较容易看懂,就不多解释了。

目录
相关文章
|
3月前
|
安全 前端开发 iOS开发
钉钉里微应用ios 底部安全区域的颜色怎么修改?
钉钉里微应用ios 底部安全区域的颜色怎么修改?
58 5
|
7月前
|
iOS开发
iOS 渐变颜色 CGGradientCreateWithColorComponents 属性介绍
iOS 渐变颜色 CGGradientCreateWithColorComponents 属性介绍
60 0
|
7月前
|
iOS开发
iOS UIPageViewController 翻页背景颜色修改
iOS UIPageViewController 翻页背景颜色修改
27 0
|
4月前
|
API 开发工具 iOS开发
在应用研发平台EMAS中,ios的推送有没有办法在app端设置在收到通知后是否展示的逻辑
在应用研发平台EMAS中,ios的推送有没有办法在app端设置在收到通知后是否展示的逻辑
33 1
|
iOS开发
iOS 15后设置导航控制器的导航条背景色无效的问题处理
iOS 15后设置导航控制器的导航条背景色无效的问题处理
374 0
|
7月前
|
缓存 iOS开发
iOS LaunchScreen.storyboard 启动页设置图片不显示
iOS LaunchScreen.storyboard 启动页设置图片不显示
139 0
|
10月前
|
小程序 API Android开发
小程序获取WIFI的API(IOS conncetWifi()自动跳转设置页)
小程序获取WIFI的API(IOS conncetWifi()自动跳转设置页)
264 0
|
存储 自然语言处理 API
iOS 多语言快捷设置Xib设置
iOS 多语言快捷设置Xib设置
iOS 多语言快捷设置Xib设置
iOS--设置系统导航栏右上角按钮不显示问题
iOS--设置系统导航栏右上角按钮不显示问题
196 0