Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否将模糊视图添加到标签?_Ios_Swift_Label_Blur - Fatal编程技术网

Ios 是否将模糊视图添加到标签?

Ios 是否将模糊视图添加到标签?,ios,swift,label,blur,Ios,Swift,Label,Blur,如何将模糊视图添加到标签?标签在UIImage前面,我希望标签的背景模糊,这样用户可以更好地阅读文本。我在标签的边界内得到模糊效果,但是文本本身消失了(可能也会变得模糊,idk为什么)。我还尝试以编程方式添加标签,但没有成功。我很感谢任何帮助 let blur = UIBlurEffect(style: .Light) let blurView = UIVisualEffectView(effect: blur) blurView.frame = findATeamLabel.

如何将模糊视图添加到标签?标签在UIImage前面,我希望标签的背景模糊,这样用户可以更好地阅读文本。我在标签的边界内得到模糊效果,但是文本本身消失了(可能也会变得模糊,idk为什么)。我还尝试以编程方式添加标签,但没有成功。我很感谢任何帮助

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()中的“在展开可选值时意外发现零”初始化后如何使标签模糊?这里回答了问题,