11

How do I make an object automatically move when I change the text preceeding/"attached to" it?

I'm trying to figure out how this default symbol works (gif included below) and replicate it.

When I type " " into the override, the wifi symbol moves next to the reception dots. When I type a longer string in (see gif), the wifi symbol automatically moves accordingly to after the text.

It's as if the wifi symbol is anchored to the END of the "Carrrier" text box (which doesn't have a fixed width), and moves according to how long the text box is. How is this done in Sketch?


Demo of changing text in a symbol with an object moving depending on the lenght of the symbol

The actual artboard doesn't have any special on it:

Artboard layers

Any solutions? Please & thanks!

2
  • 2
    How is it done? For example, I create a symbol with text and an say, a square right next to the text box, when I edit the text, the square doesn't move. I want the square to automatically move when I change the text in the overrides.
    – jess
    Sep 2, 2016 at 14:01
  • Can't reproduce this desired behavior in any of the suggestions. Is there still a way to achieve this with the newest Sketchapp Version 51.2? Sadly, also this post doesn't seem to be accurate/working for the newest Sketchapp Version. Cheerio
    – UX Brewer
    Sep 7, 2018 at 15:26

5 Answers 5

4

Many thanks to twitter user @kieranpblack !

You must have at least 1px between the text layer and the second layer for it to work. Works on any resizing option, including default stretch!

(Can't even remember if I trialled this before the Sketch 41 update or not? Seems so simple looking at it now.)

4

With a bit of trial and error I've narrowed down the requirements.

  • The alignment of the text needs to be set to Auto
  • The horizontal space between the text and the other layer needs to be ≥ 0 and ≤ 19px
  • The distance between the top of the text frame and the top of the other layer and the bottom of the text frame and the bottom of the other layer needs to be ≤ 8px

The text can be on the left or right of the displaced layer.

1
  • These conditions were damn helpful, thanks a ton. Oct 2, 2018 at 10:26
1

For Sketch 44+, this becomes quite simple.

  1. Create an auto-width text layer
  2. Place an object (like a rectangle) to the right of that layer
  3. Select both the text layer and the object
  4. From the layers panel choose "Create Symbol"
  5. From the create symbol dialog, choose "↦Left to Right Layout" in the dropdown underneath the symbol name input

Now, when you override the text value in your newly created symbol, the object to the right will keep its position relative to the width of the text

screenshot showing the layout option when creating the symbol

screenshot showing the result of the symbol with overridden text

0

I can't find any documentation about this, but I have figured out how to reproduce the behaviour, if you're still interested.

  1. Create a background rectangle, a piece of auto-width text on top of that rectangle, and a smaller rectangle up to 20px from the right edge of the text.
  2. Select the text and the smaller rectangle and create a group. Let's call it 'group1'
  3. Make sure the small rectangle comes after the auto-width text in the layer list.
  4. Select the background rectangle and group1, and create a symbol.
  5. On the symbol edit page, select group1 and specify 'pin to corner' as the resize method. Check that all of your other elements (the two rectangles and the text) have 'stretch' as their resize methods.
  6. Create some instances and override the text, it should work.

It's tricky to deduce the full ruleset from experimentation. I wish there was a manual or something.

3
  • I tried your steps but sadly couldn't replicate it… I appreciate your help though! It's also frustrating because on the actual symbol, there's no grouping or any "Pin to corner" special resizing… I might send Bohemian code a message asking about it.
    – jess
    Sep 28, 2016 at 12:25
  • Did you hear back from Bohemian? You can find details about how 'group resizing' controls work all over. Try here: medium.com/sketch-app-sources/…
    – Joshua
    Oct 13, 2016 at 0:53
  • Haven't heard from them. I understand the group resizing features but this doesn't feel like that as I'm not necessarily physically resizing the overall object…just changing the text in one of the overrides. All the resizing options on the original symbol are just set to the default "stretch".
    – jess
    Nov 4, 2016 at 20:32
0

Just stumbled on this question in search of answers with no luck, but I figured out a workflow to make this happen:

Steps:

  1. Create a text field that you would like your object to anchor to.
  2. Align object to text field (I have only tried object on right).
  3. Group text field and object.
  4. Set the "resizing" option of the text field to stretch and of the object to pin-to-corner.
  5. Create symbol with the group you just created.
  6. Important: In the symbols page, extend your new symbol's art-board to a width that will accommodate all of your overrides you will input into your text field.

You now should be able to override your text and your object will anchor to your new text.

Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.