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

981 阅读2分钟

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

比较容易看懂,就不多解释了。
下载地址