動的な高さのスクロールビューがあります。高さは、コア データベースから取得した画像とテキストによって異なります。私は次のものを持っています。
私のViewDidLoad
中で私は自分のメソッドを呼び出しますgetNewsItem
。
メソッドは次のようになります。
-(void)getNewsItem{
NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"News"];
NSLog(@"news ID IN METHOD %d",_newsId);
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"new_id = %d",_newsId];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"new_id" ascending:YES];
fetchRequest.sortDescriptors = @[descriptor];
NSArray *matches = [context executeFetchRequest:fetchRequest error:nil];
newsItem = [matches objectAtIndex:0];
lblTitle.text = newsItem.pnt_new_title;
lblTitle.font = [UIFont fontWithName:@"ShadowsIntoLightTwo-Regular" size:16.0];
lblTitle.textColor = [UIColor whiteColor];
NSString *datetext = [newsItem.new_date substringWithRange:NSMakeRange(0, 11)];
lblDate.text = datetext;
lblDate.font = [UIFont fontWithName:@"TitilliumWeb-Regular" size:15.0];
NSString *imgUrl = @"";
imgUrl = [NSString stringWithFormat:@"http://mosaqua.appmax.be/%@",newsItem.new_imgurlbig];
NSData *imgData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imgUrl]];
UIImage *image = [UIImage imageWithData:imgData];
self.img_Picture.image = [UIImage imageWithData:imgData];
float ratioImage = image.size.width/image.size.height;
float ratioPlaceholder = 320/206;
float width;
float heigth;
float x;
if(ratioImage < ratioPlaceholder){
width = 206 * ratioImage;
heigth = 206;
}else{
width = 320;
heigth = 320/ratioImage;
}
if (heigth < self.view.bounds.size.width){
x = (self.view.bounds.size.width - width) / 2;
}else{
x = 0;
}
NSLog(@"x is %f",x);
if(heigth < 206){
self.img_titlback.frame= CGRectMake(-4, heigth-8, self.view.bounds.size.width+5, 45);
self.lblTitle.frame = CGRectMake(40, heigth-8, self.view.bounds.size.width, 40);
self.btnBack.frame = CGRectMake(5, heigth, 67, 28);
self.lblDate.frame = CGRectMake(7, heigth+30, self.view.bounds.size.width, 45);
[self updateScrollView:newsItem.pnt_new_description andHeight:heigth+60];
}else{
[self updateScrollView:newsItem.pnt_new_description andHeight:300];
}
self.img_Picture.frame = CGRectMake(x, 0, width, heigth);
}
ウェブビュー方式
-(void)updateScrollView:(NSString *)text andHeight:(float)height{
NSLog(@"till here in scrollview");
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, height,self.view.bounds.size.width-30, 20)];
[[webView scrollView] setBounces: NO];
webView.delegate = self;
[scrollView addSubview:webView];
[self setWebDescription:webView];
}