如何更改 SpTextPresenter 中的字体?

问题描述

Pharo 9,Spec 2 -- 我有一个带有 text 小部件的 Spec 2 演示者:

initializePresenters
    text := self newText.
    super initializePresenters

据我所知,它的类型是 SpTextPresenter。如何更改此 text 的字体?字体,此小部件中所有显示文本的大小...例如,“Courier New”,9。

编辑 1

我也试过:

    text addStyle: { SpStyleSTONReader fromString:
'
Font {
#name: "Source Sans Pro",#size: 12,#bold: false,#italic: true
}' }.

但它不起作用,错误是:Improper store into indexable object

编辑 2:

我还发现了 this documentation。看来场景一定是:

  1. 将样式读取为 STON
  2. 在某处(哪里?)为所有应用程序设置样式。它们在 STON 中以其名称进行描述,因此可以在应用程序中以其名称进行引用。
  3. 调用 addStyle: 'the-name' 以便名称为 the-name 的小部件将从加载的 STON 中引用自己的样式。

问题出在 2. - 我没有申请,只有一个演示者,我用 openWithSpec 打开。

解决方法

我直到现在才注意到这一点。
规范“样式”不能直接添加到组件中,但它们需要成为样式表的一部分。
样式表在您的应用程序中定义(特别是在您的应用程序配置中)。
您可以查看 StPharoApplication>>resetConfigurationStPharoMorphicConfiguration>>styleSheetStPharoMorphicConfiguration>>styleSheetCommon 作为示例(您还将在那里看到使用 STON 来声明您的样式只是一种方便的方式,而不是强制性的)。>

这里是您将在那里找到的简化版本:

StPharoApplication >> resetConfiguration

    self useBackend: #Morphic with: StPharoMorphicConfiguration new
StPharoMorphicConfiguration >> styleSheet

    ^ SpStyle defaultStyleSheet,self styleSheetCommon
StPharoMorphicConfiguration >> styleSheetCommon
    "Just an example on how to build styles programatically ;)"

    ^ SpStyleSTONReader fromString: '
.application [
    .searchInputField [
        Font { #size: 12 }
    ]
]
'

然后您可以将样式添加到您的组件中:

text addStyle: 'searchInputField'

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...