ios - 调整 NSAttributedString 中使用的图像大小

标签 ios objective-c nsattributedstring

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

我正在尝试制作一个新闻文章详细信息 View Controller ,以在顶部显示文章的图片,然后显示日期,然后是详细说明。

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

App Screenshot

我什么都试过了,没用。

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

提前致谢。

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

#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 没有 ImageView ……啊……好吧,我又想到了另一个想法。如何调整 UIImage 数据,然后将该图像传递给 NSTextAttachment,就像这篇文章中那样:

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

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

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

关于ios - 调整 NSAttributedString 中使用的图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30192842/

相关文章:

objective-c - iOS:标记错误

iphone - 即使正确设置了 NSTimezone,仍然存在 NSDateFormatter 结果问题,为什么?

ios - 从 NSAttributedString 中提取图像

ios - 将 UIScrollView 添加到 CCSprite

ios - 更新过程中应用程序沙箱中的哪些目录被删除

ios - 设置 View 框架并转换另一个 View - 奇怪的行为

objective-c - MsgPack 无法消息打包 NSData 对象

ios - UITextView 保存属性文本

ios - NSMutableAttributedString 中的 Swift 替换字体大小

ios - 用于IOS标记的Google Maps SDK已被覆盖