然后呢,我就想着用UIVibrancyEffect做,毛玻璃嘛,然后试了一个多小时,找他的各种属性调试,不行,
然后就想着可能需要先把文字转成图片,在加毛玻璃
func image(string:String,font:UIFont,height: CGFloat,width:CGFloat,textAlignment:NSTextAlignment) -> UIImage{
let str = string as NSString
let size = CGSize(width: width, height: height)//str.boundingRect(with:
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
UIColor.clear.set()
let rect = CGRect(x:0, y:0, width: size.width + 200, height : size.height)
context!.fill(rect)
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .left
// paragraph.firstLineHeadIndent = 0
// paragraph.headIndent = 0
// paragraph.paragraphSpacingBefore = 0
// paragraph.paragraphSpacing = 0
// paragraph.lineSpacing = 0
let attributes = [NSAttributedString.Key.foregroundColor : UIColor.black,
NSAttributedString.Key.font : font,
NSAttributedString.Key.paragraphStyle:paragraph
]
str.draw(in: rect, withAttributes: attributes)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
也不行,去百度和谷歌都找不到相应的东西,然后为了发版本,只能先盖一张固定的模糊过的文字图片上去做做样子(感谢UI妹纸的配合),先发了版本再说,于是过了几天,我突然想到一个东西:CIFilter,他也可以做模糊的呀,于是我找了下资料
// CIGaussianBlur 高斯模糊
// CIBoxBlur 均值模糊
// CIDiscBlur 环形卷积模糊
// CIMotionBlur 运动模糊
CIFilter *filter = [CIFilter filterWithName:@"CIMotionBlur"];
运动模糊应该就是我要的效果了
于是直接试了一下
let inputImg = self.image(string: label.text!, font: UIFont.systemFont(ofSize: 22),height:label.frame.size.height, width: label.frame.size.width, textAlignment: .left)
let input = CIImage(cgImage: inputImg.cgImage!)
let filter = CIFilter(name: "CIMotionBlur")
filter?.setValue(input, forKey: kCIInputImageKey)
filter?.setValue(NSNumber(value: 8), forKey: kCIInputRadiusKey)
let context = CIContext(options: nil)
let outputImg = filter?.outputImage
let imgRef = context.createCGImage(outputImg!, from: outputImg!.extent)
imageV.image = UIImage.init(cgImage: imgRef!)
也就是上面那个效果,不过此处有个问题,模糊图片前面会有一段空白,这个是在文本转图片的时候存在的问题,我也试了修改NSMutableParagraphStyle的属性值,也就是我所注释掉的那部分代码,即使加了那些也没有办法把文字开头的空白去掉,只能带改天研究如何做到文本和文字一样等高等宽了.
如果有什么遗漏的或者知道怎么解决这个问题的小伙伴,还请不吝赐教.