简体   繁体   中英

How to add a bounding box to a composite shape in Roassal 3?

I'm trying to draw a bounding box around a group of shapes. I get everything in the scene, but I don't know how to make the bounding box and the text get correctly aligned:

结果

    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

So here is how I did it. The missing key point was to put an 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

here is another solution

在此处输入图像描述

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 

Maybe in a future we can add an extension method for strings 'hello world' asRoassalShape .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM