textview 设置placeHolder

通过运行时,发现UITextView有一个叫做“_placeHolderLabel”的私有变量,

 Ivar *ivars = class_copyIvarList([UITextView class], &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        const char *name = ivar_getName(ivar);
        NSString *objcName = [NSString stringWithUTF8String:name];
        NSLog(@"%d : %@",i,objcName);
    }

so

- (UITextView *)reasonTextView {
    if (!_reasonTextView) {
        _reasonTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, self.addReason.bottom + 20, kScreenW - 20, 150)];
        [_reasonTextView setBackgroundColor:RGBOF(0xF3F3F4)];
        
        UILabel *placeHolderLabel = [[UILabel alloc] init];
        placeHolderLabel.text = @"输入你想加入此家族的原因,最少5个字";
        placeHolderLabel.numberOfLines = 0;
        placeHolderLabel.textColor = [UIColor lightGrayColor];
        [placeHolderLabel sizeToFit];
        [_reasonTextView addSubview:placeHolderLabel];
        
        // same font
        _reasonTextView.font = [UIFont systemFontOfSize:16.f];
        placeHolderLabel.font = [UIFont systemFontOfSize:16.f];
        
        [_reasonTextView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
        _reasonTextView.delegate = self;
    }
    return _reasonTextView;
}

你可能感兴趣的:(textview 设置placeHolder)