【问题标题】:Autosize UITextView dependent on text [duplicate]根据文本自动调整 UITextView [重复]
【发布时间】:2013-06-11 02:17:44
【问题描述】:

我想根据文本大小调整我的 UITextView,

CGRect frame = _text.frame;
frame.size.height = (_text.contentSize.height);
frame.size.width = _text.contentSize.width;
_text.frame = frame;

我已经尝试过这段代码,但问题是即使有 5 行文本,它也只显示 2 行(您可以滚动查看其余文本)。如何解决它只显示 5 行?

【问题讨论】:

  • question 可能重复,它是 answer
  • 为什么你没有按照上面提到的方法!

标签: iphone ios objective-c


【解决方案1】:

使用此代码

UIFont font = _text.font;
CGSize textsize = [_text.text sizeWithFont:font constrainedToSize:CGSizeMake(_text.frame.size.width,999) lineBreakMode: NSLineBreakByTruncatingTail]; 

[_text setFrame:CGRectMake(_text.frame.origin.x,_text.frame.origin.y,_text.frame.size.width, textsize.height)];

【讨论】:

  • UILineBreakModeTailTruncation 在 iOS6 中已弃用 ...
  • 我将使用 NSLineBreakByTruncatingTail 代替
  • @Shinonuma 好的,谢谢。
  • 但是这不起作用,它现在只显示 1 行文本和下一行的一小段...:\
  • 改用自动换行或字符换行。
猜你喜欢
  • 1970-01-01
  • 2015-08-27
  • 2021-06-28
  • 2013-04-09
  • 2014-06-09
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 2014-05-04
相关资源
最近更新 更多