- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
留在 SpriteKit 中,是否可以使用 TextKit 提供的更强大的控制创建更多“艺术”文本,然后(以某种方式)将这些字符串转换为图像以便它们可以用作 SKSpriteNode?
我问是因为我想做一些更严肃的字距调整……更大的间距,以及其他一些 SKLabels 无法实现的东西,但它们是 TextKit 的一部分,但我希望它们能够一旦我完成让它们看起来像我想要的方式,就成为位图。
但我找不到将 A TextKit 转换为图像的方法。
最佳答案
您可以在 CGContext
中绘制文本,然后从中创建纹理并将该纹理分配给 SKSpriteNode
。
这是来自 this GitHub project 的示例:
class ASAttributedLabelNode: SKSpriteNode {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init(size: CGSize) {
super.init(texture: nil, color: UIColor.clear, size: size)
}
var attributedString: NSAttributedString! {
didSet {
draw()
}
}
func draw() {
guard let attrStr = attributedString else {
texture = nil
return
}
let scaleFactor = UIScreen.main.scale
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue
guard let context = CGContext(data: nil, width: Int(size.width * scaleFactor), height: Int(size.height * scaleFactor), bitsPerComponent: 8, bytesPerRow: Int(size.width * scaleFactor) * 4, space: colorSpace, bitmapInfo: bitmapInfo) else {
return
}
context.scaleBy(x: scaleFactor, y: scaleFactor)
context.concatenate(CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: size.height))
UIGraphicsPushContext(context)
let strHeight = attrStr.boundingRect(with: size, options: .usesLineFragmentOrigin, context: nil).height
let yOffset = (size.height - strHeight) / 2.0
attrStr.draw(with: CGRect(x: 0, y: yOffset, width: size.width, height: strHeight), options: .usesLineFragmentOrigin, context: nil)
if let imageRef = context.makeImage() {
texture = SKTexture(cgImage: imageRef)
} else {
texture = nil
}
UIGraphicsPopContext()
}
}
关于swift - SKLabel 避免,使用 TextKit 而不是创建 SKTexture?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40472026/
我从 objc.io 中获取了 TextKit 示例项目 #5 http://www.objc.io/issue-5/getting-to-know-textkit.html修改Text Intera
如果我有一个很长的 NSTextStorage 并且我知道 NSTextContainer 的 CGSize,我如何计算文本的页数?这个东西有没有标准的程序或某种动态计算的方法?我找不到好的教程来使用
示例项目:http://d.pr/f/1coXu 我正在使用 TextKit 将一些文本呈现为本质上是 UILabel 的非常基本的再创造。代码非常简单,在这个例子中只是将硬编码的 NSAttribu
我正在开发一个由 TextKit 支持的多页阅读应用程序,该应用程序基于 WWDC 2013 的“高级文本布局和文本工具包效果” session (但一些代码是根据不完整的示例重建的)。基本结构是预先
我正在尝试使用 UITextView 获得以下效果: 基本上我想在文本之间插入图像。图片只需占用 1 行空间,因此无需换行。 我试着只添加一个 UIView 到 subview : UIView *p
今天是个好日子。我在使用带有 textview 的 TextKit 显示带连字符的对齐文本时遇到问题。 我试图通过段落样式和 NSLayoutManager 进行连字符,但没有预期的结果。段落样式示例
我正在通过 TextKit 构建一个杂志应用程序,这里是一个 TextKit demo (查看 developer 分支)。它从 rtfd 文件中加载一个 NSAttributeString 作为文本
我实际上正在开发一个古兰经应用程序,我在其中使用 TextKit 来突出显示经文并更改它们的颜色。一切都很顺利,但我对出现多次的单词有点问题。首先,我的代码是: import UIKit cl
我在 Storyboard中有一个 UITextView 和一个 UINavigationController,它看起来像这样: 它在顶部有一个自动布局约束,以防止最上面的文本隐藏在导航栏下方,并且在
在我的应用程序中,如果可能的话,我需要使用 Objective-C 实现以下功能。我想知道是否可以从 UITextView 生成所选文本的屏幕截图。我知道这听起来像是一个奇怪的问题,但它是一项重要且必
我正在尝试在 iPad 上显示来自网站的 html 页面。我没有使用 UIWebview,而是使用了 Textkit,这样我就可以在多列中显示内容。我使用 iOS7 Day-by-Day day 21
有没有人有幸在 Swift 中创建文档的多页 View (一个 NSTextStorage、一个 NSLayoutManager 和多个 NSTextContainer)。我很难找到不在 object
我需要像 textkit swift 这样的东西来在我的应用程序中实现文本编辑器。问题是所有 textkit 代码示例和信息都在旧版本的 swift 中,swift 4 不支持它并且它有超过 100
留在 SpriteKit 中,是否可以使用 TextKit 提供的更强大的控制创建更多“艺术”文本,然后(以某种方式)将这些字符串转换为图像以便它们可以用作 SKSpriteNode? 我问是因为我想
在以前的 iOS 版本中,我的 UITextView 将滚动到底部使用 [displayText scrollRangeToVisible:NSMakeRange(0,[displayText.tex
我有一个 UIImage,它包含多个图像帧和一个从动画 GIF 文件切片在一起的持续时间。如果我在 UIWebView 中加载图像,它将按预期进行动画处理,但我想改用 UITextView。显而易见的
我的应用程序中使用了以下代码来更改带有 删除线 的 TextView 中的文本状态。现在我用 Objective-C 和 Swift 编写了一个小示例应用程序。正如您在屏幕截图中看到的那样,结果再次令
我假设我可以使用 TextKit 简洁地做到这一点。 我不需要向后兼容性。 我不想破坏任何现有的属性NSAttributedString 实例涉及。 我不一定介意子类化 UILabel,如果我能得到一
我想为标签设置样式。没有什么疯狂的,只是一些颜色,改变特定单词的字体大小,等等。我可以用 NSAttributedString 做到这一点,但是在 iOS 7 新引入的 TextKit 中这样做可能/
我是一名优秀的程序员,十分优秀!