I'm making app with using Xamarin.forms.
My problem is there is no way to create TWO line TextView. The text will be automatically go to next line even if I set enough width.
If I want to create single line, I set singleline property as true and setMaxLine as 1.
But the Two line, If I set both of LineNumber and MaxLineNumber, it does not work. Text will makes third line. for example, if text value is "I'm a baby not robot.\\nPlease give me a milk", it goes like
I'm a baby not robot.
Please give me
a milk
How to solve this problem?
Thanks.
By default all the EditText
widgets in Android are multi-lined.
Here is some sample code:
<EditText
android:inputType="textMultiLine" <!-- Multiline input -->
android:lines="8" <!-- Total Lines prior display -->
android:minLines="6" <!-- Minimum lines -->
android:gravity="top|left" <!-- Cursor Position -->
android:maxLines="10" <!-- Maximum Lines -->
android:layout_height="wrap_content" <!-- Height determined by content -->
android:layout_width="fill_parent" <!-- Fill entire width -->
android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>
But the Two line, If I set both of LineNumber and MaxLineNumber, it does not work. Text will makes third line.
You can create a custom label control and consume it in your Xaml:
CustomLabel:
public class CustomLabel:Label
{
}
Xaml:
<ContentPage ...
xmlns:local="clr-namespace:LabelDemo"
...>
<local:CustomLabel Text="I'm a baby not robot. \nPlease give me a milk" VerticalOptions="Center" HorizontalOptions="Center" />
Then create a custom renderer in your Droid project like below:
using Android.Widget;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
[assembly:ExportRenderer(typeof(LabelDemo.CustomLabel),typeof(LabelDemo.Droid.MyLabelRenderer))]
namespace LabelDemo.Droid
{
public class MyLabelRenderer:LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var text = Control.Text;
text = text.Replace("\\n", "\n");//"\n" in shared project will be automatically transformed into "\\n", so we need to change it back
Control.SetLines(2);
Control.SetText(text, TextView.BufferType.Normal);
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.