[Android] If MaxLines is not explicitly set, let LineBreakMode handle it (#3936)
* [Android] If MaxLines is not explicitly set, all LineBreakMode to set it; if MaxLines is explicitly set, MaxLines setting wins. Use a more reasonable default for native MaxLines when handling it via LineBreakMode. Consolidate MaxLines handling code between legacy and fast renderers. Remove unnecessary setting of SingleLine (which only applies to input). - fixes #3772 * Remove unnecessary maxLines setting * Handle resetting MaxLines to -1/default * Reinstate workaround for array bounds exception in older Android APIs
This commit is contained in:
Родитель
f3f39f8022
Коммит
480d75370e
|
@ -1,4 +1,5 @@
|
|||
using Android.Text;
|
||||
using System;
|
||||
using Android.Text;
|
||||
using Android.Widget;
|
||||
using System.Collections.Generic;
|
||||
using Xamarin.Forms.Internals;
|
||||
|
@ -7,41 +8,68 @@ namespace Xamarin.Forms.Platform.Android
|
|||
{
|
||||
internal static class TextViewExtensions
|
||||
{
|
||||
public static void SetLineBreakMode(this TextView textView, LineBreakMode lineBreakMode)
|
||||
public static void SetMaxLines(this TextView textView, Label label)
|
||||
{
|
||||
var maxLines = label.MaxLines;
|
||||
|
||||
if (maxLines == (int)Label.MaxLinesProperty.DefaultValue)
|
||||
{
|
||||
// MaxLines is not explicitly set, so just let it be whatever gets set by LineBreakMode
|
||||
textView.SetLineBreakMode(label);
|
||||
return;
|
||||
}
|
||||
|
||||
textView.SetMaxLines(maxLines);
|
||||
}
|
||||
|
||||
static void SetMaxLines(this TextView textView, Label label, int lines)
|
||||
{
|
||||
// If the Label's MaxLines has been explicitly set, we should not set it here
|
||||
if (label.MaxLines != (int)Label.MaxLinesProperty.DefaultValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
textView.SetMaxLines(lines);
|
||||
}
|
||||
|
||||
public static void SetLineBreakMode(this TextView textView, Label label)
|
||||
{
|
||||
var lineBreakMode = label.LineBreakMode;
|
||||
|
||||
int maxLines = Int32.MaxValue;
|
||||
bool singleLine = false;
|
||||
|
||||
switch (lineBreakMode)
|
||||
{
|
||||
case LineBreakMode.NoWrap:
|
||||
textView.SetMaxLines(1);
|
||||
textView.SetSingleLine(true);
|
||||
maxLines = 1;
|
||||
textView.Ellipsize = null;
|
||||
break;
|
||||
case LineBreakMode.WordWrap:
|
||||
textView.Ellipsize = null;
|
||||
textView.SetMaxLines(100);
|
||||
textView.SetSingleLine(false);
|
||||
break;
|
||||
case LineBreakMode.CharacterWrap:
|
||||
textView.Ellipsize = null;
|
||||
textView.SetMaxLines(100);
|
||||
textView.SetSingleLine(false);
|
||||
break;
|
||||
case LineBreakMode.HeadTruncation:
|
||||
textView.SetMaxLines(1);
|
||||
textView.SetSingleLine(true);
|
||||
maxLines = 1;
|
||||
singleLine = true; // Workaround for bug in older Android API versions (https://bugzilla.xamarin.com/show_bug.cgi?id=49069)
|
||||
textView.Ellipsize = TextUtils.TruncateAt.Start;
|
||||
break;
|
||||
case LineBreakMode.TailTruncation:
|
||||
textView.SetMaxLines(1);
|
||||
textView.SetSingleLine(true);
|
||||
maxLines = 1;
|
||||
textView.Ellipsize = TextUtils.TruncateAt.End;
|
||||
break;
|
||||
case LineBreakMode.MiddleTruncation:
|
||||
textView.SetMaxLines(1);
|
||||
textView.SetSingleLine(true);
|
||||
maxLines = 1;
|
||||
singleLine = true; // Workaround for bug in older Android API versions (https://bugzilla.xamarin.com/show_bug.cgi?id=49069)
|
||||
textView.Ellipsize = TextUtils.TruncateAt.Middle;
|
||||
break;
|
||||
}
|
||||
|
||||
textView.SetSingleLine(singleLine);
|
||||
textView.SetMaxLines(label, maxLines);
|
||||
}
|
||||
|
||||
public static void RecalculateSpanPositions(this TextView textView, Label element, SpannableString spannableString, SizeRequest finalSize)
|
||||
|
|
|
@ -317,14 +317,13 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
|
|||
|
||||
void UpdateLineBreakMode()
|
||||
{
|
||||
this.SetLineBreakMode(Element.LineBreakMode);
|
||||
this.SetLineBreakMode(Element);
|
||||
_lastSizeRequest = null;
|
||||
}
|
||||
|
||||
void UpdateMaxLines()
|
||||
{
|
||||
SetSingleLine(Element.MaxLines == 1);
|
||||
SetMaxLines(Element.MaxLines > 0 ? Element.MaxLines : 1);
|
||||
this.SetMaxLines(Element);
|
||||
}
|
||||
|
||||
void UpdateText()
|
||||
|
|
|
@ -211,7 +211,7 @@ namespace Xamarin.Forms.Platform.Android
|
|||
|
||||
void UpdateLineBreakMode()
|
||||
{
|
||||
_view.SetLineBreakMode(Element.LineBreakMode);
|
||||
_view.SetLineBreakMode(Element);
|
||||
_lastSizeRequest = null;
|
||||
}
|
||||
|
||||
|
@ -226,8 +226,7 @@ namespace Xamarin.Forms.Platform.Android
|
|||
|
||||
void UpdateMaxLines()
|
||||
{
|
||||
Control.SetSingleLine(Element.MaxLines == 1);
|
||||
Control.SetMaxLines(Element.MaxLines > 0 ? Element.MaxLines : 1);
|
||||
Control.SetMaxLines(Element);
|
||||
}
|
||||
|
||||
void UpdateText()
|
||||
|
|
Загрузка…
Ссылка в новой задаче