【问题标题】:how to resize an image or done as a NSAttributedString NSTextAttachment (or set its initital size)如何调整图像大小或作为 NSAttributedString NSTextAttachment 完成(或设置其初始大小)
【发布时间】:2014-03-12 16:02:21
【问题描述】:

我有一个 NSAttributedString,我正在向其中添加一个 NSTextAttachment。该图像是 50w x 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


【解决方案1】:

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

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

【讨论】:

  • 完美运行!谢谢!
【解决方案2】:

如果您需要调整一组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)

【讨论】:

  • 该链接似乎无法再访问了……
  • 是的,不幸的是它因 nosupportlinuxhosting 而失败。但我相信这篇文章中的代码就是它的全部内容。
  • 我也创建了一个setImageWidth,但它非常不言自明
【解决方案3】:

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

【讨论】:

  • attachment.bounds
【解决方案4】:

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)

您可以在此处添加可以替换为您自己的图片:

左图

正确的图像

输出会是这样

【讨论】:

    【解决方案5】:

    NSTextAtatchment 只是一个 UIImage 的持有者,所以当它需要缩放时缩放图像并重新创建文本附件或设置它的图像。如果您更改现有文本附件上的图像,则需要强制更新 nslayout。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-23
      • 2021-01-04
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 2010-12-29
      相关资源
      最近更新 更多