iOS开发iOS && AndroidiOS进阶指南

如何实现UITextView的自增长--类似qq聊天输入框

2016-03-26  本文已影响1364人  改变自己_now

公司项目中需要实现评论功能,输入框要和qq或微信聊天哪样自增长得输入框。效果大概如下:

yj_demo.gif

很明显这个需要采用UITextView来定制,可是到底要实现呢?

问题1:UITextView没有类似于UITextfield的placeholder的属性

没有,我们就集成UITextView,自己添加一个这样的属性为了区分我把他命名为

@property(nonatomic, copy) NSString *yj_placeholder;

@property(nonatomic, strong) UIColor *yj_placeholderColer;

实现原理就是重写drawRect方法,同事监听输入的变化的通知或者代理,代码如下:

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    
    // 注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextViewTextDidChangeNotification object:nil];
    
    self.tintColor = [UIColor grayColor];
  
}
return self;
}

- (void)dealloc {

[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)textChange:(NSNotification*)notice{


NSLog(@"%@",notice.object);


[self setNeedsDisplay];
}


- (void)setYj_placeholder:(NSString *)yj_placeholder {

_yj_placeholder = yj_placeholder;

[self setNeedsDisplay];
}

- (void)setYj_placeholderColer:(UIColor *)yj_placeholderColer {

_yj_placeholderColer = yj_placeholderColer;

[self setNeedsDisplay];
}

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

[super setFont:font];

[self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect {

if (self.hasText) {
    
    return;
}


NSLog(@"%@",NSStringFromCGRect(rect));


NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = self.font;
attrs[NSForegroundColorAttributeName] = self.yj_placeholderColer?self.yj_placeholderColer:[UIColor grayColor];

CGFloat x = 5;
CGFloat w = rect.size.width - 2*x;
CGFloat y = 8;
CGFloat h = rect.size.height - 2*y;

[self.yj_placeholder drawInRect:CGRectMake(x, y, w, h) withAttributes:attrs];
}
问题二,如何实现自增长?

在- (void)textViewDidChange:(UITextView *)textView方法中计算文字高度的变化,首先计算一行文字高度是多少,当换行时看起高度变化多少,改变所在view的y值和高度,同时也改变自身的高度。

NSString *test = @"我";
CGFloat minH = [test stringHeightWithMaxWidth:_commentInputView.width-10 andFont:_commentInputView.font].height;

说明:上面的方法是我自己为了代码的重用写了一个NSString 的分类,如下:

  /**
 *  根据一定宽度和字体计算高度
 *
 *  @param maxWith 最大宽度
 *  @param font  字体
 *
 *  @return 返回计算好高度的size
 */
- (CGSize)stringHeightWithMaxWidth:(CGFloat)maxWidth andFont:(UIFont*)font ;


/**
 *  根据一定高度和字体计算宽度
 *
 *  @param maxHeight 最大高度
 *  @param font      字体
 *
 *  @return 返回计算宽度的size
 */
- (CGSize)stringWidthWithMaxHeight:(CGFloat)maxHeight andFont:(UIFont*)font;

实现如下:

  - (CGSize)stringHeightWithMaxWidth:(CGFloat)maxWidth andFont:(UIFont*)font  {

   return  [self boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
}

- (CGSize)stringWidthWithMaxHeight:(CGFloat)maxHeight andFont:(UIFont*)font {

return  [self boundingRectWithSize:CGSizeMake(MAXFLOAT, maxHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
}

似乎扯远了,有了minH每一行字的高度,每次技术下文字高度,除以每一行高度,就能计算出多少行。这样我们就能计算y的偏移量了。另外需要一个变量来记录当前所在行

    //获取行数
NSUInteger lines = changeTextHeight/self.minTextHeight;

if (lines>5) {
    //如果大于五行的就是直接返回
    return;
}

if (lines>_currentLine) {
    
    //文字行数增加的时候需要改变UITextView的高度,和该View的y值,让后记录当前行数
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.height+= (lines-_currentLine)*self.minTextHeight;
       
        self.y = self.y - (lines-_currentLine)*self.minTextHeight;
         _currentLine = lines;
        
       
    }];
 
}else if (lines<_currentLine){
    
    //文字删除减少的时候
    [UIView animateWithDuration:0.25 animations:^{
       self.height -= (_currentLine - lines)*self.minTextHeight;
        self.y+= (_currentLine - lines)*self.minTextHeight;
       
        _currentLine = lines;
    }];
 
}

这样就能实现上面的效果了,毕竟能力有限,有错误的地方,谢谢指正!

上一篇 下一篇

猜你喜欢

热点阅读