Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何动态设置uitextview的高度_Iphone_Ios_Height_Uitextview - Fatal编程技术网

Iphone 如何动态设置uitextview的高度

Iphone 如何动态设置uitextview的高度,iphone,ios,height,uitextview,Iphone,Ios,Height,Uitextview,我想在UITextView上动态设置高度,但它不起作用。我的代码是: [super viewDidLoad]; DTAttributedTextView *_textView; contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)]; _textView = [[DTAttributedTextView alloc] initWithFrame:CGRect

我想在UITextView上动态设置高度,但它不起作用。我的代码是:

 [super viewDidLoad];
 DTAttributedTextView *_textView;
contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
_textView = [[DTAttributedTextView alloc] initWithFrame:CGRectMake(0.0, 80.0, self.view.frame.size.width, self.view.frame.size.height)];

UIImage *backgroundImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.epnet.fr%@",[[[currentDicoNew valueForKey:@"image"] valueForKey:@"thumb_rect"] valueForKey:@"url"]]]]];
imageNew = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 320, 178)];
imageNew.image = backgroundImage;
imageNew.contentMode = UIViewContentModeScaleAspectFill;

CGSize maxImageSize = CGSizeMake(self.view.bounds.size.width - 20.0, self.view.bounds.size.height);
NSString *html = [SundownWrapper convertMarkdownString:[currentDicoNew valueForKey:@"content"]];
NSData *HTMLData = [html dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{ DTDefaultFontFamily : @"Helvetica",
                           DTDefaultFontSize : [NSNumber numberWithFloat:14.0],
                           DTDefaultLinkColor:[UIColor colorWithRed:0.0/255.0 green:174.0/255.0 blue:239.0/255.0 alpha:1],
                           DTMaxImageSize : [NSValue valueWithCGSize:maxImageSize],
                        };
NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTMLData:HTMLData options:options documentAttributes:nil];

_textView.attributedString = attrString;
_textView.shouldDrawImages = YES;
_textView.shouldDrawLinks = YES;
_textView.textDelegate = self;
[_textView setScrollIndicatorInsets:UIEdgeInsetsMake(0, 0, 44, 0)];
_textView.contentInset = UIEdgeInsetsMake(10, 10, 54, 10);
[_textView.attributedTextContentView setNeedsDisplay];
[_textView setScrollEnabled:NO];
[contentView addSubview:_textView];

MDCParallaxView *parallaxView = [[MDCParallaxView alloc] initWithBackgroundView:imageNew
                                                                 foregroundView:contentView];
parallaxView.frame = CGRectMake(0, 40, 320, self.view.frame.size.height);
parallaxView.backgroundHeight = 100;
parallaxView.scrollView.scrollsToTop = YES;
parallaxView.backgroundInteractionEnabled = YES;
parallaxView.scrollViewDelegate = self;

[self.view addSubview:parallaxView];
高度等于420(self.view.frame.size.height),但我想知道如何检索动态内容的高度。因为,当我在viewDidDisapear中编写此代码时,它可以工作,但不能在ViewDidLoad中工作:

CGRect frame1;
frame1 = _textView.frame;
frame1.size.height = [_textView contentSize].height;
_textView.frame = frame1;
NSLog(@"%f", frame1.size.height);
那么,我如何动态设置高度呢?非常感谢

试着用这个

  - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    CGSize size = [txtView.text sizeWithFont:txtView.font];
    CGRect f = txtView.frame;
    f.size.height = ceil(size.width/f.size.width)*size.height;
    txtView.frame = f;
}
你试过使用吗

-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size

这将返回适合textview的文本大小。您可以将该大小设置为文本视图。

已经用
[\u textview sizeToFit]尝试过了?是的。不想工作..谢谢,我不能这样做,因为它是DTAttributedTextView文本视图,因为文本已解析。和.text属性是不允许的…k…所以不要实现它,我也不知道DTAttributedTextView.Ty。我如何使用它?知道它实际上不是UITextView,而是DTAttributedTextView,因为文本是经过解析的。@Benjamin为什么不能使用
attributeString
?您可以使用此方法,因为DTAttributedTextView中有图像。。。这个方法只支持字符串。。。但为什么在视图中,这是正确的<代码>CGRect frame1;frame1=_textView.frame;frame1.size.height=[[u textView contentSize].height_textView.frame=frame1;NSLog(@“%f”,frame1.大小.高度)我没有使用该类,但如果它在viewDidLoad中不起作用,可能是因为您的视图仍在加载,请尝试将代码放入一个方法中,并使用performSelector:在稍有延迟后调用它。@Mat是的,它会运行,但视图已使用self.view.frame.height加载。。我能在2秒后把它换成好的高度吗_textView=[[DTAttributedTextView alloc]initWithFrame:CGRectMake(0.0,80.0,self.view.frame.size.width,self.view.frame.size.height)];