Possible duplicate but the accepted answer below is a simple one-function solution.
I'm trying to make a news article details view controller to display the article's image at the top then date then a long description.
I learned that i have to use NSAttributedString
and only one UITextView
in order to do that. Now everything works fine but i have only one issue now: the image width is not fitting the screen width (or the TextView width), please see this image below:
I tried everything, nothing works.
How can i make the image fit screen width ?
Thanks in advance.
EDITED AGAIN This is the code that is involved:
#import "SingleNewsViewController.h"
@interface SingleNewsViewController ()
@end
@implementation SingleNewsViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSAttributedString *image = [self imageString];
NSAttributedString *date = [self dateString];
NSAttributedString *body = [self bodyString];
NSMutableAttributedString *wholeStory = [NSMutableAttributedString new];
// TODO: can you be sure image, date and body are all non-nil?
NSArray *allComponents = @[image, date, body];
for(NSAttributedString *component in allComponents)
{
[wholeStory appendAttributedString:component];
if(component != [allComponents lastObject])
[[wholeStory mutableString] appendString:@"\n\n"];
}
self.tv.attributedText = wholeStory;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIImage *)image
{
return [UIImage imageNamed:@"latest_news_img.jpg"];
}
- (NSString *)dateText
{
return @"Hi There, this is date!";
}
- (NSString *)bodyText
{
return @"Hi There, this is body text!Hi There, .....";
}
- (NSAttributedString *)imageString
{
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [self image];
return [NSAttributedString attributedStringWithAttachment:textAttachment];
}
- (NSAttributedString *)dateString
{
return [[NSAttributedString alloc]
initWithString:[self dateText]
/*attributes:
@{
NSFontAttributeName: [UIFont preferredFontForTextStyle: UIFontTextStyleSubheadline],
... etc ...
}*/];
}
- (NSAttributedString *)bodyString
{
return [[NSAttributedString alloc]
initWithString:[self bodyText]
/*attributes:
@{
NSFontAttributeName: [UIFont preferredFontForTextStyle: UIFontTextStyleBody],
... etc ...
}*/];
}
@end