Find Height of Textblock / Text - c#

i try to calculate the height of TextBlock.
I dont want to take the Textblock on some page. I just have to know the height of my Content.
I tried it this way, but it just returns 0
private static double CalculateContentHeight(string content, double width, double fontSize)
{
//Nur einen Textblock ggf. um Ressourcen zu sparen
TextBlock textblock = new TextBlock
{
FontSize = fontSize,
Text = content,
TextWrapping = TextWrapping.Wrap,
MaxWidth = width,
Height = Double.NaN
};
return textblock.ActualHeight;
}
If i take a existing TextBlock in XAML and just:
MyXAMLTextBlock.MaxWidth = 7;
double height = MyXAMLTextBlock.ActualHeight;
I get the right value for it.
I know there are possibilitys with Meassure() and Arrange() like in this Example, but Meassure(Size) takes the size paramter and i dont have the height for it.
I know that you can set the size on Auto with Double.Nan, but it doesnt compile with
double height = 9;
Size s = new Size(height, Double.Nan);
Maybe you can help me and thanks a lot.

I have no idea why you are doing this. But with Measure and Arrange it is possible:
Just run this two lines before you return the ActualHeight:
textblock.Measure(new Size());
textblock.Arrange(new Rect());
Hope I could help you

Related

ColorSpectrum Width and Height can't be Changed in UWP?

ColorSpectrum ColorSpectrum = new ColorSpectrum();
ColorSpectrum.Width = 400;
ColorSpectrum.Height = 180;
But the Width and height cant be changed..How to change it?
But the Width and height cant be changed..How to change it?
Unfortunately, the default shape of ColorSpectrum is square, because a user will has more control when they select a specific color using a square because more of the color gamut is shown. So ColorSpectrum will limit by the minimum value of Width Height . If you want change the size, please set Width and Height same value.
ColorSpectrum ColorSpectrum = new ColorSpectrum();
ColorSpectrum.Width = ColorSpectrum.Height = 400;

Adapt the height of a TextBox

I am working on a UserControl that contains a multiline TextBox.
When using my control, one will be able to set the text that will be displayed. The TextBox should then adapt its Height to make the text fit, the Width cannot change.
So here is the property that handles the text :
[Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
public string TextToDisplay
{
get
{
return internalTextBox.Text;
}
set
{
internalTextBox.Text = value;
AdaptTextBoxSize();
}
}
My first attempt was rather simple :
private void AdaptTextBoxSize()
{
int nbLignes = internalTextBox.Lines.Length;
float lineHeight = internalTextBox.Font.GetHeight();
internalTextBox.Height = (int)((nbLignes) * lineHeight);
}
This did not work as it doesn't take into account spacing between two lines of text. So the more lines I have in the text, the more I get clipped.
So I tried this :
private void AdaptTextBoxSize()
{
Size textSize = internalTextBox.GetPreferredSize(new Size(internalTextBox.Width, 0));
internalTextBox.Height = textSize.Height;
}
This does work when all the lines in the textbox are shorter than the Width. But when one line is longer and should be clipped to the next line, GetPreferredSize() returns a larger width than the one I passed, and therefore the height is too small.
So I changed again and tried this one:
private void AdaptTextBoxSize()
{
Size textSize = TextRenderer.MeasureText(
internalTextBox.Text,
internalTextBox.Font,
new Size(internalTextBox.Width, 0),
TextFormatFlags.WordEllipsis
);
internalTextBox.Height = textSize.Height;
}
This time the returned Width is correct, as it does not exceed the one I passed, but the height is the same as the previous trial. So it doesn't work either. I tried different combinations for TextFormatFlags, but could not manage to find the winning one...
Is this a bug from the framework?
The real question here is, is there another thing I can try, or another to achieve what I want (i.e. auto-adapt the height when setting the TextToDisplay property)?
TextBox.GetPositionFromCharIndex returns the pixel position of a character. Position here means top/left so we need to add one more line..
This seems to work here:
textBox.Height = textBox.GetPositionFromCharIndex(textBox4.Text.Length - 1).Y + lineHeight;
I get the line height like this:
int lineHeight = -1;
using (TextBox t = new TextBox() { Font = textBox.Font }) lineHeight = t.Height;
I set the Height instead of the ClientSize.Height, which is slightly wrong unless BorderStyle is None. You can change to textBox.ClientSize = new Size(textBox.ClientSize.Width, l + lh);

TextBlock Width measurement is differed for WinRT and WPF

I need to return width of GridComboBoxColumn based on the maximum length of combotext mentioned below, which is loaded as its ItemsSource . The maximum combotext is "Frankfurt a.M.-Germany-Country" and the width is calculted in WinRT and WPF as follows:
//Calculate width for maximum display text ComboBox items
TextBlock TextBlock = new TextBlock()
{
FontFamily = FontFamily,
Margin = Margin,
FontSize = FontSize,
TextWrapping=TextWrapping.NoWrap
};
//Set the TextBlock display text to combo items text and initialize Height and Width for TextBlock
TextBlock.Text = DisplayText;
var parentBorder = new Border { Child = TextBlock };
TextBlock.MaxHeight = size.Height;
TextBlock.MaxWidth = double.MaxValue;
this.DataGrid.UpdateLayout();
//Measuer the width and Height of parentBorder
parentBorder.Measure(new Size(TextBlock.MaxWidth, TextBlock.MaxHeight));
parentBorder.Child = null;
return parentBorder.DesiredSize.Width;
In WPF, the width returned as 182.46 and in WinRT the width is returned as 248 . But in WinRT the column width fit with ComboBox, in WPF the ComboBox does not fit inside the column width. Please advise how the size is measured in both WPF and WinRT?

How to calculate font size of TextBlock to fill in Canvas?

What I need is some formula to calculate font size of TextBlock for its owner - Canvas.
Let's say I have Canvas height 100.0 then which TextBlock font size should be to fill all space of the Canvas?
P.S. The main problem is that I scroll those TextBlocks horizontally...
Why not just use whatever FontSize while putting the TextBlock in a Viewbox whose Height is bound to that of the Canvas? (When not set explicitly the ActualHeight of the Canvas)
I found the solution which works fine for me.
double h = canvas1.Height / 2;
TextBlock1.FontSize = h;
I found the solution which works fine for me.
double h = canvas1.Height / 2;
foreach (var item in textBlocks)
{
if (item is TextBlock)
{
(item as TextBlock).FontSize = h;
}
}

How do I keep a label centered in WinForms?

In WinForms I am using a Label to display different messages like success, failure, etc.
I'd like to center that label in the center form. I want a solution that will keep it centered whether there's just one word or a whole sentence in the label.
Set Label's AutoSize property to False, TextAlign property to MiddleCenter and Dock property to Fill.
You will achive it with setting property Anchor: None.
Some minor additional content for setting programmatically:
Label textLabel = new Label() {
AutoSize = false,
TextAlign = ContentAlignment.MiddleCenter,
Dock = DockStyle.None,
Left = 10,
Width = myDialog.Width - 10
};
Dockstyle and Content alignment may differ from your needs. For example, for a simple label on a wpf form I use DockStyle.None.
If you don't want to dock label in whole available area, just set SizeChanged event instead of TextChanged. Changing each letter will change the width property of label as well as its text when autosize property set to True. So, by the way you can use any formula to keep label centered in form.
private void lblReport_SizeChanged(object sender, EventArgs e)
{
lblReport.Left = (this.ClientSize.Width - lblReport.Size.Width) / 2;
}
The accepted answer didn't work for me for two reasons:
I had BackColor set so setting AutoSize = false and Dock = Fill causes the background color to fill the whole form
I couldn't have AutoSize set to false anyway because my label text was dynamic
Instead, I simply used the form's width and the width of the label to calculate the left offset:
MyLabel.Left = (this.Width - MyLabel.Width) / 2;
I wanted to do something similar, but on a form with a background image, I found that when the text in the label changed the repaints were obvious with this method, so I did the following:
* Set the label AutoSize to true and TextAlign to MiddleCenter
Then, each time the text changed (mine was done using a timer) I called the following method:
private Point GetPosition()
{
int y = (this.Height / 2) - (label1.Height / 2);
int x = (this.Width / 2) - (label1.Width / 2);
return new Point(x, y);
}
And set the label's Location property to this return value. This ensured that the label was always in the center of the form when the text changed and the repaints for a full-screen form weren't obvious.
You could try out the following code snippet:
private Point CenterOfMenuPanel<T>(T control, int height=0) where T:Control {
Point center = new Point(
MenuPanel.Size.Width / 2 - control.Width * 2,
height != 0 ? height : MenuPanel.Size.Height / 2 - control.Height / 2);
return center;
}
It's Really Center

Categories

Resources