I have a label in my XAML that I bind to my data and sometimes the text is longer than others and if the text is long enough to get into 2 rows, i want to adjust my code a little bit to make the UI look better.
Is there a way to solve that in xamarin forms?
XAML
<Label x:Name = "title" />
CODE
title.Text = Title; //title is a string cointaining the text.
So do something with:
if(title.text > 2 (rows?))
{
//change the ui.
}
Or if there is a solution that sees if a label doesnt fit on a the row. so if i have a absolutelayout that only allows 1 row and the label recieves the "..." because it all cannot fit. can you do something with that?
if (title.Text.Contains == "...")
{
// change size of label
}
No you can't do that like that, as xamarin forms uses "plaftorm renderers", specific to each platform, to render your string.
In fact you have no way to know if your label will split on more than one line. What you can do is count the number of chars that can fit on one line on most devices, and customize your rendering based on this value.
If you still want to compute the number of lines, you can do it using specific platform code that needs the width in point of your label. For example on iOS you will use NSString GetSizeUsingAttributes like described here: https://forums.xamarin.com/discussion/10016/measuring-string-width-getsizeusingattributes
Related
My goal is to display text with feature, that you can tap on any word of displayed text and it will do something with this selected word.
I choose the label approach. It means that every word in text will be in it's own label. And then I can rise tapped event on that label.
But I can't find out how to format that "group" of labels into "nice" formated text.
My idea was:
private void ReadPageViewModel_ReloadPageEvent(string text)
{
MyLayout.Spacing = 2; //MyLayout is StackLayout
MyLayout.Orientation = StackOrientation.Horizontal;
//MyLayout.HorizontalOptions = LayoutOptions.EndAndExpand;
var splitedtText = text.Split(';');
foreach(var item in splitedtText)
{
MyLayout.Children.Add(new Label {Text = item , HorizontalOptions = LayoutOptions.FillAndExpand });
}
}
Every try results in wrong formated text.
Could you give me some advice? Am I using wrong layout? Or is there some easy trick that I don't know?
EDIT:
Screenshots:
This is just text inserted into label, this is what I want to achieve with 1 label for every word in this text.
One of many tries (result of sample above)
Thanks for your time. I found answer. Something like wrappanel was what I was looking for. But xamarin.forms doesn't have one so I had to help myself.
I found this wrappanel classes
https://gist.github.com/NicoVermeir/7ffb34ebd639ed958382 and
https://github.com/conceptdev/xamarin-forms-samples/blob/master/Evolve13/Evolve13/Controls/WrapLayout.cs
Pick one wrappanel class and include it in project. Then add namespace to your view and it is all.
xmlns:controls="clr-namespace:SecretProject75.Controls;assembly=SecretProject75"
<controls:AwesomeWrappanel Grid.Column="1" x:Name="MyWarappicek" Orientation="Horizontal"></controls:AwesomeWrappanel>
I have a GUI Program, that creates labels containing names of files according to the search query provided by the user at runtime and displays them. The no of labels displayed can vary considerably based on the user input.
The no can vary between 0 to 2000(approx) . When the no of labels exceed 1000 or so, the time taken for the form in which the labels are contained takes a lot of time to display.
The time taken to display the form completely outweighs the time saved in making a good algorithm(for some cases)!. I am looking for an efficient way so that I can display the form easily in less amount of time. Currently, It takes 1minute 45 seconds to display a form containing 1499 Labels.
Currently , I am doing like this:-
foreach(string Elements in FileList)
{
Label LabelA = new Label() ;
// other code here to modify the appearence of label
LabelA.Show() ;
}
MyForm.Show();
// File List is the List of file names which are to be displayed.
//MyForm is the name of the form in which labels are added at runtime.
There could be at least a couple of techniques:
it's technically impossible that one can look on all of them contemporary, so solution is just devide them into Tabs. So divide them into groups of your application logic.
defered scrolling. So when user scrolls the window, only the data inside them changed (and also corresponding data field)
Just to give an idea, but basically to invite attention to a simple fact that you for sure do need all that controls at once on UI. If these solutions are not ok, change control, so rapresentation of data.
Asyn get result Maybe you can use BackGroundWorker to asynchronously query the result and create the label. The way will give you a well user experience .
Suspend then Resume When you add the label you should use Control.SuspendLayout() and Control.ResumeLayout()
Page the Result Other way you can page for the result.
If you want to use labels with that create labels on program startup and add them a list for instant access to them
// create a list on there
List<Label> lblList = new List<Label>;
public Form1()
{
InitializeComponent();
for(int i = 0 ; i<1500;i++)
{
Label LabelA = new Label() ;
// other code here to modify the appearence of label
LabelA.Show() ;
lblList.Add(LabelA);
}
}
if you do this on a backGroundWorker your program will open fast and you can make a prograss bar for label creation. this will create labels and add them a list. You can access them with their indexes and change their text like that:
int index=0;
foreach(Label o in lblList)
{
o.Text="Text";
index++;//you can use this index if you want to know which label you editing
}
if you create labels on start up this will take some time but when you use them this way will be faster from creating them and values will display instantly.
Sorry for bad english.
I am creating an application where I need to loop through a series of text lines that may contain whitespace.
I build up a string by doing my_string += the_line_to_add and update the Text property of the TextBlock with the final string.
Pretty simple actually, however, a line that looks like this:
"a b c"
will end up as follows:
"a b c"
I don't want all of those spaces to be removed though. I want the line to keep the extra spaces and remain unchanged:
"a b c"
The TextBlock is created programmatically and added into a StackPanel. I looked at the different properties but just can't figure it out.
Honestly, I'd approach this problem differently. I wouldn't use whitespaces in a string to layout the text. If you need 3 string in the screen add 3 textboxes and set Margin proprety. This depends on the input text, but if there will be too many whitespaces the text will be out of the screen.
Alternatively, you can use Run to format the text.
I've been having this problem for a few days. Whenever I update a label in a StatusStrip object it formats my text backwards. I send my label something like
toolStripVoltage.Text = batteryVoltage.ToString("F2") + " V";
and the label will display V 2.82.
and when I send it something like
toolStripVoltage.Text = batteryVoltage.ToString("0.00 V");
it will display the same thing. It seems like no matter how I format the string the "V" goes before the numbers. and! it still puts a space in between the unit and the number. And here's the kicker: when I call this same text to appear in a tooltip of another object like this
toolStripVoltage.ToolTipText = toolStripVoltage.Text;
It displays as 2.82 V. Any ideas on how I can make this work for me?
EDIT:
oh wow. I instantly figured this out somehow...the default RightToLeft property is Yes. I don't know why that would be! but the trick was to set that to No. Very strange for that to be the default setting.
I am using label in my winform . It displays the value which I enter in another textbox. My problem is label does not display whole characters I enter in textbox. Label's size is width=160 and height=19. So it truncates the last value. For testing purpose when I enter "W" in caps in textbox ;label can display maximum 13 "W"s. So I am trimming the
charater's by using labelname.substring(0,10); and for next three characters I am appending 3 dots(...)
But this solution is not desirable to my senior. He is telling me that if I enter all small letters "l" in textbox then though label has space to display more than 13 characters it will display only 13 characters(including dots).
Does anybody has solution on that ?? I also cannot increase width or height of label to accomodate more characters.
Well, you could set the AutoEllipsis property to true and don't worry about the length of the text.
Edited to Add: as per comments
If you're using RadLabel from Telerik then you need to dig a little more:
RadLabel.LabelElement.LabelText.AutoEllipsis
Nothing that 5 minutes looking through the documentation doens't solve
Set the AutoEllipsis property of your Label control to true. You can do this either in the designer, or through code:
myLabel.AutoEllipsis = true;
That will cause the ellipsis character (...) to be automatically appended to the text if it overflows the space available in the label. You won't have to manage this yourself in code at all.
This property is available as far back as .NET 3.0.
If I understand your question correctly, you can use Textbox.MaxLength property which only allows the user to enter the maximum number of characters you set the value to.
You can set the label's AutoEllipsis property to true and let it figure this out for itself.
Gets or sets a value indicating
whether the ellipsis character (...)
appears at the right edge of the
Label, denoting that the Label text
extends beyond the specified length of
the Label.