iOS добавляет заполнитель в UITextView

    При разработке для iOS мы часто используем UITextField и UITextView. При использовании UITextField система предоставит свой собственный атрибут заполнителя, но не в UITextView. Итак, как установить заполнитель в UITextView, как показано ниже:

метод первый:

#pragma mark - 懒加载
- (UITextView *)textView {
    if (!_textView) {
        _textView = [UITextView new];
        _textView.backgroundColor = [UIColor lightGrayColor];
        _textView.layer.cornerRadius = 5;
        _textView.font = [UIFont systemFontOfSize:12]; //注意必须设置字体大小,不然在UI显示时会出现placeholder字体上移的情况
        
        UILabel *placeHolderLabel = [[UILabel alloc] init];
        placeHolderLabel.text = @"请输入内容";
        placeHolderLabel.numberOfLines = 0;
        placeHolderLabel.textColor = [UIColor lightGrayColor];
        [placeHolderLabel sizeToFit];
        placeHolderLabel.font = [UIFont systemFontOfSize:12];
        [_textView addSubview:placeHolderLabel];
        [_textView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
    }
    return _textView;
}

Способ 2. Используйте стороннюю библиотеку UITextView+Placeholder.

Прямой модуль «UITextView+Placeholder»

Затем #import "UITextView+Placeholder.h" на странице, которую вам нужно использовать.

При использовании следующим образом:

textView.placeholder = @"请输入内容";

 

Je suppose que tu aimes

Origine blog.csdn.net/qq_37269542/article/details/107457893
conseillé
Classement