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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条