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