示例#1
0
 public static void UpdateLineBreakMode(this Button platformButton, ILineBreakMode button)
 {
     if (platformButton.GetContent <TextBlock>() is TextBlock textBlock)
     {
         textBlock?.UpdateLineBreakMode(button);
     }
 }
示例#2
0
        public static void UpdateLineBreakMode(this TextBlock platformControl, ILineBreakMode breakMode)
        {
            var lineBreakMode = breakMode.LineBreakMode;
            var label         = breakMode as ILabel;

            switch (lineBreakMode)
            {
            case LineBreakMode.NoWrap:
                platformControl.TextTrimming = TextTrimming.Clip;
                platformControl.TextWrapping = TextWrapping.NoWrap;
                break;

            case LineBreakMode.WordWrap:
                platformControl.TextTrimming = TextTrimming.None;
                platformControl.TextWrapping = TextWrapping.Wrap;
                break;

            case LineBreakMode.CharacterWrap:
                platformControl.TextTrimming = TextTrimming.WordEllipsis;
                platformControl.TextWrapping = TextWrapping.Wrap;
                break;

            case LineBreakMode.HeadTruncation:
                // TODO: This truncates at the end.
                platformControl.TextTrimming = TextTrimming.WordEllipsis;

                if (label != null)
                {
                    platformControl.DetermineTruncatedTextWrapping(label);
                }
                break;

            case LineBreakMode.TailTruncation:
                platformControl.TextTrimming = TextTrimming.CharacterEllipsis;

                if (label != null)
                {
                    platformControl.DetermineTruncatedTextWrapping(label);
                }
                break;

            case LineBreakMode.MiddleTruncation:
                // TODO: This truncates at the end.
                platformControl.TextTrimming = TextTrimming.WordEllipsis;

                if (label != null)
                {
                    platformControl.DetermineTruncatedTextWrapping(label);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
示例#3
0
     public static void UpdateLineBreakMode(this UIButton nativeButton, ILineBreakMode button)
     {
         nativeButton.TitleLabel.LineBreakMode = button.LineBreakMode switch
         {
             LineBreakMode.NoWrap => UILineBreakMode.Clip,
             LineBreakMode.WordWrap => UILineBreakMode.WordWrap,
             LineBreakMode.CharacterWrap => UILineBreakMode.CharacterWrap,
             LineBreakMode.HeadTruncation => UILineBreakMode.HeadTruncation,
             LineBreakMode.TailTruncation => UILineBreakMode.TailTruncation,
             LineBreakMode.MiddleTruncation => UILineBreakMode.MiddleTruncation,
             _ => throw new ArgumentOutOfRangeException()
         };
     }
 }
示例#4
0
 public static void UpdateLineBreakMode(this Button nativeControl, ILineBreakMode button)
 {
     nativeControl.SetLineBreakMode(button);
 }
示例#5
0
        internal static void SetLineBreakMode(this TextView textView, ILineBreakMode breakMode, int?maxLines = null)
        {
            var lineBreakMode = breakMode.LineBreakMode;

            if (breakMode is ILabel label)
            {
                maxLines = label.MaxLines;
            }

            if (!maxLines.HasValue || maxLines <= 0)
            {
                maxLines = int.MaxValue;
            }

            bool singleLine          = false;
            bool shouldSetSingleLine = !OperatingSystem.IsAndroidVersionAtLeast(23);

            switch (lineBreakMode)
            {
            case LineBreakMode.NoWrap:
                maxLines           = 1;
                textView.Ellipsize = null;
                break;

            case LineBreakMode.WordWrap:
                textView.Ellipsize = null;
                break;

            case LineBreakMode.CharacterWrap:
                textView.Ellipsize = null;
                break;

            case LineBreakMode.HeadTruncation:
                maxLines = 1;                         // If maxLines is anything greater than 1, the truncation will be ignored: https://developer.android.com/reference/android/widget/TextView#setEllipsize(android.text.TextUtils.TruncateAt)

                if (shouldSetSingleLine)
                {
                    singleLine = true;                             // Workaround for bug in older Android API versions (https://issuetracker.google.com/issues/36950033) (https://bugzilla.xamarin.com/show_bug.cgi?id=49069)
                }

                textView.Ellipsize = TextUtils.TruncateAt.Start;
                break;

            case LineBreakMode.TailTruncation:

                // Leaving this in for now to preserve existing behavior
                // Technically, we don't _need_ this for Labels; they will handle Ellipsization at the end just fine, even with multiple lines
                // But we don't have a mechanism for setting MaxLines on other controls (e.g., Button) right now, so we need to force it here or
                // they will potentially exceed a single line. Also, changing this behavior the for Labels would technically be breaking (though
                // possibly less surprising than what happens currently).
                maxLines           = 1;
                textView.Ellipsize = TextUtils.TruncateAt.End;
                break;

            case LineBreakMode.MiddleTruncation:
                maxLines           = 1;               // If maxLines is anything greater than 1, the truncation will be ignored: https://developer.android.com/reference/android/widget/TextView#setEllipsize(android.text.TextUtils.TruncateAt)
                textView.Ellipsize = TextUtils.TruncateAt.Middle;
                break;
            }

            if (shouldSetSingleLine)             // Save ourselves this trip across the bridge if we're on an API level that doesn't need it
            {
                textView.SetSingleLine(singleLine);
            }

            textView.SetMaxLines(maxLines.Value);
        }