简体   繁体   English

改变`UITextView`的字体大小,就像'UILabel'一样setAdjustsFontSizeToFitWidth

[英]changing font size of `UITextView` same like `UILabel's` setAdjustsFontSizeToFitWidth

I want to change font size of the UITextView as per text length. 我想根据文本长度更改UITextView字体大小。 So if the text length is very big than Table cell then font size should become smaller. 因此,如果文本长度比表格单元格大,那么字体大小应该变小。 I can use UILabel's setAdjustsFontSizeToFitWidth which will work fine but as to keep the project code with minimal changes i don't want to change it to UILabel . 我可以使用UILabel's setAdjustsFontSizeToFitWidth,它可以正常工作但是为了保持项目代码的最小变化,我不想将其更改为UILabel Thank You in advance. 先感谢您。

To make a dynamically sizing UITextView inside a UITableViewCell, 要在UITableViewCell内部动态调整大小UITextView,

  • Add a UITextView to a UITableViewCell and add constraint to the sides 将UITextView添加到UITableViewCell并向边添加约束
  • Set the UITextView's scrollEnabled property to NO. 将UITextView的scrollEnabled属性设置为NO。

Use AutoDimension property - 使用AutoDimension属性 -

tableView.estimatedRowHeight = 150;
tableView.rowHeight = UITableViewAutomaticDimension;

If UITextView not editable that's it you need to do. 如果UITextView不可编辑,那就是你需要做的。

If you're allowing users to edit the UITextView, you also need to: Implement the textViewDidChange method of the UITextViewDelegate protocol as below - 如果您允许用户编辑UITextView,您还需要:实现UITextViewDelegate协议的textViewDidChange方法,如下所示 -

 -(void)textViewDidChange:(UITextView *)textView;
{
    [tableView beginUpdates];
    [tableView endUpdates];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM