我想在我的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);
那么,我怎么能动态设置高度呢?非常感谢
答案 0 :(得分:1)
您是否尝试过使用
-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
这将返回文本视图中适合的文本大小。您可以将该大小设置为textview。
答案 1 :(得分: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;
}