【问题标题】:How to add a bounding box to a composite shape in Roassal 3?如何在 Roassal 3 中为复合形状添加边界框?
【发布时间】:2020-07-05 21:14:16
【问题描述】:

我正在尝试围绕一组形状绘制一个边界框。我得到了场景中的所有内容,但我不知道如何使边界框和文本正确对齐:

    c := RSCanvas new.
    text := RSGroup new.
    foo := RSLabel new text: 'foo'.
    bar := RSLabel new text: 'bar'.
    text add: foo; add: bar.
    RSVerticalLineLayout on: text.
    bound := RSShapeFactory box
        model: self;
        border: (RSBorder new width: 1; color: Color black);
        cornerRadius: 5;
        width: text encompassingRectangle width + 15;
        height: text encompassingRectangle height + 10.
    all := RSComposite new shapes: { bound. text asShape }.
    c add: all.
    c  @ RSCanvasController.
    ^ c

【问题讨论】:

  • 你为什么要做 RSVerticalLineLayout ?
  • Pharo Discord discord.com/channels/223421264751099906/300020095844089856 为您提供更快的答案
  • @snorik 将文本放在另一行下方。有没有更好的方法来做到这一点?
  • @StephanEggermont 我知道,但是那里的答案会随着时间的推移而被遗忘,stackoverflow 可以更好地保留一种常见问题解答

标签: smalltalk pharo roassal


【解决方案1】:

这就是我的做法。缺少的关键点是放置一个 RSLocation。

    c := RSCanvas new.
    text := RSGroup new.
    foo := RSLabel new text: 'foo'.
    bar := RSLabel new text: 'bar'.
    text add: foo; add: bar.
    RSVerticalLineLayout on: text.
    bound := RSShapeFactory box
        model: self;
        border: (RSBorder new width: 1; color: Color black);
        cornerRadius: 5;
        width: text encompassingRectangle width + 15;
        height: text encompassingRectangle height + 10.
    contents := text asShape.
    all := RSComposite new shapes: { bound. contents }.
    RSLocation new center; outer; stick: contents on: bound.
    c add: all.
    c  @ RSCanvasController.
    ^ c

【讨论】:

    【解决方案2】:

    这是另一个解决方案

    text := 'Foo
    bar'.
    label := RSMultilineLabelBuilder new shapeFor: text.
    box := RSBox new 
        fromRectangle: label encompassingRectangle;
        cornerRadius: 10;
        noPaint
        withBorder.
    box extent: box extent + 15.
    all := { box . label} asGroup asShape.
    
    canvas := RSCanvas new.
    canvas add: all.
    canvas @ RSCanvasController 
    

    也许将来我们可以为字符串 'hello world' asRoassalShape 添加扩展方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 2013-11-22
      • 2011-05-19
      相关资源
      最近更新 更多