1.若是只是静态显示textView的内容为设置的行间距,执行以下代码:字体
// textview 改变字体的行间距 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 10;// 字体的行间距 NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:15], NSParagraphStyleAttributeName:paragraphStyle }; textView.attributedText = [[NSAttributedString alloc] initWithString:@"输入你的内容" attributes:attributes];
2.若是是想在输入内容的时候就按照设置的行间距进行动态改变,那就须要将上面代码放到textView的delegate方法里spa
-(void)textViewDidChange:(UITextView *)textView { //textview 改变字体的行间距 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 20;// 字体的行间距 NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:15], NSParagraphStyleAttributeName:paragraphStyle }; textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes]; }
UITextView上如何加上相似于UITextField的placeholder呢,其实在UITextView上加上一个UILabel或者UITextView,若是用UILable的话,会出现一个问题就是当placeholder的文字过长致使换行的时候就会出现问题,而用UITextView则能够有效避免此问题。code
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if (![text isEqualToString:@""]) { _placeholderLabel.hidden = YES; } if ([text isEqualToString:@""] && range.location == 0 && range.length == 1) { _placeholderLabel.hidden = NO; } return YES; }
说明以下:blog
(1) _placeholderLabel 是加在UITextView后面的UITextView,_placeholderLabel要保证和真正的输入框的设置同样,字体设置成浅灰色,而后[_placeholderLabel setEditable:NO];真正的输入框要设置背景色透明,保证能看到底部的_placeholderLabel。ci
(2) [text isEqualToString:@""] 表示输入的是退格键it
(3) range.location == 0 && range.length == 1 表示输入的是第一个字符io