1. While using UITextView, I have been worried about placeholder Settings. At the beginning, I added labels to the placeholder, but later I felt it was troublesome, so I simulated the placeholder with some features of UITextView itself.

2. The method of setting the line spacing of UITextView is easy to search on the Internet, but it is found that the direct setting does not work. The reason is that the textView must have content in the beginning (the reason is not to go into the details, I think it is system level), but it is very strange, so I came up with the idea of giving textView content in the beginning. Input delete, so you have 1 in the placeholder effect, can be said to kill two birds with one stone.

3. Make the placeholder effect, the color should also be different, find this statement:

    _textView.textColor = [UIColor redColor];
Copy the code

It has no effect at all, so it also needs to be set through the property. The specific code to achieve the above content is shown below:



The first thing is to support UITextViewDelegate;

Now look at the code:

#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 = @" Please enter something here "; _textView.layer.cornerRadius = 10; _textView.layer.borderWidth = 1; _textView.layer.borderColor = [UIColor blackColor].CGColor; [self.view addSubview:_textView]; // This setting takes effect only when there is content. Remember that 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< touch *> *)touches withEvent:(UIEvent *)event {  } #pragma mark - UITextViewDelegate - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { 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 {// change the placeholder effect color to normal when editing 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 placeholder effect 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. } @endCopy the code

It’s easier to understand, so I won’t explain it much. Download address