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
    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 '16 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 at 15:26
up vote 4 down vote accepted

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.)

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.

  • These conditions were damn helpful, thanks a ton. – Shravan Veer Kohli Oct 2 at 10:26

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.

  • 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 '16 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 '16 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 '16 at 20:32

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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