2017-03-01 60 views
1

我想用类型安全的css在tornadofx-app中加载自定义字体,这可能吗? 感谢和问候。如何使用类型安全的CSS加载自定义字体?

+0

嗨!目前没有对@ font-face规则的特别支持,但我们正在努力。现在,您可以覆盖样式表的'render()'函数并添加'“@ font-face {...}”+ super.render()'。我们会尽快收到更好的解决方案:) –

回答

2

Edvin是正确的,我们正在寻求解决方案,但我暂时发现了一个更好的解决方案。只要字体被加载,它可以在CSS中使用,所以下面的工作:

class FontTest : App(Main::class, Styles::class) { 
    class Main : View("Font Test") { 
     override val root = stackpane { 
      label("This is my Label") { 
       addClass(Styles.custom) 
      } 
     } 
    } 

    class Styles : Stylesheet() { 
     companion object { 
      val custom by cssclass() 
      val riesling: Font? = Styles::class.java.getResourceAsStream("/fonts/riesling.ttf").use { Font.loadFont(it, 48.0) } 
     } 

     init { 
      custom { 
       padding = box(25.px) 

       riesling?.let { font = it } 
       // or if you just want to set the font family: 
       // riesling?.let { fontFamily = it.family } 
      } 
     } 
    } 
} 

enter image description here

+0

嗨,Edvin和Ruckus T-Boom,感谢您的快速帮助。它适合我。随着TornadoFx你做了一个伟大的工作。 – thlinde

0

顺便说自29天为Ruckus T-Boom回答了这个问题,他补充loadFont功能:) ,有可能写:

class Styles : Stylesheet() { 
    private val customFont = loadFont("/fonts/custom-font.ttf", 14)!! 

    init { 
     root { 
      font = customFont 
      fontSize = 11.px 
     } 
    } 
}