UITextView为什么没有占位文字 还需要自己写

所以我已经写好了

.m

import "CustomTextView.h"

#import "UIView+Extension.h"   // 这个是UIView的setget方法设置xywh属性的 相信大家项目里都有这样一个分类

#define DefaultColor [UIColor grayColor]

@interface JXCustomTextView ()

@property (nonatomic, strong) UILabel * placeHolderLabel;

@property (nonatomic, assign) UIEdgeInsets placeHolderLabelInsets;

@end

@implementation JXCustomTextView

- (void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:nil];

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidEndEditingNotification object:nil];

}

- (instancetype)initWithCoder:(NSCoder*)aDecoder{

    if(self= [superinitWithCoder:aDecoder]) {

        [selfsetUpView];

    }

    return self;

}

- (instancetype)initWithFrame:(CGRect)frame{

    if(self= [superinitWithFrame:frame]) {

        [selfsetUpView];

    }

    return self;

}

#pragma mark---setter

- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset{

    //调整text内容边距

    [supersetTextContainerInset:textContainerInset];

    self.placeHolderLabelInsets=UIEdgeInsetsMake(textContainerInset.top, textContainerInset.left+2, textContainerInset.bottom, textContainerInset.right+2);

    [self setNeedsLayout];

}

- (void)setText:(NSString*)text{

    [supersetText:text];

    [self textViewDidChange:nil];

}

- (void)setFont:(UIFont*)font{

    [supersetFont:font];

    self.placeHolderLabel.font  = font;

}

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

    _placeholder= placeholder;

    self.placeHolderLabel.text = _placeholder;

}

- (void)setPlaceholderColor:(UIColor*)placeholderColor{

    _placeholderColor= placeholderColor;

    self.placeHolderLabel.textColor = _placeholderColor;

}

- (void)setIsAutoHeight:(BOOL)isAutoHeight{

    _isAutoHeight= isAutoHeight;

    if (_isAutoHeight) {

        self.scrollEnabled=NO;

    }

}

- (void)setPlaceHolderLabelInsets:(UIEdgeInsets)placeHolderLabelInsets{

    _placeHolderLabelInsets= placeHolderLabelInsets;

    [self layoutSubviews];

}

- (void)resetPlaceHolderLabelState{

    if([selfhasText]) {

        self.placeHolderLabel.hidden = YES;

    }else{

        self.placeHolderLabel.hidden = NO;

    }

}

#pragma mark---UI

- (void)setUpView{

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];

    _placeHolderLabel = [UILabel new];

    _placeHolderLabel.textColor = self.placeholderColor ? self.placeholderColor : DefaultColor;

    _placeHolderLabel.text = self.placeholder;

    _placeHolderLabel.font = self.font;

    _placeHolderLabel.numberOfLines = 0;

    [self addSubview:_placeHolderLabel];



    [self setPlaceHolderLabelInsets:UIEdgeInsetsMake(8, 2, 8, 2)];

    self.font = [UIFont systemFontOfSize:17];

    self.isAutoHeight = YES;

    self.maxHeight=300;

    self.minHeight=30;

}

- (void)layoutSubviews{

    [super layoutSubviews];

    _placeHolderLabel.frame = CGRectMake(_placeHolderLabelInsets.left, _placeHolderLabelInsets.top, self.frame.size.width - _placeHolderLabelInsets.left - _placeHolderLabelInsets.right, 0);

    [_placeHolderLabel sizeToFit];

    if (_placeHolderLabel.hidden == NO && self.isAutoHeight) {

        CGFloat placeHolderHeight = _placeHolderLabel.height + (_placeHolderLabelInsets.top + _placeHolderLabelInsets.bottom);

        if(placeHolderHeight >self.height) {

            self.height= placeHolderHeight;

        }

    }



}

#pragma mark ---NSNotificationEvent

- (void)textViewDidEndEditing:(NSNotification*)notification{



}

- (void)textViewDidChange:(NSNotification*)notification{

    if(self.text.length==0) {

        self.placeHolderLabel.text = _placeholder;

        self.placeHolderLabel.hidden = NO;

        [self setNeedsLayout];

    }else{

        self.placeHolderLabel.hidden = YES;

    }

    if(self.markedTextRange == nil){

        //没有候选字符

        [self st_setAttributedString];

        self.textDidChangedBlock ? self.textDidChangedBlock(self.text) : nil;

    };



    [self st_autoHeight];

    [self resetPlaceHolderLabelState];



}

#pragma mark--- function

//设置属性字符串

- (void)st_setAttributedString{

    //设置了间距

    if (self.verticalSpacing > 0) {

        if(self.text.length>0) {

            NSRangerange =self.selectedRange;

            self.attributedText= [[NSAttributedStringalloc]initWithString:self.textattributes:[selfattrs]];

            self.selectedRange= range;

        }

    }

}

- (NSDictionary *)attrs{

    if (self.verticalSpacing > 0) {

        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

        paragraphStyle.lineSpacing=self.verticalSpacing;// 字体的行间距

        NSDictionary *attributes = @{NSFontAttributeName:self.font,NSParagraphStyleAttributeName:paragraphStyle};

        returnattributes;

    }

    return nil;

}

- (void)st_autoHeight{

    //是否开启

    CGFloatheight =0;;

    if (self.isAutoHeight) {

        if(self.text.length==0){

            height =self.minHeight;

            self.height=self.minHeight;

            //高度变化

            self.textViewAutoHeight ? self.textViewAutoHeight(self.height) : nil;

        }else{

            height = [selfatt_height];

            if(height !=self.height) {

                if(height >self.maxHeight) {

                    self.height=self.maxHeight;

                    self.scrollEnabled=YES;

                }elseif(height <self.minHeight){

                    self.height=self.minHeight;

                    self.scrollEnabled=NO;

                }else{

                    self.scrollEnabled=NO;

                    self.height= height;

                }

                //高度变化

                self.textViewAutoHeight ? self.textViewAutoHeight(self.height) : nil;

            }

        }

    }

}

//计算高度

- (CGFloat)att_height{

    @autoreleasepool{

        UITextView* tempTV = [UITextViewnew];

        tempTV.font=self.font;

        tempTV.textContainerInset = self.textContainerInset;

        if([selfattrs]) {

            tempTV.attributedText= [[NSAttributedStringalloc]initWithString:self.textattributes:[selfattrs]];

        }else{

            tempTV.text=self.text;

        }

        CGFloatheight =  [tempTVsizeThatFits:CGSizeMake(self.width,MAXFLOAT)].height;

        returnheight;

    }

}

@end

.h

@interface CustomTextView : UITextView

/** 占位文字 */

@property (nonatomic, copy) NSString *placeholder;

/** 占位文字颜色 */

@property (nonatomic, strong) UIColor *placeholderColor;

/** 行间距 */

@property (nonatomic, assign) CGFloat verticalSpacing;

/**设置最大高度*/

@property (nonatomic, assign) CGFloat maxHeight;

/**设置最小高度*/

@property (nonatomic, assign) CGFloat minHeight;

/**是不是自适应高度,默认为YES*/

@property (nonatomic, assign) BOOL isAutoHeight;

@property (nonatomic, copy) void(^textDidChangedBlock)(NSString * text);

@property (nonatomic, copy) void (^textViewAutoHeight)(CGFloat textHeight);

@end



/// 用法

- (CustomTextView *)themeTextView {

    if (!_themeTextView) {

        _themeTextView= [[CustomTextView alloc] init];

        _themeTextView.placeholder = @"添加日程、会议、活动主题";

        _themeTextView.font = [UIFont boldSystemFontOfSize:16];

        _themeTextView.verticalSpacing = 2;

        _themeTextView.textContainerInset=UIEdgeInsetsMake(15,10,15,10);

        _themeTextView.maxHeight = 80; // TextView会自动撑开,设置最大高度就会撑到这个高度就不会再撑开了,就会向上滚动

        _themeTextView.minHeight = 80; // 最小高度  也就是默认高度 

        _themeTextView.backgroundColor= [UIColor whiteColor];

        _themeTextView.text = @"";

        _themeTextView.textViewAutoHeight = ^(CGFloat height){

        };

    }

    return  _themeTextView;

}

goodBay

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容