Ios 是否将模糊视图添加到标签?
如何将模糊视图添加到标签?标签在UIImage前面,我希望标签的背景模糊,这样用户可以更好地阅读文本。我在标签的边界内得到模糊效果,但是文本本身消失了(可能也会变得模糊,idk为什么)。我还尝试以编程方式添加标签,但没有成功。我很感谢任何帮助Ios 是否将模糊视图添加到标签?,ios,swift,label,blur,Ios,Swift,Label,Blur,如何将模糊视图添加到标签?标签在UIImage前面,我希望标签的背景模糊,这样用户可以更好地阅读文本。我在标签的边界内得到模糊效果,但是文本本身消失了(可能也会变得模糊,idk为什么)。我还尝试以编程方式添加标签,但没有成功。我很感谢任何帮助 let blur = UIBlurEffect(style: .Light) let blurView = UIVisualEffectView(effect: blur) blurView.frame = findATeamLabel.
let blur = UIBlurEffect(style: .Light)
let blurView = UIVisualEffectView(effect: blur)
blurView.frame = findATeamLabel.bounds
findATeamLabel.addSubview(blurView)
您可以尝试将其发送到标签的视图层次结构的后面。试一试
findATeamLabel.sendSubviewToBack(blurView)
我通过在标签后面添加一个视图(标签不在该视图内,而在它前面)来实现它。然后我在视图中添加了模糊效果。。。我仍然认为应该有一个更简单的方法。您可以制作自己的模糊标签
,它可以模糊/取消模糊文本。通过CoreImage模糊过滤器,获取标签文本,在图像中模糊它,并在标签顶部显示该图像
class BlurredLabel: UILabel {
func blur(_ blurRadius: Double = 2.5) {
let blurredImage = getBlurryImage(blurRadius)
let blurredImageView = UIImageView(image: blurredImage)
blurredImageView.translatesAutoresizingMaskIntoConstraints = false
blurredImageView.tag = 100
blurredImageView.contentMode = .center
blurredImageView.backgroundColor = .white
addSubview(blurredImageView)
NSLayoutConstraint.activate([
blurredImageView.centerXAnchor.constraint(equalTo: centerXAnchor),
blurredImageView.centerYAnchor.constraint(equalTo: centerYAnchor)
])
}
func unblur() {
subviews.forEach { subview in
if subview.tag == 100 {
subview.removeFromSuperview()
}
}
}
private func getBlurryImage(_ blurRadius: Double = 2.5) -> UIImage? {
UIGraphicsBeginImageContext(bounds.size)
layer.render(in: UIGraphicsGetCurrentContext()!)
guard let image = UIGraphicsGetImageFromCurrentImageContext(),
let blurFilter = CIFilter(name: "CIGaussianBlur") else {
UIGraphicsEndImageContext()
return nil
}
UIGraphicsEndImageContext()
blurFilter.setDefaults()
blurFilter.setValue(CIImage(image: image), forKey: kCIInputImageKey)
blurFilter.setValue(blurRadius, forKey: kCIInputRadiusKey)
var convertedImage: UIImage?
let context = CIContext(options: nil)
if let blurOutputImage = blurFilter.outputImage,
let cgImage = context.createCGImage(blurOutputImage, from: blurOutputImage.extent) {
convertedImage = UIImage(cgImage: cgImage)
}
return convertedImage
}
}
PS:请确保根据您的要求改进此组件(例如,如果已经模糊,请避免模糊,或者如果文本已更改,您可以删除当前模糊并再次应用模糊)
PSP:还要考虑到对某些东西应用模糊会使其内容溢出,因此,可以将clipsToBounds=false
设置为BlurredLabel
,或者找到其他方法来实现视觉效果,以避免模糊图像看起来与先前的标签未模糊文本不在同一位置
要使用它,只需创建一个模糊标签
:
let blurredLabel = BlurredLabel()
blurredLabel.text = "56.00 €"
点击一些按钮,您可能会在blurredLabel.blur()
时实现模糊,在blurredLabel.unblur()
时实现取消模糊
这是使用blur()
通过2.5的blurRadius
实现的输出:
要了解更多关于高斯模糊的信息,维基百科上有一篇好文章:u r在标签顶部添加模糊视图。你应该把它加在标签后面。我也是这么想的,但是怎么做呢将模糊视图添加到标签后面的图层…查看图层后面的addlayer。是否可以显示一些示例代码?[self.view.layer insertSublayer:visualeffectView.layer lower:label.layer]。我想你可以把它转换成swift。我也试过了,但一切都是模糊的,没有text@beginner_T我是在遇到同样的挑战后才发现这篇文章的,我提供了一个带有自定义UILabel的答案,该标签可以模糊文本,请在此处查看:如果你称之为blur()的话,这非常有用视图布局后的功能。但是,在我的例子中,标签位于UITableViewCells中,因此当标签已布置好时,我无法调用blur()函数。有什么想法吗?初始化后的模糊标签会导致
UIGraphicsGetCurrentContext()中的“在展开可选值时意外发现零”代码>初始化后如何使标签模糊?这里回答了问题,