iOS 为 textView 添加 placeholder
阅读原文时间:2023年07月10日阅读:2

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)

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章