4

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:

App Screenshot

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
Community
  • 1
  • 1
ANA
  • 272
  • 2
  • 16
  • Set the ```UIImageView``` property to ```Scale to Fit```. –  May 12 '15 at 13:49
  • i did all this view programmatically, using NSAttributedString, not storyboard – ANA May 12 '15 at 13:49
  • i need a programmatic solution – ANA May 12 '15 at 13:50
  • Ok so then show us your code. I can't help you if I can't see whats wrong with your code. –  May 12 '15 at 13:50
  • @dan okay can you please look at my code – ANA May 12 '15 at 13:55
  • Yeah had a look at it. You need to then pragmatically set the aspect fit property of that image view. Try this: ```textAttachment.imageView.contentMode = UIViewContentModeScaleAspectFit;```. –  May 12 '15 at 13:57
  • @Lance exactly, so what to do ? – ANA May 12 '15 at 14:11
  • possible duplicate of [how to resize an image or done as a NSAttributedString NSTextAttachment (or set its initital size)](http://stackoverflow.com/questions/22357171/how-to-resize-an-image-or-done-as-a-nsattributedstring-nstextattachment-or-set) – pNre May 12 '15 at 14:58

1 Answers1

0

Oh right, so NSTextAttachment doesn't have image views... ah... Right well then another idea comes to mind. How about reszing the UIImage data and then passing that image to the NSTextAttachment, like in this post:

https://stackoverflow.com/a/2658801/4657588

Make use of this method to resize the image accordingly:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}
Community
  • 1
  • 1
  • `Property imageView not found on object of type NSTextAttachment` – ANA May 12 '15 at 14:07
  • because there's no image view – ANA May 12 '15 at 14:11
  • NSTextAttachment doesn't have an `imageView` property – Lance May 12 '15 at 14:11
  • @ANA ok so in that case are you adding the image view in pragmatically yourself? There must be somewhere in your code where you are doing something a long the lines of ```[self.view addSubview:myImageView]```?? –  May 12 '15 at 14:16
  • @ANA show me the code you are using to add in the image view to your view. You are probably doing it in ```viewDidLoad``` or some method like that. Show me that code. I may be able to help then. –  May 12 '15 at 14:17
  • @Dan There is no imageView, that's the idea of `NSAttributedString`, let me edit the code to show you. – ANA May 12 '15 at 14:25
  • @Dan ok i tried it, the one in the link it works, but how to make it fit the screen, because there are several iphone screen widths ? – ANA May 12 '15 at 14:43
  • @Dan Actually, it worked on all screens i tested it, thanks, if you can edit your answer so i can accept it :) – ANA May 12 '15 at 14:49
  • @ANA That's great to hear! I updated my answer too :) –  May 12 '15 at 14:51
  • @Dan okay and delete the original code you posted for people to benefit from your second correct answer :) – ANA May 12 '15 at 14:53