如何调整图像大小或作为 NSAttributedString NSTextAttachment 完成(或设置其初始大小) [英] how to resize an image or done as a NSAttributedString NSTextAttachment (or set its initital size)

查看:18
本文介绍了如何调整图像大小或作为 NSAttributedString NSTextAttachment 完成(或设置其初始大小)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSAttributedString,我正在向其中添加 NSTextAttachment.图像是 50w x 50h,但我希望它缩小以反映属性字符串的行高.我以为这会自动完成,但我想不会.我查看了 UImage 类参考,但该图像似乎没有在 UIImageView 中设置,因此无法访问框架属性.这是我目前拥有的屏幕截图:

I have a NSAttributedString to which I am adding a NSTextAttachment. The image is 50w by 50h but I'd like it to scale down to reflect the line height of the attributed string. I thought this would be done automatically but I guess not. I have looked at the UImage class reference but this image doesn't seem to be set in a UIImageView so no access to a frame property. Here's a screenshot of what I currently have:

在理想的世界中,我还想实现一种根据用户输入(例如增加字体大小)来放大图像的方法.关于如何实现这一目标的任何想法?

In an ideal world, I would also like to implement a way to scale up the image based upon user input (such as increasing the font size). Any ideas on how to achieve this?

谢谢

这是我创建它的方式:

    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"note-small.png"];
    NSLog(@"here is the scale: %f", textAttachment.image.scale);
    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [headerAS replaceCharactersInRange:NSMakeRange([headerAS length], 0) withString:@" "];
    [headerAS replaceCharactersInRange:NSMakeRange([headerAS length], 0) withAttributedString:attrStringWithImage];

推荐答案

查看子类化 NSTextAttachment 并实现 NSTextAttachmentContainer 方法以根据提供的文本容器返回不同的大小.默认情况下,NSTextAttachment 只返回它提供的图像的大小.

Look at subclassing NSTextAttachment and implementing the NSTextAttachmentContainer methods to return different sizes based on the text container supplied. By default, NSTextAttachment just returns the size of the image it is provided with.

这篇关于如何调整图像大小或作为 NSAttributedString NSTextAttachment 完成(或设置其初始大小)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆