将文本内容放在容器中间

Place a text content in the middle of a container

我正在使用拼贴,这样元素就会被放置在方框的中间。

import Graphics.Element exposing (show)
import Graphics.Collage exposing (collage)

textBox =
  show "hello world"

main =
  collage 1000 1000 [textBox]

但是最后一行出现类型不匹配错误,

Graphics.Element.Element
Graphics.Collage.Form

因为,show 函数 returns Elementcollage 只接受 Form。我还可以使用什么其他功能来将文本内容定位在 collage?

的中间

您可以使用 Graphics.Collage.toForm

将元素转换为表单
toForm : Element -> Form

http://package.elm-lang.org/packages/elm-lang/core/2.1.0/Graphics-Collage#toForm

你的程序就变成了

main = collage 1000 1000 [toForm textBox]

grumpyjames 关于将 Element 转换为 Form 以将它们放在拼贴画上的回答是正确的。我只想指出,您 不需要 使用拼贴画将 Element 放在中间。 Graphics.Element 包有一个 container 功能,其目的与拼贴相似,但使用 Element 而不是 Form。所以你也可以这样做:

import Graphics.Element exposing (..)

main =
  container 1000 1000 middle (show "Hello, World!")