gpt4 book ai didi

ios - 在 UILabel 中使表情符号灰度化

转载 作者:可可西里 更新时间:2023-11-01 03:00:51 26 4
gpt4 key购买 nike

我想在 UILabel 中使用 Apple 的内置表情符号(特别是一些笑脸,例如 \ue415) 但我希望表情符号以灰度呈现。

我希望它们在 UILabel 中保留字符(纯文本或属性都可以)。我不是在寻找混合图像/字符串解决方案(我已经有了)。

有谁知道如何做到这一点?

最佳答案

我知道你说过你不是在寻找“混合图像解决方案”,但我一直在追逐这条龙,我能想到的最好结果是混合图像。为了以防万一我的解决方案对您的旅程更有帮助,我将其包含在此处。祝你好运!

import UIKit
import QuartzCore

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// the target label to apply the effect to
let label = UILabel(frame: view.frame)
// create label text with empji
label.text = "🍑 HELLO"
label.textAlignment = .center
// set to red to further show the greyscale change
label.textColor = .red
// calls our extension to get an image of the label
let image = UIImage.imageWithLabel(label: label)
// create a tonal filter
let tonalFilter = CIFilter(name: "CIPhotoEffectTonal")
// get a CIImage for the filter from the label image
let imageToBlur = CIImage(cgImage: image.cgImage!)
// set that image as the input for the filter
tonalFilter?.setValue(imageToBlur, forKey: kCIInputImageKey)
// get the resultant image from the filter
let outputImage: CIImage? = tonalFilter?.outputImage
// create an image view to show the result
let tonalImageView = UIImageView(frame: view.frame)
// set the image from the filter into the new view
tonalImageView.image = UIImage(ciImage: outputImage ?? CIImage())
// add the view to our hierarchy
view.addSubview(tonalImageView)
}
}

extension UIImage {
class func imageWithLabel(label: UILabel) -> UIImage {
UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0)
label.layer.render(in: UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img!
}
}

关于ios - 在 UILabel 中使表情符号灰度化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15749092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com