When it have a long text that is not being edited, I want to show 2 or 3 lines with ellipsis and when that field is being edited the full text should be visible. For this I have tried this
public class TruncatedEditor : Editor
{
private string _fullText;
private const int MaxCharsForTruncatedText = 100;
public TruncatedEditor()
{
this.Focused += TruncatedEditor_Focused;
this.Unfocused += TruncatedEditor_Unfocused;
this.AutoSize = EditorAutoSizeOption.TextChanges;
}
private void TruncatedEditor_Unfocused(object sender, FocusEventArgs e)
{
// Store the full text
_fullText = Text;
// Truncate text with ellipsis
if (_fullText != null && _fullText.Length > MaxCharsForTruncatedText)
{
Text = _fullText.Substring(0, MaxCharsForTruncatedText - 3) + "...";
}
InvalidateMeasure();
}
private void TruncatedEditor_Focused(object sender, FocusEventArgs e)
{
Text = _fullText;
if (!string.IsNullOrEmpty(Text))
{
CursorPosition = Text.Length;
SelectionLength = 0;
}
InvalidateMeasure();
}
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
// Measure the height based on the current text and width constraint
var size = base.OnMeasure(widthConstraint, heightConstraint);
var textLength = Text?.Length ?? 0;
// Estimate the number of lines needed based on character count and width
int charsPerLine = (int)(widthConstraint / 7); // Adjust average character width as needed
int lineCount = (int)Math.Ceiling((double)textLength / charsPerLine);
// Adjust height based on the estimated number of lines
double newHeight = lineCount * size.Request.Height;
return new SizeRequest(new Size(size.Request.Width, newHeight));
}
}
# Used in page like this:
<ctrls:TruncatedEditor
x:Name="customEditor"
Placeholder="Enter here...."
HorizontalOptions="Fill"
Text="{Binding Source={x:Reference customEditorm},Path=Text}"/>
This does work on Android, but in iOS the Cursor is not getting set to the last position when getting focus, it getting set in between anywhere in the editor. Plus the OnMesure
method is also not getting called in iOS simulator, does anyone know how to handle this for different devices like Tablet, iPad?