cvbcvb 2015-10-17 16:45 采纳率: 25%
浏览 1571
已结题

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

自定义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条回答 默认 最新

  • devmiao 2015-10-17 22:12
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料