NSAttributedString:使图像适应容器 [英] NSAttributedString: Fit image to container
本文介绍了NSAttributedString:使图像适应容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个由HTML制作的NSAttributedString,它显示一些图像。问题是,图像比容器大,我不知道如何适应它。
感谢您的帮助
content.enumerateAttribute (NSAttachmentAttributeName,inRange:NSMakeRange(0,content.length),options:NSAttributedStringEnumerationOptions(0)){(value,range,stop) - >如果让attachement = value为?,则在
中作废? NSTextAttachment {
let image = attachement.imageForBounds(attachement.bounds,textContainer:NSTextContainer(),characterIndex:range.location)
let screenSize:CGRect = UIScreen.mainScreen()。bounds
if image.size.width> screenSize.width-2 {
let newImage = image.resizeImage((screenSize.width-2)/image.size.width)
let newAttribut = NSTextAttachment()
newAttribut.image = newImage
content.addAttribute(NSAttachmentAttributeName,value:newAttribut,range:range)
}
}
}
函数 resizeImage()
定义如下:
扩展名UIImage {
func resizeImage(scale:CGFloat) - > UIImage {
let newSize = CGSizeMake(self.size.width * scale,self.size.height * scale)
let rect = CGRectMake(0,0,newSize.width,newSize.height)
UIGraphicsBeginImageContext(newSize)
self.drawInRect(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
$ / code>
Swift 3版本
<$ p $ b $ text.enumerateAttribute(NSAttachmentAttributeName,in:NSMakeRange(0,text.length),options:.init(rawValue:0),使用:{(value,range,stop)in $ b $如果让attachchement = value为?NSTextAttachment {
let image = attachement.image(forBounds:attachement.bounds,textContainer:NSTextContainer(),characterIndex:range.location)!
let screenSize:CGRect = UIScreen .main.bounds
if image.size.width> screenSize.width-20 {
let newImage = image。 resizeImage(scale:(screenSize.width-2)/image.size.width)
let newAttribut = NSTextAttachment()
newAttribut.image = newImage
text.addAttribute(NSAttachmentAttributeName,value:newAttribut ,范围:
}
}
})
函数 resizeImage()
定义如下:
func resizeImage规模:CGFloat) - > UIImage {
let newSize = CGSize(width:self.size.width * scale,height:self.size.height * scale)
let rect = CGRect(origin:CGPoint.zero,size:newSize)
UIGraphicsBeginImageContext(newSize)
self.draw(in:rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
I have a NSAttributedString which is made from HTML and it displays some images. The problem is that the images are bigger than the container and I wonder how to fit them in it.
Thanks for your help
解决方案
I finally found how to do that:
content.enumerateAttribute(NSAttachmentAttributeName, inRange: NSMakeRange(0, content.length), options: NSAttributedStringEnumerationOptions(0)) { (value, range, stop) -> Void in
if let attachement = value as? NSTextAttachment {
let image = attachement.imageForBounds(attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location)
let screenSize: CGRect = UIScreen.mainScreen().bounds
if image.size.width > screenSize.width-2 {
let newImage = image.resizeImage((screenSize.width-2)/image.size.width)
let newAttribut = NSTextAttachment()
newAttribut.image = newImage
content.addAttribute(NSAttachmentAttributeName, value: newAttribut, range: range)
}
}
}
The function resizeImage()
is defined like that:
extension UIImage {
func resizeImage(scale: CGFloat) -> UIImage {
let newSize = CGSizeMake(self.size.width*scale, self.size.height*scale)
let rect = CGRectMake(0, 0, newSize.width, newSize.height)
UIGraphicsBeginImageContext(newSize)
self.drawInRect(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
Swift 3 version
text.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, text.length), options: .init(rawValue: 0), using: { (value, range, stop) in
if let attachement = value as? NSTextAttachment {
let image = attachement.image(forBounds: attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location)!
let screenSize: CGRect = UIScreen.main.bounds
if image.size.width > screenSize.width-20 {
let newImage = image.resizeImage(scale: (screenSize.width-2)/image.size.width)
let newAttribut = NSTextAttachment()
newAttribut.image = newImage
text.addAttribute(NSAttachmentAttributeName, value: newAttribut, range: range)
}
}
})
The function resizeImage()
is defined like that:
func resizeImage(scale: CGFloat) -> UIImage {
let newSize = CGSize(width: self.size.width*scale, height: self.size.height*scale)
let rect = CGRect(origin: CGPoint.zero, size: newSize)
UIGraphicsBeginImageContext(newSize)
self.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
这篇关于NSAttributedString:使图像适应容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文