0

I have a very simple fixed height and width textblock with some content including a url. Problem is if it encounters a url which can't be accomodated complelty in rest of the line then it splits the url from ":// " which looks very ugly. I just want it to stick together, if it can't be accomodated in a line then it should move the whole url to next line.

Sample code and image is attached.

 <Grid>
   <TextBlock Width="200" Height="50" Background="Yellow" TextWrapping="WrapWithOverflow">
       <Run Text="This is supposed to test url http://google.com"></Run>
   </TextBlock>
</Grid>

enter image description here

Please keep in mind, neither the text nor the url is pre-defined, it is a variable value.

Manvinder
  • 4,495
  • 16
  • 53
  • 100
  • `TextWrapping` will wrap from where it find the border. try this ` ` – Abin Oct 08 '19 at 21:53
  • Or this will also work -- ` This is supposed to test url http://google.com" ` – Abin Oct 08 '19 at 21:57
  • Is this a fixed URL that you show in this textbox, or is the URL and the accompanying text coming from some user input/database etc.? If its a fixed value I guess the proposal from Abin Mathew might be good enough, otherwise you might need to parse the text and add the line break. – Arif Eqbal Oct 09 '19 at 05:07
  • @ArifEqbal This is not a fixed url. – Manvinder Oct 09 '19 at 06:33
  • @MegaMind, in that case use a Regex to identify the URL (https://stackoverflow.com/questions/5717312/regular-expression-for-url) and insert a Line break before that. Now if you want something more sophisticated for eg. do not blindly insert a line break unless the URL actually breaks into two lines or some similar logic, this would require a little more effort in terms of logic. – Arif Eqbal Oct 09 '19 at 06:43
  • @ArifEqbal Yes, the thought is to determine if we need a new line or not, otherwise adding the line break is not an issue as both are separate variables and if first variable has only couple of words and link is also small then then there is no point in putting that in a separate line. – Manvinder Oct 09 '19 at 11:07

2 Answers2

0

Try this instead:

Use break tag in text to print URL in new line with out breaks

SAM
  • 1
0

You might try something like this, its probably not a complete solution but might guide you to get done what you want.

Try to measure the width of the formatted text, if the width of the formatted text is larger than the width of your textblock there surely is a wrap in play. To do this you might want to use the FormattedText class something like this:

var formattedText = new FormattedText(
                [Your Text from the TextBlock Here],
                CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight,
                new Typeface([YourTextBlock].FontFamily, [YourTextBlock].FontStyle, [YourTextBlock].FontWeight, [YourTextBlock].FontStretch),
                [YourTextBlock].FontSize,
                Brushes.Black);

Then formattedText.Width will give you the width of the displayed text. Compare that with the width of the Text Block object and if the formatted text is larger you insert a Line Break before the http:// using a simple regex search.

Note: Your TextBlock does not have Text directly, it has Run element which means it can be further formatted though your posted code does not show that. It might become a little more complicated if the Run has formatting and if its not going to have formatting I would rather get rid of it.

Arif Eqbal
  • 3,068
  • 1
  • 18
  • 10