如何调整图像大小或作为NSAttributedString NSTextAttachment完成(或设置其初始大小)

问题描述 投票:21回答:5

我有一个NSAttributedString我要添加一个NSTextAttachment。图像是50w乘50h,但我希望它缩小以反映属性字符串的行高。我以为这会自动完成,但我猜不会。我查看了UImage类引用,但是这个图像似乎没有在UIImageView中设置,因此无法访问框架属性。这是我目前拥有的截图:

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

谢谢

编辑1

这是我如何创建它:

    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];
ios ios7 uiimage nsattributedstring nstextattachment
5个回答
15
投票

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


40
投票

您应该设置边界表单附件以调整图像大小,如下所示:

attachment.bounds = CGRectMake(0, 0, yourImgWidth, yourImgHeight)


24
投票

如果你需要调整一堆NSTextAttachment图像,同时保持它们的宽高比我写了一个方便的扩展:http://hack.swic.name/convenient-nstextattachment-image-resizing

extension NSTextAttachment {
    func setImageHeight(height: CGFloat) {
        guard let image = image else { return }
        let ratio = image.size.width / image.size.height

        bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: ratio * height, height: height)
    }
}

用法示例:

let textAttachment = NSTextAttachment()
textAttachment.image = UIImage(named: "Image")
textAttachment.setImageHeight(16) // Whatever you need to match your font

let imageString = NSAttributedString(attachment: textAttachment)
yourAttributedString.appendAttributedString(imageString)

2
投票

Swift4,如果你想要一个属性字符串以及图像或图标

在这里你可以做这样的事情。

func AttributedTextwithImgaeSuffixAndPrefix(AttributeImage1 : UIImage , AttributedText : String ,AttributeImage2 : UIImage,  LabelBound : UILabel) -> NSMutableAttributedString
{
    let fullString = NSMutableAttributedString(string: "  ")
    let image1Attachment = NSTextAttachment()
    image1Attachment.bounds = CGRect(x: 0, y: ((LabelBound.font.capHeight) - AttributeImage1.size.height).rounded() / 2, width: AttributeImage1.size.width, height: AttributeImage1.size.height)
    image1Attachment.image = AttributeImage1
    let image1String = NSAttributedString(attachment: image1Attachment)
    let image2Attachment = NSTextAttachment()
    image2Attachment.bounds = CGRect(x: 0, y: ((LabelBound.font.capHeight) - AttributeImage2.size.height).rounded() / 2, width: AttributeImage2.size.width, height: AttributeImage2.size.height)
    image2Attachment.image = AttributeImage2
    let image2String = NSAttributedString(attachment: image2Attachment)
    fullString.append(image1String)
    fullString.append(NSAttributedString(string: AttributedText))
    fullString.append(image2String)
    return fullString
}

你可以使用下面提到的代码:

        self.lblMid.attributedText = AttributedTextwithImgaeSuffixAndPrefix(AttributeImage1: #imageLiteral(resourceName: "Left") , AttributedText: "  Payment Details  ", AttributeImage2: #imageLiteral(resourceName: "RIght") , LabelBound: self.lblMid)

在这里你可以添加图片,你可以用你自己的替换它:

左图enter image description here

正确的图像enter image description here

输出将像这个qazxsw poi


0
投票

NSTextAtatchment只是UIImage的持有者,因此在需要缩放和重新创建文本附件或设置图像时缩放图像。如果更改现有文本附件上的图像,则需要强制执行nslayout更新。

© www.soinside.com 2019 - 2024. All rights reserved.