1. 程式人生 > >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];

完全沒有作用,所以也需要通過屬性來設定了,具體的要實現以上內容的程式碼請看下面:
這裡寫圖片描述
首先要支援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

比較容易看懂,就不多解釋了。
下載地址