iOS UITextView设置placeholder(占位字符

2021-03-24  本文已影响0人  xu1Peng

```

- (UITextView *)textview{

    if(!_textview) {

        _textview= [[UITextViewalloc]initWithFrame:CGRectMake(0,100,ScreenWidth-30,80)];

        _textview.backgroundColor = [UIColor tertiarySystemBackgroundColor];

        self.textview.font= [UIFontfontWithName:@"Arial"size:14.0];

        self.textview.delegate = self;

        self.textview.returnKeyType = UIReturnKeyDefault;

        self.textview.keyboardType = UIKeyboardTypeDefault;

        self.textview.scrollEnabled = YES;

        self.textview.autoresizingMask = UIViewAutoresizingFlexibleHeight;

        // _placeholderLabel

       UILabel*placeHolderLabel = [[UILabelalloc]init];

       placeHolderLabel.text=@"点击编辑备注信息哦~";

       placeHolderLabel.numberOfLines=0;

       placeHolderLabel.textColor= [UIColorlightGrayColor];

       [placeHolderLabelsizeToFit];

       [self.textviewaddSubview:placeHolderLabel];//这句很重要不要忘了

       // same font

       self.textview.font= [UIFontsystemFontOfSize:14.f];

       placeHolderLabel.font= [UIFontsystemFontOfSize:14.f];

       [self.textviewsetValue:placeHolderLabelforKey:@"_placeholderLabel"];

    }

    return _textview;

}

```

上一篇 下一篇

猜你喜欢

热点阅读