自定义textView的placeHolder被拉伸怎么办 20C

自定义textVIew的placeHolder(灰色文字部分)用quartz2D绘制
我监听键盘的高度 然后在监听方法里面改变textView高度 键盘弹出 textView的高度变小 键盘收回 高度变大
但是,键盘收缩,placeHolder的文字,会被短时间拉伸压缩 然后恢复正常 怎么办? 图片说明

 textView.m
 -(void)setPlaceHolder:(NSString *)placeHolder{
    _placeHolder=placeHolder;
    self.change=YES;
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect {

    //设置textView代理
    self.delegate=self;
    self.backgroundColor=[UIColor whiteColor];
    //判断输入内容
    NSString *str;
    if ((self.isChange==YES)&&(self.isClean==NO)) {
        str=self.placeHolder;
        //NSLog(@"1");
    }

    if ((self.isChange==NO)&&(self.isClean==NO)) {
        str=@"请输入内容";
        //NSLog(@"2");
    }

    if (self.isClean==YES) {
        //NSLog(@"3");
        str=@"";
    }

    //绘制画文字
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    // NSForegroundColorAttributeName : 文字颜色
    // NSFontAttributeName : 字体
    attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    if (self.fontSize) {
        attrs[NSFontAttributeName] = [UIFont systemFontOfSize:self.fontSize];
    }else{
        attrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    }
    [str drawAtPoint:CGPointMake(5, 8) withAttributes:attrs];
}

#pragma mark - 监控textView开始
-(void)textViewDidChange:(UITextView *)textView{
    //NSLog(@"4");
    self.clean=YES;
    if (self.text.length==0) {
        self.clean=NO;
    }
    [self setNeedsDisplay];
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    //清空placeholder
   // NSLog(@"5");
    self.clean=YES;
    if (self.text.length==0) {
        self.clean=NO;
    }
    [self setNeedsDisplay];

}


#pragma mark - 监控textView结束
-(void)textViewDidEndEditing:(UITextView *)textView{


    if (self.text.length==0) {
        //NSLog(@"6");
        self.clean=NO;
    }
    [self setNeedsDisplay];

}

//controller.m

 - (void)viewDidLoad {
    [super viewDidLoad];
    self.placeHolderTextView.delegate=self;
    //隐藏tabbar
    self.tabBarController.tabBar.hidden=YES;

    //创建textView
    self.placeHolderTextView=[[IWTextView alloc]init];
    CGFloat textViewX=0;
    CGFloat textViewY=CGRectGetMaxY(self.timeLabel.frame);
    CGFloat textViewW=self.view.bounds.size.width;
    CGFloat textViewH=self.view.bounds.size.height-textViewY;
    self.placeHolderTextView.frame=CGRectMake(textViewX,textViewY, textViewW, textViewH);

    //添加进父类
    [self.view addSubview:self.placeHolderTextView];

    //设置placeHolder
    self.placeHolderTextView.placeholder=@"请输入备忘事项";

    //监听键盘的通知
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

//    MemoModel *model=[[MemoModel alloc]init];
//    model.title=self.placeHolderTextView.text;
//    model.date=self.timeLabel.text;
//    NSLog(@"123");
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    //键盘弹出
    [self.placeHolderTextView becomeFirstResponder];

}

#pragma mark - 键盘frame
- (void)KeyboardWillShow:(NSNotification *)notification
{
    //获取键盘frame
    CGRect keyboardFrame=[notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    //设置textView的frame
    CGRect textViewFrame=self.placeHolderTextView.frame;
    textViewFrame.size.height=CGRectGetMinY(keyboardFrame)-CGRectGetMaxY(self.timeLabel.frame);
    self.placeHolderTextView.frame=textViewFrame;
    NSLog(@"small%@",NSStringFromCGRect(self.placeHolderTextView.frame));

}

- (IBAction)FinishAction:(UIBarButtonItem *)sender {
    //键盘退出
    [self.view endEditing:YES];

}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问