如何调整图像大小或作为NSAttributedString NSTextAttachment完成(或设置其初始大小) [英] how to resize an image or done as a NSAttributedString NSTextAttachment (or set its initital size)
问题描述
我有一个NSAttributedString我要添加一个NSTextAttachment。图像是50w乘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?
thx
这是怎么回事我正在创建它:
here's how I'm creating it:
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屋!