调整 NSAttributedString 中使用的图像大小

Resize an image that is used in NSAttributedString

Possible duplicate but the accepted answer below is a simple one-function solution.

我正在尝试制作新闻文章详细信息视图控制器,以在顶部显示文章的图片,然后显示日期,然后显示详细说明。

我了解到我必须使用 NSAttributedString,而且只有一个 UITextView 才能做到这一点。现在一切正常,但我现在只有一个问题:图像宽度不适合屏幕宽度(或 TextView 宽度),请参见下图:

我什么都试过了,还是不行。

如何使图像适合屏幕宽度?

提前致谢。

再次编辑 这是涉及的代码:

#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

哦对了,所以 NSTextAttachment 没有图像视图...啊... 好吧,然后想到另一个想法。如何调整 UIImage 数据,然后将该图像传递给 NSTextAttachment,就像这样 post:

使用此方法相应地调整图像大小:

+ (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;
}