UITextView sizeToFit不起作用 [英] UITextView sizeToFit not working

查看:47
本文介绍了UITextView sizeToFit不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直到处都在寻找这个答案很奇怪,但是似乎没有任何效果!

It's weird I've been looking everywhere for this answer but nothing seems to work!

我有一个简单的 UITextView ,我正在尝试根据其内容在viewController中调整其大小:

I have simple UITextView that I'm trying to resize in a viewController according to its contents:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *lorum = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";

    self.textView.text = lorum;

    self.textView.scrollEnabled = NO;
    [self.textView sizeToFit];
    [self.textView layoutIfNeeded];
}

但这就是我最终得到的:

But this is all I end up with:

我缺少明显的东西吗?

已更新(框架背景):

推荐答案

正如我在评论中所写,在我看来,如果您不想使用UITextView滚动,我建议使用一个简单的UILabel ..这是代码:

as i wrote in comment, in my opinion if you dont want use scroll of UITextView i suggest to use a simple UILabel.. this is code:

编辑:如果出于某些原因(例如可编辑的文本或可选的文本)要使用UITextView,则可以使用相同的代码进行一些修改(我将这些修改添加到注释之类的代码中)

if you want use UITextView for some reason like editable text or selectable text, you can use the same code with some modifications (i add this modifications in code like comments)

EDIT2 (如果您的标签在情节提要中),请检查约束条件或删除自动版式

EDIT2 if your label is in storyboard check constraints or remove autolayout

.h

  @interface ViewController : UIViewController {

    IBOutlet UILabel *label;

   }

.m

 - (void)viewDidLoad {
   [super viewDidLoad];

 //  UILabel *label = [[UILabel alloc] init];
   label.backgroundColor = [UIColor lightGrayColor];

   NSString *lorum = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";

   //for use labrect with UITextView you set the font of UITextView:
   //label.font = [UIFont systemFontOfSize:17];       

   CGSize maxSize = CGSizeMake(320, 410);
   CGRect labrect = [lorum boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:Nil];

   label.text = lorum;
   //for use UITextView you should comment the line under
   label.numberOfLines = 0;
   label.frame = CGRectMake(0, 30, 320, labrect.size.height);

 //  [self.view addSubview:label];

  };

这篇关于UITextView sizeToFit不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆