Change font size to fit textblock - c#

I'm having a little problem, i have Custom list box item, that is 165 px height. So, the text block wraps text and gets 50% height of the list box item, example:
How you noticed the text size is too big.
Is there a way to make fint size exatly big to fit in the textblock?

As far as my understanding it can be done with help of textBox.Text.Length property. Also It depends on the font you are using.

What happens when the Text is 10000 characters long? Rather than trying to show all of the text you should make the font smaller (no need for something that large) and use TextTrimming. TextTrimming will cut the text short and put an ellipse at the end.
<TextBlock Text="{Binding MyProperty}" TextTrimming="WordEllipsis" />

Related

Dynamic sized multi line TextBox field in a PDF

I have created a document in an open office with a multi-line form field:
The issue I am having is when the dynamic content exceeds the initial size of the multi-line text box:
Sure I can re-size the Text Box in the original template but the dynamic content may be from 1 to 50 lines and I want the text after the Text Box to be close to the last line of dynamic content.
Can someone suggest a way to solve this?
I have once coded a solution to set the width and height of a textbox programmatically based on the characters supplied. I think this was a school assignment a long time ago.
This can be done with both VBA in a code behind or probably with a macro even. Or with VB.net.
I don't have the code I used way back when, but basically determine the maximum width you are able to provide in character width, the preferred width. Determine the pixel requirement per character for that width. This becomes the textbox width.
Divide your total string character count by your preferred width character count. Round up, calculate the pixel height per character. And use this value times the rounded result for the textbox height. Dirty but it should work.
Any chance you can change to a label and set AutoSize to True? You can fix the width and let the height auto adjust. This should be done before converting to a pdf. In fact all of the sizing should be resovled before pdf conversion.
Another Down voter without a comment, should not be allowed.

How to change font size in TextBlock, when the text is in the second line

I'm looking for method to change font size in TextBlock if text goes to second line.
How can I know, when the text proceed to the second line?
Thanks for help.
What you could do is handle the TextChanged event and in the handler check the length of the text.
If the text is <= 20 you keep the original size. If it's > 20 but <= 25 you decrease the font size by say 3 points. An so on until you reach a limit of characters you want to support, say 50 or something.
You will probably need to disable Textbox text wrapping so that you only have a single line of text.
Wrapping with a Viewbox may give you the functionality you're after:
<Viewbox Stretch="Uniform" Width="50" Height="50">
<TextBlock Text="Test" />
</Viewbox>
source - https://stackoverflow.com/a/13268895/1202600

Limit text to width of TextBox

I'm using a TextBox to create an editable title on something that looks like a post-it note. I changed to a TextBox from a RichEditBox to see if that could solve my problem, which it didn't, so I'm willing to change back if that helps.
My problem is that I don't want the user to be able to enter more characters than fit in the set width of the TextBox, because I want the whole title to be visible. Setting a fixed limit to the amount of characters that can be entered doesn't really work since for example 10 large M's would fill the width of my TextBox, but other characters will only fill half of it. So I would like to compare the width of the text to the width of the TextBox, so I can restrict input beyond that point.
Edit: I'm using the Segoe UI font, and I don't really consider changing the font to one with characters of equal width as a solution.
Set the TextBox Font to a Courier Font, then all characters are equal width and calculating max becomes trivial.
I am suck in programming world. i m try help you..

Best Silverlight controls for creating a console

I'm developing a Silverlight application where I want to simulate a console. There are a lot of ways to represent this - StackPannels, grid of TextBoxes, etc - and I was wondering what the bets fit would be?
Requirements:
Display an 80x20 grid that scales based on parent size
Be able to update an individual cell's character
Be able to set a cell's forground and background color
Why would you use TextBox instead of TextBlock.
I think you should use 1 TextBlock and format the text like:
<TextBlock>
<Run FontWeight="Bold">Hello There.</Run>
<Run Foreground="Red">How are you?</Run>
<Run FontStyle="Italic">I am fine thanks!</Run>
<Run>漢字</Run>
</TextBlock>
And start with setting 80*20 space keys.
Then implement some algoritmen to find a specific character and fx to set its Foreground by clipping it out of the Run it is in and make some new Run objects.
And wire up some events to receive new keys. Or use one TextBlock where the keyboard pointer is.
You could also get inspiration from here:
http://silverlight.net/content/samples/sl2/dlrconsole/index.html
- you can download the code to the DLRConsole
Grid filled with Textboxes?
But wait... 1600 Text boxes... I don't know what to do, sorry ((

WPF - Work out a width for a FlowDocument

I am working in C# and VS2008.
I have a WPF application containing a FlowDocument, which contains a paragraph, which contains a number of fairly short lines (i.e. Spans and LineBreaks). However the length of lines varies. I would like the FlowDocument's width to be large enough to accommodate the longest of these lines without wrapping, but no wider so as to not waste space.
Putting it into a grid column with Width=Auto doesn't work: the FlowDocument always consumes the max width allowed, since the FlowDocument can validly wrap to suit a number of different widths.
I could go through the lines as I generate them in code, and work out a width for each one, but I see no Width property on the Span or Run classes.
Any ideas on how to automatically or manually set a width for this document?
I used AndrewS's suggested idea and moved from a FlowDocument to a StackPanel of labels each containing a line. The XAML Markup is
<Border BorderThickness="2" Margin="2" BorderBrush="Black">
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<StackPanel
Orientation="Vertical"
x:Name="itemsStackPanel" />
</ScrollViewer>
</Border>
The code creates spans and adds them as follows:
Label itemLabel = new Label();
// pad left and right, keep close top and bottom
itemLabel.Padding = new Thickness(2,0,2,0);
itemLabel.Content = contentSpan;
this.itemsStackPanel.Children.Add(itemLabel);
Sizing is perfect now, and the only snag is that the font doesn't look exactly the same as the FlowDocument next to it, despite the font Family and Font Size being the same (Segoe UI, 15pt), and that the Labels look much further apart than the lines in the FlowDocument. I have asked this as a separate question
You could replace the lines with a different type of element such as TextBlocks or Labels (for which you can control wrapping) or Glyphs which "are designed for fixed-format document presentation and print scenarios".
I really don't think you want to try measuring lines and forcing the document width.
I've got a similar situation, but I do want to measure the line width. Sadly, the RichTextBox has two important features bundled together - text foreground and background colors in runs and formatting. I need the colors and could care less about the formatting. I've got a "grid" of characters and I want to run several "cursors" over the characters. I plan on implementing the background colors as the individual cursors and use foreground colors for syntax coloring. It's a fixed width font and I don't want the grid to wrap so essentially no formatting. I suppose I could implement this as a grid that I set the rows and columns at runtime according to how big my text is, but that seems like a real waste of resources - especially if the file is really large - i.e., a 100x100 grid with a separate label in every cell. Originally, and perhaps a little naively, I thought that it would be a snap with RichTextBox but I'm beginning to believe otherwise. Another possibility is to put a canvas on top of a standard TextBox and drawing rects myself but having to deal with everything as stuff scrolls, etc. sounds ugly and error prone to me. My code right now is:
private static void SetRichText(RichTextBox rtb, string[] arLines)
{
Contract.Assert(arLines.All(l => l.Length == arLines[0].Length));
FlowDocument doc = new FlowDocument { PageWidth = 65535.0 };
IEnumerable<Paragraph> iep = arLines.Select(l =>
{
Paragraph p = new Paragraph();
p.Inlines.Add(new Run(l));
return p;
});
foreach (Paragraph p in iep)
{
doc.Blocks.Add(p);
}
rtb.Document = doc;
doc.PagePadding = new Thickness(0, 0, 0, 0);
Rect rcEOL = rtb.Document.ContentStart.GetPositionAtOffset(arLines[0].Length).GetCharacterRect(LogicalDirection.Forward);
// TODO : Figure why I have to add a fudge factor in the following (I'm guessing some margin I'm unaware of in the
// RichTextBox somewhere)...
rtb.Document.PageWidth = rcEOL.Right + 18;
}
As I state in the comments, this seems to work for my current case, but I haven't really tested it with many others so I'm not sure whether it works in general, plus it's just a big kludge anyway that I can't explain so I'd like to know how to handle this correctly. Thanks for any responses. Oh, I should point out as I stated above, that the for is fixed pitch and all the incoming strings are the same length as enforced by the contract so all lines should be exactly the same length.

Categories

Resources