public void InsertText_(NSObject text, NSRange replacementRange) { if (replacementRange.Location != NSRange.NotFound) { _control.Select((int)replacementRange.Location, (int)replacementRange.Length); } NSAttributedString attributedText = text as NSAttributedString; if (attributedText != null) { _control.SelectedText = attributedText.ToString(); _control.UpdateLayout(); } else { _control.SelectedText = text.ToString(); _control.UpdateLayout(); } _control.CaretIndex = _control.SelectionStart + _control.SelectionLength; }
/// <summary> /// Gets the size of the desired. /// </summary> /// <returns>The desired size.</returns> /// <param name="widthConstraint">Width constraint.</param> /// <param name="heightConstraint">Height constraint.</param> public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) { if (widthConstraint < 0 || heightConstraint < 0) { return(new SizeRequest(Size.Zero)); } if (Control == null || Element == null) { return(new SizeRequest(Size.Zero)); } if (string.IsNullOrEmpty((ControlText ?? ControlAttributedText?.ToString()))) { return(new SizeRequest(Size.Zero)); } UpdateFont(); //if (Invalid || Math.Abs(widthConstraint - LastWidthConstraint) > 0.01 || Math.Abs(heightConstraint - LastHeightContraint) > 0.01 || Math.Abs(Element.MinFontSize - LastMinFontSize) > 0.01) { LastWidthConstraint = widthConstraint; LastHeightContraint = heightConstraint; LastMinFontSize = Element.MinFontSize; switch (Element.LineBreakMode) { case LineBreakMode.HeadTruncation: ControlLineBreakMode = UILineBreakMode.HeadTruncation; break; case LineBreakMode.TailTruncation: ControlLineBreakMode = UILineBreakMode.TailTruncation; break; case LineBreakMode.MiddleTruncation: ControlLineBreakMode = UILineBreakMode.MiddleTruncation; break; case LineBreakMode.NoWrap: ControlLineBreakMode = UILineBreakMode.Clip; ControlLines = 1; break; case LineBreakMode.CharacterWrap: ControlLineBreakMode = UILineBreakMode.CharacterWrap; break; case LineBreakMode.WordWrap: default: ControlLineBreakMode = UILineBreakMode.WordWrap; break; } var tmpFontSize = (nfloat)Element.FontSize; if (tmpFontSize < 0) { tmpFontSize = (nfloat)(UIFont.LabelFontSize * Math.Abs(tmpFontSize)); } if (Math.Abs(tmpFontSize) <= double.Epsilon * 10) { tmpFontSize = UIFont.LabelFontSize; } var minFontSize = (nfloat)LastMinFontSize; if (minFontSize < 0) { minFontSize = 4; } if (tmpFontSize < minFontSize) { tmpFontSize = minFontSize; } ControlLines = int.MaxValue; double linesHeight = -1; double desiredWidth = widthConstraint; if (Element.Lines == 0) { if (!double.IsInfinity(heightConstraint)) { ControlLines = 0; tmpFontSize = ZeroLinesFit(widthConstraint, heightConstraint, tmpFontSize); } } else { if (Element.AutoFit == AutoFit.Lines) { if (double.IsPositiveInfinity(heightConstraint)) { linesHeight = Element.Lines * (ControlFont.LineHeight + ControlFont.Leading); } else { var lineHeightRatio = ControlFont.LineHeight / ControlFont.PointSize; var leadingRatio = ControlFont.Leading / ControlFont.PointSize; //tmpFontSize = (nfloat)(((heightConstraint) / ((1 + leadingRatio) * Element.Lines)) / lineHeightRatio - 0.1f); var tmpLineSize = (nfloat)(heightConstraint - 0.05f) / Element.Lines; tmpFontSize = tmpLineSize / lineHeightRatio; } } else if (Element.AutoFit == AutoFit.Width) { tmpFontSize = WidthFit(widthConstraint, tmpFontSize); } } if (tmpFontSize < minFontSize) { tmpFontSize = minFontSize; } if (Math.Abs(tmpFontSize - Element.FittedFontSize) > 0.05) { //Device.StartTimer(TimeSpan.FromMilliseconds(50), () => //{ if (Element != null && Control != null) // multipicker test was getting here with Element and Control both null { #pragma warning disable RECS0018 // Comparison of floating point numbers with equality operator Element.FittedFontSize = tmpFontSize == Element.FontSize || (Element.FontSize == -1 && tmpFontSize == UIFont.LabelFontSize) ? -1 : (double)tmpFontSize; #pragma warning restore RECS0018 // Comparison of floating point numbers with equality operator Debug("SETTING FITTED FONT SIZE: " + Element?.FittedFontSize); } //return false; //}); } var syncFontSize = (nfloat)((ILabel)Element).SynchronizedFontSize; if (syncFontSize >= 0 && tmpFontSize != syncFontSize) { tmpFontSize = syncFontSize; } ControlFont = ControlFont.WithSize(tmpFontSize); CGSize cgSize = LabelSize(widthConstraint, tmpFontSize); Debug("cgSize: " + cgSize); //if (Control.Font != ControlFont || Control.AttributedText != ControlAttributedText || Control.Text != ControlText || Control.LineBreakMode != ControlLineBreakMode || Control.Lines != ControlLines) { Control.Hidden = true; Control.Lines = Element.Lines; Control.LineBreakMode = ControlLineBreakMode; Control.AdjustsFontSizeToFitWidth = false; Control.ClearsContextBeforeDrawing = true; Control.ContentMode = UIViewContentMode.Redraw; Control.Font = ControlFont; if (ControlAttributedText != null) { Control.AttributedText = ControlAttributedText; } else { Control.Text = ControlText; } Control.Hidden = false; } double reqWidth = cgSize.Width; double reqHeight = cgSize.Height + 0.05; var textHeight = cgSize.Height; var textLines = Lines(textHeight, Control.Font); string alg = "--"; //string cnstLinesStr = "CL: n/a "; //string lineHeight = "LH: " + Control.Font.LineHeight.ToString("00.000"); //string cnstLinesHeight = "CLH: n/a "; if (double.IsPositiveInfinity(heightConstraint)) { Debug("A"); if (Element.Lines > 0) { if (Element.AutoFit == AutoFit.Lines)// && Element.Lines <= textLines) { reqHeight = Element.Lines * Control.Font.LineHeight; } else if (Element.AutoFit == AutoFit.None && Element.Lines <= textLines) { reqHeight = Element.Lines * Control.Font.LineHeight; } } // alg = "∞A"; //} Control.Center = new CGPoint(Control.Center.X, reqHeight / 2); Debug("Control.Center: " + Control.Center); } else { Debug("B"); var constraintLines = Lines(heightConstraint, Control.Font); Debug("\t constraintLines: " + constraintLines); var constraintLinesHeight = Math.Floor(constraintLines) * Control.Font.LineHeight; Debug("\t constraintLinesHeight: " + constraintLinesHeight); //cnstLinesStr = "CL: " + constraintLines.ToString("0.000"); if (Element.Lines > 0 && Element.Lines <= Math.Min(textLines, constraintLines)) { reqHeight = Element.Lines * Control.Font.LineHeight; alg = "A"; } else if (textLines <= constraintLines) { reqHeight = textHeight; alg = "B"; } else if (constraintLines >= 1) { reqHeight = constraintLinesHeight; alg = "C"; } else { reqHeight = heightConstraint; alg = "D"; } Debug("\t alg: " + alg); Debug("\t reqHeight: " + reqHeight); Debug("\t Element.VerticalTextAlignment: " + Element.VerticalTextAlignment); if (Element.VerticalTextAlignment == TextAlignment.Start) { Control.Center = new CGPoint(Control.Center.X, reqHeight / 2); } else if (Element.VerticalTextAlignment == TextAlignment.End) { Control.Center = new CGPoint(Control.Center.X, heightConstraint - reqHeight / 2); } Debug("Control.Center: " + Control.Center); } LastDesiredSize = new SizeRequest(new Size(reqWidth, reqHeight), new Size(10, ControlFont.LineHeight)); } return(LastDesiredSize); }