我有一个NSAttributedString,我要添加一个NSTextAttachment。图像是50w乘50h,但我希望它缩小以反映属性字符串的行高。我以为这会自动完成,但我猜不会。我查看了UImage类引用,但是这个图像似乎没有在UIImageView中设置,因此无法访问框架属性。这是我目前拥有的截图:
在理想的世界中,我还想实现一种基于用户输入(例如增加字体大小)来放大图像的方法。关于如何实现这一点的任何想法?
THX
这就是我创建它的方式:
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];
答案 0 :(得分:39)
您应该设置边界形式附件以调整图像大小,如下所示:
attachment.bounds = CGRectMake(0, 0, yourImgWidth, yourImgHeight)
答案 1 :(得分: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 :(得分:15)
查看子类NSTextAttachment
并实现NSTextAttachmentContainer
方法,根据提供的文本容器返回不同的大小。默认情况下,NSTextAttachment
只返回其提供的图像大小。
答案 3 :(得分: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
}
&#13;
您可以使用下面提到的代码:
self.lblMid.attributedText = AttributedTextwithImgaeSuffixAndPrefix(AttributeImage1: #imageLiteral(resourceName: "Left") , AttributedText: " Payment Details ", AttributeImage2: #imageLiteral(resourceName: "RIght") , LabelBound: self.lblMid)
&#13;
在这里你可以添加图片,你可以用自己的图片替换它:
答案 4 :(得分:0)
NSTextAtatchment只是UIImage的持有者,因此在需要缩放和重新创建文本附件或设置图像时缩放图像。如果更改现有文本附件上的图像,则需要强制执行nslayout更新。