static ICharSequence EndTruncatedFormattedIter(F9PFormattedString baseFormattedString, TextPaint paint, int secondToLastEnd, int start, int endLow, int endHigh, float availWidth) { if (endHigh - endLow <= 1) { return(baseFormattedString.ToSpannableString(EllipsePlacement.End, secondToLastEnd, start, endLow)); } int mid = (endLow + endHigh) / 2; var formattedText = baseFormattedString.ToSpannableString(EllipsePlacement.End, 0, start, mid); var layout = new StaticLayout(formattedText, paint, int.MaxValue - 10000, Android.Text.Layout.Alignment.AlignNormal, 1.0f, 0.0f, true); if (layout.GetLineWidth(0) > availWidth) { return(EndTruncatedFormattedIter(baseFormattedString, paint, secondToLastEnd, start, endLow, mid, availWidth)); } return(EndTruncatedFormattedIter(baseFormattedString, paint, secondToLastEnd, start, mid, endHigh, availWidth)); }
static ICharSequence MidTruncatedFormattedIter(F9PFormattedString baseFormattedString, TextPaint paint, int secondToLastEnd, int startLastVisible, int midLastVisible, int startLow, int startHigh, int end, float availWidth) { if (startHigh - startLow <= 1) { return(baseFormattedString.ToSpannableString(EllipsePlacement.Mid, secondToLastEnd, startHigh, end, startLastVisible, midLastVisible)); } int mid = (startLow + startHigh) / 2; var formattedText = baseFormattedString.ToSpannableString(EllipsePlacement.Mid, 0, mid, end, startLastVisible, midLastVisible); var layout = TextExtensions.StaticLayout(formattedText, paint, int.MaxValue - 10000, Android.Text.Layout.Alignment.AlignNormal, 1.0f, 0.0f, true); if (layout.GetLineWidth(0) > availWidth) { return(MidTruncatedFormattedIter(baseFormattedString, paint, secondToLastEnd, startLastVisible, midLastVisible, mid, startHigh, end, availWidth)); } return(MidTruncatedFormattedIter(baseFormattedString, paint, secondToLastEnd, startLastVisible, midLastVisible, startLow, mid, end, availWidth)); }
static ICharSequence StartTruncatedFormattedIter(F9PFormattedString baseFormattedString, TextPaint paint, int secondToLastEnd, int startLow, int startHigh, int end, float availWidth, float lineHeightMultiplier) { if (startHigh - startLow <= 1) { return(baseFormattedString.ToSpannableString(EllipsePlacement.Start, secondToLastEnd, startHigh, end)); } int mid = (startLow + startHigh) / 2; var formattedText = baseFormattedString.ToSpannableString(EllipsePlacement.Start, 0, mid, end); using (var layout = TextExtensions.StaticLayout(formattedText, paint, int.MaxValue - 10000, Android.Text.Layout.Alignment.AlignNormal, lineHeightMultiplier, 0.0f, true)) { if (layout.GetLineWidth(0) > availWidth) { return(StartTruncatedFormattedIter(baseFormattedString, paint, secondToLastEnd, mid, startHigh, end, availWidth, lineHeightMultiplier)); } return(StartTruncatedFormattedIter(baseFormattedString, paint, secondToLastEnd, startLow, mid, end, availWidth, lineHeightMultiplier)); } }
//bool _truncating; internal static StaticLayout Truncate(string text, F9PFormattedString baseFormattedString, TextPaint paint, int availWidth, int availHeight, AutoFit fit, LineBreakMode lineBreakMode, ref int lines, ref ICharSequence textFormatted) { StaticLayout layout = null; var fontMetrics = paint.GetFontMetrics(); var fontLineHeight = fontMetrics.Descent - fontMetrics.Ascent; var fontLeading = System.Math.Abs(fontMetrics.Bottom - fontMetrics.Descent); textFormatted = ((ICharSequence)baseFormattedString?.ToSpannableString()) ?? new Java.Lang.String(text); if (lines > 0) { if (baseFormattedString != null) { layout = new StaticLayout(textFormatted, paint, availWidth, Android.Text.Layout.Alignment.AlignNormal, 1.0f, 0.0f, true); if (layout.Height > availHeight) { var visibleLines = (int)((fontLeading + availHeight) / (fontLineHeight + fontLeading)); if (visibleLines < lines) { lines = visibleLines; } } if (layout.LineCount > lines && lines > 0) { var secondToLastEnd = lines > 1 ? layout.GetLineEnd(lines - 2) : 0; var start = lines > 1 ? layout.GetLineStart(layout.LineCount - 2) : 0; switch (lineBreakMode) { case LineBreakMode.HeadTruncation: textFormatted = StartTruncatedFormatted(baseFormattedString, paint, secondToLastEnd, start, layout.GetLineEnd(layout.LineCount - 1), availWidth); break; case LineBreakMode.MiddleTruncation: textFormatted = MidTruncatedFormatted(baseFormattedString, paint, secondToLastEnd, layout.GetLineStart(lines - 1), (layout.GetLineEnd(lines - 1) + layout.GetLineStart(lines - 1)) / 2 - 1, start, layout.GetLineEnd(layout.LineCount - 1), availWidth); break; case LineBreakMode.TailTruncation: textFormatted = EndTruncatedFormatted(baseFormattedString, paint, secondToLastEnd, layout.GetLineStart(lines - 1), layout.GetLineEnd(layout.LineCount - 1), availWidth); break; default: //textFormatted = baseFormattedString.ToSpannableString(EllipsePlacement.None, 0, 0, layout.GetLineEnd(lines - 1)); textFormatted = TruncatedFormatted(baseFormattedString, paint, secondToLastEnd, layout.GetLineStart(lines - 1), layout.GetLineEnd(layout.LineCount - 1), availWidth); break; } } } else { layout = new StaticLayout(text, paint, availWidth, Android.Text.Layout.Alignment.AlignNormal, 1.0f, 0.0f, true); if (layout.Height > availHeight) { var visibleLines = (int)((fontLeading + availHeight) / (fontLineHeight + fontLeading)); if (visibleLines < lines) { lines = visibleLines; } } if (layout.LineCount > lines && lines > 0) { var secondToLastEnd = lines > 1 ? layout.GetLineEnd(lines - 2) : 0; var start = lines > 1 ? layout.GetLineStart(layout.LineCount - 2) : 0; switch (lineBreakMode) { case LineBreakMode.HeadTruncation: text = StartTruncatedLastLine(text, paint, secondToLastEnd, start, layout.GetLineEnd(layout.LineCount - 1), availWidth); break; case LineBreakMode.MiddleTruncation: text = MidTruncatedLastLine(text, paint, secondToLastEnd, layout.GetLineStart(lines - 1), (layout.GetLineEnd(lines - 1) + layout.GetLineStart(lines - 1)) / 2 - 1, start, layout.GetLineEnd(layout.LineCount - 1), availWidth); break; case LineBreakMode.TailTruncation: text = EndTruncatedLastLine(text, paint, secondToLastEnd, layout.GetLineStart(lines - 1), layout.GetLineEnd(layout.LineCount - 1), availWidth); break; default: text = text.Substring(0, layout.GetLineEnd(lines - 1)); break; } textFormatted = new Java.Lang.String(text); } } } return(new StaticLayout(textFormatted, paint, availWidth, Android.Text.Layout.Alignment.AlignNormal, 1.0f, 0.0f, true)); }