iOS 为 textView 添加 placeholder

时间:2021-06-02 17:44:27   收藏:0   阅读:13

OC :

    //自定义一个 placeholder 样式的 label
    UILabel *placeholder = [UILabel new];
    placeholder.text = @"请输入您的评价";
    placeholder.numberOfLines = 0;
    placeholder.textColor = kColor(153,153,153);
    [placeholder sizeToFit];
    placeholder.font = kRegularFont(10);

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(15, 143, SCREEN_WIDTH-30, 48)];
    textView.backgroundColor = kColor(245,245,245);
    textView.textColor = kColor(153,153,153);
    textView.font = kRegularFont(10);
    [textView setValue:placeholderforKey:@"_placeholderLabel"]; //这句话必须写
    [textView addSubview:placeholder]; // 这句话必须写
    [self.mainScroll addSubview:textView];

swift:

    let placeholder = UILabel()
    placeholder.text = "想反馈的内容打在这里~"
    placeholder.numberOfLines = 0
    placeholder.textColor = gray(rgb: 202)
    placeholder.sizeToFit()
    placeholder.font = regularFont(f: 15)
    
    textView = UITextView(frame: CGRect(x: 10, y: 10, width: SCREEN_WIDTH - 20, height: 140))
    textView.backgroundColor = .white
    textView.textColor = gray(rgb: 51)
    textView.font = regularFont(f: 15)
    textView.setValue(placeholder, forKey: "_placeholderLabel")
    textView.addSubview(placeholder)
    textView.layer.cornerRadius = 5
    textView.clipsToBounds = true
    textView.inputAccessoryView = self.addToolBar()
    self.view.addSubview(textView)        

原文:https://www.cnblogs.com/betty666/p/iOS_textView_placeholder.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!