2016-08-12 3 views
0

최대 너비와 최소 높이를 가진 UITextView를 만들고 싶습니다. 우리 아이폰의 "message"앱에서 채팅 버블처럼. sizeThatFits을 사용해 보았습니다. 그러나 멋진 크기는주지 못했습니다. 내가 아는UITextView에서 너비와 높이를 제한하는 방법은 무엇입니까?

NSString *string = @"hello everyone"; 
_textView = [[UITextView alloc] init]; 
_textView.font = [UIFont systemFontOfSize:16]; 
_textView.text = string; 
[self.view addSubview:_textView]; 

CGSize size = [string sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}]; 
CGFloat finalWidth = MIN(250, size.width); 
CGSize constraintSize = CGSizeMake(finalWidth, MAXFLOAT); 
CGSize finalSize = [_textView sizeThatFits:constraintSize]; 
_textView.frame = CGRectMake(0, 0, finalSize.width, finalSize.height); 

내가 제대로 UITextView의 폭과 높이를 제한 할 수있는 좋은 방법을 생각해 내 code.So에 문제가있을 수 있습니다. 감사


친구들은 내가하려고 Autolayout.Then를 사용 말해 it.Here 내 코드

[_textView mas_makeConstraints:^(MASConstraintMaker *maker){ 
    maker.right.equalTo(self.mas_left); 
    maker.top.equalTo(self.mas_top); 
    maker.width.lessThanOrEqualTo(@(250)); 
    maker.height.greaterThanOrEqualTo(@(40)); 
}]; 

입니다하지만 [_textView sizeToFit]를 사용하지 않으면 작동하지 않습니다 제약 조건에 문제가 있습니까? 나는 그 내용에 따라 textView 자체를 조정하고 싶습니다!

+1

UITextView에 제약 조건을 추가하여 Autolayout을 사용하십시오. 마술은 코드를 작성하지 않고 일어날 것입니다. :) – Harsh

+0

당신은 autolayout을 사용해야합니다! – Lion

답변

-1
let minTextViewHeight = CGFloat(33) 
    let maxTextViewHeight = CGFloat(70) 
    @IBOutlet var textViewHeightConst: NSLayoutConstraint! 
    func textViewDidChange(textView: UITextView) { 
      //TEXTVIEW HEIGHT INCREASE WHILE TYPING 
      var height = ceil(txtViewChat.contentSize.height) // ceil to avoid decimal 

      if (height < minTextViewHeight) { 
       height = minTextViewHeight 
      } 
      if (height > maxTextViewHeight) { 
       height = maxTextViewHeight 
      } 
      if height != textViewHeightConst.constant { 
       textViewHeightConst.constant = height 
       } 
     } 
+0

자동 사용! – Harsh

관련 문제