我需要所有的苹果表情符号
我可以通过从网站getemoji复制所有表情符号并将其放入String,但在我的应用程序中,我需要正确顺序的表情符号as images

推荐答案

以下是更新后的答案,其中有以下更改:

Swift 5

import UIKit
extension String {
    func textToImage() -> UIImage? {
        let nsString = (self as NSString)
        let font = UIFont.systemFont(ofSize: 1024) // you can change your font size here
        let stringAttributes = [NSAttributedString.Key.font: font]
        let imageSize = nsString.size(withAttributes: stringAttributes)

        UIGraphicsBeginImageContextWithOptions(imageSize, false, 0) //  begin image context
        UIColor.clear.set() // clear background
        UIRectFill(CGRect(origin: CGPoint(), size: imageSize)) // set rect size
        nsString.draw(at: CGPoint.zero, withAttributes: stringAttributes) // draw text within rect
        let image = UIGraphicsGetImageFromCurrentImageContext() // create image from context
        UIGraphicsEndImageContext() //  end image context

        return image ?? UIImage()
    }
}

Swift 3.2

import UIKit
extension String {
    func textToImage() -> UIImage? {
        let nsString = (self as NSString)
        let font = UIFont.systemFont(ofSize: 1024) // you can change your font size here
        let stringAttributes = [NSFontAttributeName: font]
        let imageSize = nsString.size(attributes: stringAttributes)

        UIGraphicsBeginImageContextWithOptions(imageSize, false, 0) //  begin image context
        UIColor.clear.set() // clear background
        UIRectFill(CGRect(origin: CGPoint(), size: imageSize)) // set rect size
        nsString.draw(at: CGPoint.zero, withAttributes: stringAttributes) // draw text within rect
        let image = UIGraphicsGetImageFromCurrentImageContext() // create image from context
        UIGraphicsEndImageContext() //  end image context

        return image ?? UIImage()
    }
}

Swift相关问答推荐

我应该在自定义存储队列上使用弱self 吗?

在列表项内的NavigationLink中的按钮无法正常工作

通常从数组调用的SWIFT静态协议函数

查找数组中 ** 元素 ** 的属性的最小值和最大值

如何使用AsyncTimerSequence获取初始时钟,然后在指定的时间间隔内开始迭代?

在 SwiftUI 中禁用 Select 器选项

如何在 SwiftUI 中创建条件内容?

如何打印出此 struct 中的数据?

这是 Int64 Swift Decoding 多余的吗?

符合协议 - 错误?

如果没有标记,则为 Swift 预处理器

如何实现 Swift 的 Comparable 协议?

'#selector' 的参数不引用 '@objc' 方法、属性或初始化程序

try 在解除分配时加载视图控制器的视图... UIAlertController

如何从 UITableViewCell 类中引用 UITableViewController?

如何使用 swift 在 tableview 中填充两个不同数组的两个部分?

URLComponents.url 为零

<<错误类型>> 奇怪的错误

如何判断 firebase 数据库值是否存在?

UITableView 布局在 push segue 和 return 上搞砸了. (iOS 8、Xcode beta 5、Swift)