UITextView的子类不可编辑

时间:2017-06-09 12:31:18

标签: ios uitextview

我创建了一个名为SWTextView的类,它继承自UITextView。它除了添加占位符标签外什么都不做。当我初始化SWTextView并将其放在UITableViewCell中时,即使SWTextView的editable属性为YES,它也不可编辑。有没有人遇到过同样的问题?

SWTextView如下:

#import <UIKit/UIKit.h>

@interface SWTextView : UITextView

- (void)setPlaceholder:(NSString *)placeholder;

@end

@interface SWTextView ()

@property(nonatomic, strong) UILabel *placeholderLabel;

@end

@implementation SWTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self = [super initWithFrame:frame]) {
        _placeholderLabel = [[UILabel alloc] init];
        _placeholderLabel.textColor = [UIColor grayColor];
        [self addSubview:_placeholderLabel];

        self.font = [UIFont systemFontOfSize:12];
    }

    return self;
}

- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    _placeholderLabel.font = font;
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholderLabel.text = placeholder;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    [_placeholderLabel sizeToFit];
}

@end

0 个答案:

没有答案