# How to get an adaptive frame which fits auto-wrapped text? [duplicate]

When using the built-in Debugger, the breakpoints and the current step will be framed looks like this:

My favorite part is, those frames are adaptive to the text when window width changing makes it auto-wrapped to a different shape:

So my question is:

How can I programmly get a frame with this kind of adaptive behavior, as good performance (executing fast) as possible?

-
Define "efficient." Do you mean shortest code, fastest code, or something else? –  rcollyer Dec 1 '12 at 13:35
@rcollyer Sorry for the ambiguousness. I would like a code executing as fast as possible. I have edited my descriptions. Thanks for pointing out. –  Silvia Dec 1 '12 at 13:41
Maybe relevant: en.wikipedia.org/wiki/Word_wrap#Minimum_raggedness –  ziyuang Dec 2 '12 at 2:30
@ziyuang Thanks for your information. But what I concern is not how to wrap text but how to draw the irregular frame around given text. –  Silvia Dec 2 '12 at 11:43
@Silvia I think you can draw the frame after you wrap the text by surrounding your wrapped text, if I understand your question correctly. Or are you asking whether there is a built-in function for this purpose since this effect appears in Mathematica? –  ziyuang Dec 2 '12 at 12:51
show 1 more comment

## marked as duplicate by Silvia, rasher, m_goldberg, Kuba, bobthechemistApr 10 at 13:18

This is hacky, but it works:

front = Pane[
Style["this is a test and I hope it works so I need a very long string",
Background -> Yellow], ImageSize -> 150]


back = Pane[
Style["this is a test and I hope it works so I need a very long string",
Background -> Red], ImageSize -> 150, ImageMargins -> 2]


Overlay[{MinFilter[Rasterize@back, 2], front}, Alignment -> Center,
ImageMargins -> 1]


There are a few artefacts where the text hits the edge of the background in the back copy, and therefore the black color is picked up in the MinFilter. As shown below you can fix this by setting the text color and the background color in the back copy the same.

You could build this up into a function like this:

borderedText[text_String, width_Integer?Positive, {color1_, color2_}] :=
With[{f = Pane[Style[text, Background -> color1], ImageSize -> width],
b = Pane[Style[text, color2, Background -> color2], ImageSize -> width, ImageMargins -> 2]},
Overlay[{MinFilter[Rasterize@b, 2], f}, Alignment -> Center, ImageMargins -> 1]]

borderedText["This is a test that might or might not work", 150,
{Lighter@Yellow, Red}]


-
Hi Verbeia. Thank you for your answer! It's really clever! Though I don't have access to a MMA, but it looks promising. Do you think it could be made to work in an "Input" style cell, so we can block-mark the code without affecting its executability? –  Silvia Sep 23 '13 at 3:53