0

我想在我的 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);

那么,请问如何动态设置高度?非常感谢

4

2 回答 2

1

您是否尝试过使用

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

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

于 2013-06-06T12:54:05.277 回答
0

尝试使用这个

  - (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;
}
于 2013-06-06T12:54:02.293 回答