arrays - 为什么 ASTextNode 不显示?
问题描述
我不明白为什么不显示文字
导入 UIKit 导入 AsyncDisplayKit
类视图控制器:ASViewController {
init() {
super.init(node: ASDisplayNode())
let text = ASTextNode()
let attrs = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue", size: 12.0)!]
let string = NSAttributedString(string: "Hello World!", attributes: attrs)
text.attributedText = string
self.node.addSubnode(text)
}
required init?(coder aDecoder: NSCoder) {
super.init(node: ASDisplayNode.init())
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let window = UIWindow(frame: UIScreen.main.bounds)
window.backgroundColor = UIColor.cyan
window.rootViewController = ViewController()
window.makeKeyAndVisible()
self.window = window
return true
}
解决方案
import UIKit
import AsyncDisplayKit
class ViewController: ASViewController<ASDisplayNode> {
init() {
super.init(node: LayoutExampleNode())
}
required init?(coder aDecoder: NSCoder) {
super.init(node: ASDisplayNode.init())
}
}
class LayoutExampleNode: ASDisplayNode {
override required init() {
super.init()
automaticallyManagesSubnodes = true
backgroundColor = .white
}
}
extension LayoutExampleNode {
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
let someVerticalStack = ASStackLayoutSpec.vertical()
someVerticalStack.style.flexShrink = 1.0
someVerticalStack.style.flexGrow = 1.0
let text = ASTextNode()
let attrs = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue", size: 12.0)!]
let string = NSAttributedString(string: "Hello World!", attributes: attrs)
text.attributedText = string
someVerticalStack.children = [text]
let someVerticalStackSpec = ASStackLayoutSpec(direction: .horizontal,
spacing: 40,
justifyContent: .start,
alignItems: .center,
children: [someVerticalStack])
return ASInsetLayoutSpec(insets: UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10), child: someVerticalStackSpec)
}
}
推荐阅读
- google-apps-script - 如何在不通过 Admin SDK 的情况下从 BASE Google 用户对象或电子邮件中获取用户名?
- c++ - 使用测试套件,如何让测试显示在 Qt5 的“测试”窗格视图中?
- php - 无法创建帖子
- http - Visual Studio 2015 - 调试 HTTP 连接问题
- animation - SwiftUI 动画:一些隐式过渡动画在 iOS 13 上被破坏了?
- ios - 如何在应用关闭时保存用户购物车?- 迅速
- html - 引导三个垂直输入组
- google-sheets - 非连续范围的条件格式
- html - ::before 带有 :hover 效果的语法
- scala - 安装 ADLS - 范围内不存在秘密:
和关键: