void UpdateText()
        {
            if (Element.FormattedText != null)
            {
                FormattedString formattedText = Element.FormattedText ?? Element.Text;
#pragma warning disable 618 // We will need to update this when .Font goes away
                _view.TextFormatted = _spannableString = formattedText.ToAttributed(Element.Font, Element.TextColor, _view);
#pragma warning restore 618
                _wasFormatted = true;
            }
            else
            {
                if (_wasFormatted)
                {
                    _view.SetTextColor(_labelTextColorDefault);
                    _lastUpdateColor = Color.Default;
                }
                _view.Text = Element.Text;
                UpdateColor();
                UpdateFont();

                _wasFormatted = false;
            }

            _lastSizeRequest = null;
        }
        SizeRequest DrawLabel(double width, double height)
        {
            if (_currentDrawState == null ||
                _currentDrawState.IsEmpty ||
                Control == null ||
                Element == null ||
                _disposed ||
                width < 0 ||
                height < 0
                )
            {
                return(new SizeRequest(Size.Zero));
            }
            if (double.IsInfinity(width))
            {
                width = int.MaxValue / 3;
            }
            if (double.IsInfinity(height))
            {
                height = int.MaxValue / 3;
            }

            var displayScale = (float)Resources.DisplayMetrics.DensityDpi / (float)Android.Util.DisplayMetricsDensity.Default;

            _currentDrawState.AvailWidth  = (int)System.Math.Floor(width * displayScale);
            _currentDrawState.AvailHeight = (int)System.Math.Floor(height * displayScale);
            _lastDrawResult = InternalLayout(Control, _currentDrawState);
            _lastDrawState  = new TextControlState(_currentDrawState);

            return(_lastDrawResult.Value);
        }
Beispiel #3
0
        /// <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 ||
                _currentDrawState == null ||
                _currentDrawState.IsBlank ||
                Control == null ||
                Element == null)
            {
                return(new SizeRequest(Size.Zero));
            }

            //_currentMeasureState?.Dispose();
            _currentMeasureState = new TextControlState(_currentDrawState)
            {
                AvailWidth  = widthConstraint,
                AvailHeight = heightConstraint
            };

            if (_currentMeasureState == _lastMeasureState && _lastMeasureResult.HasValue)
            {
                return(_lastMeasureResult.Value);
            }

            _lastMeasureResult = InternalLayout(MeasureControl, _currentMeasureState);
            //_lastMeasureState?.Dispose();
            _lastMeasureState = new TextControlState(_currentMeasureState);

            return(_lastMeasureResult.Value);
        }
        public override SizeRequest GetDesiredSize(int widthConstraint, int heightConstraint)
        {
            if (this.lastSizeRequest.HasValue)
            {
                bool flag = widthConstraint == this.lastConstraintWidth && heightConstraint == this.lastConstraintHeight;
                if (!flag)
                {
                    int  size     = MeasureSpecFactory.GetSize(this.lastConstraintWidth);
                    int  size2    = MeasureSpecFactory.GetSize(this.lastConstraintHeight);
                    int  size3    = MeasureSpecFactory.GetSize(widthConstraint);
                    int  size4    = MeasureSpecFactory.GetSize(heightConstraint);
                    bool arg_E7_0 = this.lastSizeRequest.Value.Request.Width < (double)size && this.lastSizeRequest.Value.Request.Height < (double)size2;
                    bool flag2    = (double)size3 >= this.lastSizeRequest.Value.Request.Width && (double)size4 >= this.lastSizeRequest.Value.Request.Height;
                    flag = (arg_E7_0 & flag2);
                }
                if (flag)
                {
                    return(this.lastSizeRequest.Value);
                }
            }
            SizeRequest desiredSize = base.GetDesiredSize(widthConstraint, heightConstraint);

            desiredSize.Minimum       = new Xamarin.Forms.Size(Math.Min((double)base.Context.ToPixels(10), desiredSize.Request.Width), desiredSize.Request.Height);
            this.lastConstraintWidth  = widthConstraint;
            this.lastConstraintHeight = heightConstraint;
            this.lastSizeRequest      = new SizeRequest?(desiredSize);
            return(desiredSize);
        }
        Size LabelF9pSize(double widthConstraint, double fontSize)
        {
            if (_currentDrawState == null ||
                _currentDrawState.IsEmpty ||
                Control == null ||
                Element == null ||
                _disposed)
            {
                return(Size.Zero);
            }

            var displayScale = (float)Resources.DisplayMetrics.DensityDpi / (float)Android.Util.DisplayMetricsDensity.Default;

            _currentMeasureState = new TextControlState(_currentDrawState)
            {
                AvailWidth  = (int)System.Math.Floor(System.Math.Min(widthConstraint * displayScale, int.MaxValue / 3)),
                AvailHeight = int.MaxValue / 3,
                TextSize    = (float)fontSize,
            };

            _measureControl    = _measureControl ?? new F9PTextView(Settings.Context);
            _lastMeasureResult = InternalLayout(_measureControl, _currentMeasureState);
            _lastMeasureState  = new TextControlState(_currentMeasureState);

            return(new Size(_lastMeasureResult.Value.Request.Width / displayScale, _lastMeasureResult.Value.Request.Height / displayScale));
        }
        private void UpdateGravity()
        {
            SpanView element = base.Element;

            this.view.Gravity    = (ToHorizontalGravityFlags(element.HorizontalTextAlignment) | ToVerticalGravityFlags(element.VerticalTextAlignment));
            this.lastSizeRequest = null;
        }
        void UpdateGravity()
        {
            Label label = Element;

            _view.Gravity = label.HorizontalTextAlignment.ToHorizontalGravityFlags() | label.VerticalTextAlignment.ToVerticalGravityFlags();

            _lastSizeRequest = null;
        }
        void UpdatePadding()
        {
            SetPadding(
                (int)Context.ToPixels(Element.Padding.Left),
                (int)Context.ToPixels(Element.Padding.Top),
                (int)Context.ToPixels(Element.Padding.Right),
                (int)Context.ToPixels(Element.Padding.Bottom));

            _lastSizeRequest = null;
        }
		public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
		{
			if (Element?.GetDesiredSizeDelegate == null)
				return base.GetDesiredSize(widthConstraint, heightConstraint);

			// The user has specified a different implementation of GetDesiredSize
			SizeRequest? result = Element.GetDesiredSizeDelegate(this, widthConstraint, heightConstraint);

			// If the delegate returns a SizeRequest, we use it; 
			// if it returns null, fall back to the default implementation
			return result ?? base.GetDesiredSize(widthConstraint, heightConstraint);
		}
 void UpdateLineHeight()
 {
     _lastSizeRequest = null;
     if (Element.LineHeight == -1)
     {
         _view.SetLineSpacing(_lineSpacingExtraDefault, _lineSpacingMultiplierDefault);
     }
     else if (Element.LineHeight >= 0)
     {
         _view.SetLineSpacing(0, (float)Element.LineHeight);
     }
 }
        public override SizeRequest GetDesiredSize(int widthConstraint, int heightConstraint)
        {
            if (_lastSizeRequest.HasValue)
            {
                // if we are measuring the same thing, no need to waste the time
                bool canRecycleLast = widthConstraint == _lastConstraintWidth && heightConstraint == _lastConstraintHeight;

                if (!canRecycleLast)
                {
                    // if the last time we measured the returned size was all around smaller than the passed constraint
                    // and the constraint is bigger than the last size request, we can assume the newly measured size request
                    // will not change either.
                    int lastConstraintWidthSize  = MeasureSpecFactory.GetSize(_lastConstraintWidth);
                    int lastConstraintHeightSize = MeasureSpecFactory.GetSize(_lastConstraintHeight);

                    int currentConstraintWidthSize  = MeasureSpecFactory.GetSize(widthConstraint);
                    int currentConstraintHeightSize = MeasureSpecFactory.GetSize(heightConstraint);

                    bool lastWasSmallerThanConstraints = _lastSizeRequest.Value.Request.Width < lastConstraintWidthSize && _lastSizeRequest.Value.Request.Height < lastConstraintHeightSize;

                    bool currentConstraintsBiggerThanLastRequest = currentConstraintWidthSize >= _lastSizeRequest.Value.Request.Width && currentConstraintHeightSize >= _lastSizeRequest.Value.Request.Height;

                    canRecycleLast = lastWasSmallerThanConstraints && currentConstraintsBiggerThanLastRequest;
                }

                if (canRecycleLast)
                {
                    return(_lastSizeRequest.Value);
                }
            }

            //We need to clear the Hint or else it will interfere with the sizing of the Label
            var hint = Control.Hint;

            if (!string.IsNullOrEmpty(hint))
            {
                Control.Hint = string.Empty;
            }

            SizeRequest result = base.GetDesiredSize(widthConstraint, heightConstraint);

            //Set Hint back after sizing
            Control.Hint = hint;

            result.Minimum = new Size(Math.Min(Context.ToPixels(10), result.Request.Width), result.Request.Height);

            _lastConstraintWidth  = widthConstraint;
            _lastConstraintHeight = heightConstraint;
            _lastSizeRequest      = result;

            return(result);
        }
Beispiel #12
0
        /// <summary>
        /// Raises the element changed event.
        /// </summary>
        /// <param name="e">E.</param>
        protected override void OnElementChanged(ElementChangedEventArgs <Label> e)
        {
            base.OnElementChanged(e);

            _lastMeasureState  = null;
            _lastDrawResult    = null;
            _lastMeasureResult = null;
            _measureControl?.Dispose();
            _measureControl   = null;
            _currentDrawState = new TextControlState
            {
                Lines         = e.NewElement.Lines,
                AutoFit       = e.NewElement.AutoFit,
                LineBreakMode = e.NewElement.LineBreakMode,
                SyncFontSize  = (float)e.NewElement.SynchronizedFontSize,
            };

            if (e.OldElement != null)
            {
                e.OldElement.RendererIndexAtPoint            -= IndexAtPoint;
                e.OldElement.RendererSizeForWidthAndFontSize -= LabelF9pSize;
                e.OldElement.Draw -= DrawLabel;
                Control?.SkipNextInvalidate();
            }
            if (e.NewElement != null)
            {
                if (Control == null)
                {
                    var view = new F9PTextView(Context);
                    InitControl(view);
                    SetNativeControl(view);
                }
                else
                {
                    InitControl(Control);
                }
                Control.IsNativeDrawEnabled = false;

                e.NewElement.RendererIndexAtPoint            += IndexAtPoint;
                e.NewElement.RendererSizeForWidthAndFontSize += LabelF9pSize;
                e.NewElement.Draw += DrawLabel;

                if (e.NewElement.Width > 0 && e.NewElement.Height > 0)
                {
                    var displayScale = (float)Resources.DisplayMetrics.DensityDpi / (float)Android.Util.DisplayMetricsDensity.Default;
                    DrawLabel(e.NewElement.Width * displayScale, e.NewElement.Height * displayScale);
                }
                Control.IsNativeDrawEnabled = true;
            }
        }
Beispiel #13
0
        void UpdateText()
        {
            if (Element.FormattedText != null)
            {
                FormattedString formattedText = Element.FormattedText ?? Element.Text;

                Font f = Font.OfSize(Element.FontFamily, Element.FontSize).WithAttributes(Element.FontAttributes);
                _view.TextFormatted = _spannableString = formattedText.ToSpannableString(Element.RequireFontManager());

                _wasFormatted = true;
            }
            else
            {
                if (_wasFormatted)
                {
                    _view.SetTextColor(_labelTextColorDefault);
                    _lastUpdateColor = null;
                }

                switch (Element.TextType)
                {
                case TextType.Html:
                    if (Forms.IsNougatOrNewer)
                    {
                        Control.SetText(Html.FromHtml(Element.Text ?? string.Empty, FromHtmlOptions.ModeCompact), TextView.BufferType.Spannable);
                    }
                    else
#pragma warning disable CS0618 // Type or member is obsolete
                    {
                        Control.SetText(Html.FromHtml(Element.Text ?? string.Empty), TextView.BufferType.Spannable);
                    }
#pragma warning restore CS0618 // Type or member is obsolete
                    break;

                default:
                    _view.Text = Element.UpdateFormsText(Element.Text, Element.TextTransform);

                    break;
                }

                UpdateColor();
                UpdateFont();

                _wasFormatted = false;
            }

            _lastSizeRequest = null;
        }
Beispiel #14
0
        void UpdateText()
        {
            if (Element.FormattedText != null)
            {
                FormattedString formattedText = Element.FormattedText ?? Element.Text;
#pragma warning disable 618 // We will need to update this when .Font goes away
                _view.TextFormatted = _spannableString = formattedText.ToAttributed(Element.Font, Element.TextColor, _view);
#pragma warning restore 618
                _wasFormatted = true;
            }
            else
            {
                if (_wasFormatted)
                {
                    _view.SetTextColor(_labelTextColorDefault);
                    _lastUpdateColor = Color.Default;
                }

                switch (Element.TextType)
                {
                case TextType.Html:
                    if (Forms.IsNougatOrNewer)
                    {
                        Control.SetText(Html.FromHtml(Element.Text ?? string.Empty, FromHtmlOptions.ModeCompact), TextView.BufferType.Spannable);
                    }
                    else
#pragma warning disable CS0618 // Type or member is obsolete
                    {
                        Control.SetText(Html.FromHtml(Element.Text ?? string.Empty), TextView.BufferType.Spannable);
                    }
#pragma warning restore CS0618 // Type or member is obsolete
                    break;

                default:
                    _view.Text = Element.Text;

                    break;
                }

                UpdateColor();
                UpdateFont();

                _wasFormatted = false;
            }

            _lastSizeRequest = null;
        }
Beispiel #15
0
        SizeRequest IVisualElementRenderer.GetDesiredSize(int widthConstraint, int heightConstraint)
        {
            if (_disposed)
            {
                return(new SizeRequest());
            }

            if (_lastSizeRequest.HasValue)
            {
                // if we are measuring the same thing, no need to waste the time
                bool canRecycleLast = widthConstraint == _lastConstraintWidth && heightConstraint == _lastConstraintHeight;

                if (!canRecycleLast)
                {
                    // if the last time we measured the returned size was all around smaller than the passed constraint
                    // and the constraint is bigger than the last size request, we can assume the newly measured size request
                    // will not change either.
                    int lastConstraintWidthSize  = MeasureSpecFactory.GetSize(_lastConstraintWidth);
                    int lastConstraintHeightSize = MeasureSpecFactory.GetSize(_lastConstraintHeight);

                    int currentConstraintWidthSize  = MeasureSpecFactory.GetSize(widthConstraint);
                    int currentConstraintHeightSize = MeasureSpecFactory.GetSize(heightConstraint);

                    bool lastWasSmallerThanConstraints = _lastSizeRequest.Value.Request.Width < lastConstraintWidthSize && _lastSizeRequest.Value.Request.Height < lastConstraintHeightSize;

                    bool currentConstraintsBiggerThanLastRequest = currentConstraintWidthSize >= _lastSizeRequest.Value.Request.Width && currentConstraintHeightSize >= _lastSizeRequest.Value.Request.Height;

                    canRecycleLast = lastWasSmallerThanConstraints && currentConstraintsBiggerThanLastRequest;
                }

                if (canRecycleLast)
                {
                    return(_lastSizeRequest.Value);
                }
            }

            Measure(widthConstraint, heightConstraint);
            SizeRequest result = new SizeRequest(new Size(MeasuredWidth, MeasuredHeight), new Size());

            result.Minimum = new Size(Math.Min(Context.ToPixels(10), result.Request.Width), result.Request.Height);

            _lastConstraintWidth  = widthConstraint;
            _lastConstraintHeight = heightConstraint;
            _lastSizeRequest      = result;

            return(result);
        }
Beispiel #16
0
 SizeRequest DrawLabel(double width, double height)
 {
     if (width >= 0 &&
         height >= 0 &&
         !_currentDrawState.IsBlank &&
         Control is UILabel control &&
         Element != null)
     {
         _currentDrawState.AvailWidth  = width;
         _currentDrawState.AvailHeight = height;
         if (_currentDrawState == _lastDrawState && _lastDrawResult.HasValue)
         {
             return(_lastDrawResult.Value);
         }
         _lastDrawResult = InternalLayout(control, _currentDrawState);
         _lastDrawState  = new TextControlState(_currentDrawState);
         return(_lastDrawResult.Value);
     }
     return(new SizeRequest(Xamarin.Forms.Size.Zero));
 }
 private void UpdateText()
 {
     if (base.Element.FormattedText != null)
     {
         FormattedString formattedString = base.Element.FormattedText ?? base.Element.Text;
         this.view.TextFormatted = formattedString.ToAttributed(base.Element.Font, base.Element.TextColor, this.view);
         this.wasFormatted       = true;
     }
     else
     {
         if (this.wasFormatted)
         {
             this.view.SetTextColor(this.labelTextColorDefault);
             this.lastUpdateColor = Color.Default;
         }
         this.view.Text = base.Element.Text;
         this.UpdateColor();
         this.UpdateFont();
         this.wasFormatted = false;
     }
     this.lastSizeRequest = null;
 }
        void UpdateLineBreakMode()
        {
            switch (Element.LineBreakMode)
            {
            case LineBreakMode.NoWrap:
                _view.SetSingleLine(true);
                _view.Ellipsize = null;
                break;

            case LineBreakMode.WordWrap:
                _view.SetSingleLine(false);
                _view.Ellipsize = null;
                _view.SetMaxLines(100);
                break;

            case LineBreakMode.CharacterWrap:
                _view.SetSingleLine(false);
                _view.Ellipsize = null;
                _view.SetMaxLines(100);
                break;

            case LineBreakMode.HeadTruncation:
                _view.SetSingleLine(true);
                _view.Ellipsize = TextUtils.TruncateAt.Start;
                break;

            case LineBreakMode.TailTruncation:
                _view.SetSingleLine(true);
                _view.Ellipsize = TextUtils.TruncateAt.End;
                break;

            case LineBreakMode.MiddleTruncation:
                _view.SetSingleLine(true);
                _view.Ellipsize = TextUtils.TruncateAt.Middle;
                break;
            }
            _lastSizeRequest = null;
        }
Beispiel #19
0
 void UpdateLineBreakMode()
 {
     this.SetLineBreakMode(Element.LineBreakMode);
     _lastSizeRequest = null;
 }
Beispiel #20
0
 void UpdateLineHeight()
 {
     SetLineSpacing(0, (float)Element.LineHeight);
     _lastSizeRequest = null;
 }
 void UpdateLineBreakMode()
 {
     _view.SetLineBreakMode(Element);
     _lastSizeRequest = null;
 }
Beispiel #22
0
        SizeRequest IVisualElementRenderer.GetDesiredSize(int widthConstraint, int heightConstraint)
        {
            if (_disposed)
            {
                return(new SizeRequest());
            }

            if (_lastSizeRequest.HasValue)
            {
                // if we are measuring the same thing, no need to waste the time
                bool canRecycleLast = widthConstraint == _lastConstraintWidth && heightConstraint == _lastConstraintHeight;

                if (!canRecycleLast)
                {
                    // if the last time we measured the returned size was all around smaller than the passed constraint
                    // and the constraint is bigger than the last size request, we can assume the newly measured size request
                    // will not change either.
                    int lastConstraintWidthSize  = MeasureSpecFactory.GetSize(_lastConstraintWidth);
                    int lastConstraintHeightSize = MeasureSpecFactory.GetSize(_lastConstraintHeight);

                    int currentConstraintWidthSize  = MeasureSpecFactory.GetSize(widthConstraint);
                    int currentConstraintHeightSize = MeasureSpecFactory.GetSize(heightConstraint);

                    bool lastWasSmallerThanConstraints = _lastSizeRequest.Value.Request.Width < lastConstraintWidthSize && _lastSizeRequest.Value.Request.Height < lastConstraintHeightSize;

                    bool currentConstraintsBiggerThanLastRequest = currentConstraintWidthSize >= _lastSizeRequest.Value.Request.Width && currentConstraintHeightSize >= _lastSizeRequest.Value.Request.Height;

                    canRecycleLast = lastWasSmallerThanConstraints && currentConstraintsBiggerThanLastRequest;
                }

                if (canRecycleLast)
                {
                    return(_lastSizeRequest.Value);
                }
            }

            //We need to clear the Hint or else it will interfere with the sizing of the Label
            var  hint    = Control.Hint;
            bool setHint = Control.LayoutParameters != null;

            if (!string.IsNullOrEmpty(hint) && setHint)
            {
                Control.Hint = string.Empty;
            }

            var hc = MeasureSpec.GetSize(heightConstraint);

            Measure(widthConstraint, heightConstraint);
            var result = new SizeRequest(new Size(MeasuredWidth, MeasuredHeight), new Size());

            //Set Hint back after sizing
            if (setHint)
            {
                Control.Hint = hint;
            }

            result.Minimum = new Size(Math.Min(Context.ToPixels(10), result.Request.Width), result.Request.Height);

            // if the measure of the view has changed then trigger a request for layout
            // if the measure hasn't changed then force a layout of the label
            var measureIsChanged = !_lastSizeRequest.HasValue ||
                                   (_lastSizeRequest.Value.Request.Height != MeasuredHeight || _lastSizeRequest.Value.Request.Width != MeasuredWidth);

            if (measureIsChanged)
            {
                this.MaybeRequestLayout();
            }
            else
            {
                ForceLayout();
            }

            _lastConstraintWidth  = widthConstraint;
            _lastConstraintHeight = heightConstraint;
            _lastSizeRequest      = result;

            return(result);
        }
        /// <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(int widthConstraint, int heightConstraint)
        {
            if (_currentControlState.IsNullOrEmpty || Control == null)
            {
                return(new SizeRequest(Xamarin.Forms.Size.Zero));
            }

            _currentControlState.AvailWidth = MeasureSpec.GetSize(widthConstraint);
            if (MeasureSpec.GetMode(widthConstraint) == Android.Views.MeasureSpecMode.Unspecified)
            {
                _currentControlState.AvailWidth = int.MaxValue / 2;
            }
            _currentControlState.AvailHeight = MeasureSpec.GetSize(heightConstraint);
            if (MeasureSpec.GetMode(heightConstraint) == Android.Views.MeasureSpecMode.Unspecified)
            {
                _currentControlState.AvailHeight = int.MaxValue / 2;
            }

            if (_currentControlState.AvailWidth <= 0 || _currentControlState.AvailHeight <= 0)
            {
                return(new SizeRequest(Xamarin.Forms.Size.Zero));
            }

            if (_currentControlState == _lastControlState && _lastSizeRequest.HasValue)
            {
                return(_lastSizeRequest.Value);
            }



            ICharSequence text        = _currentControlState.JavaText;
            var           tmpFontSize = BoundTextSize(Element.FontSize);

            Control.TextSize = tmpFontSize;
            Control.SetSingleLine(false);
            Control.SetMaxLines(int.MaxValue / 2);
            Control.SetIncludeFontPadding(false);
            Control.Ellipsize = null;

            int tmpHt = -1;
            int tmpWd = -1;

            var fontMetrics    = Control.Paint.GetFontMetrics();
            var fontLineHeight = fontMetrics.Descent - fontMetrics.Ascent;
            var fontLeading    = System.Math.Abs(fontMetrics.Bottom - fontMetrics.Descent);

            if (DebugCondition)
            {
                System.Diagnostics.Debug.WriteLine("");
            }

            if (_currentControlState.Lines == 0)
            {
                if (_currentControlState.AvailHeight < int.MaxValue / 3)
                {
                    tmpFontSize = F9PTextView.ZeroLinesFit(_currentControlState.JavaText, new TextPaint(Control.Paint), ModelMinFontSize, tmpFontSize, _currentControlState.AvailWidth, _currentControlState.AvailHeight);
                }
            }
            else
            {
                if (_currentControlState.AutoFit == AutoFit.Lines)
                {
                    if (_currentControlState.AvailHeight > int.MaxValue / 3)
                    {
                        tmpHt = (int)System.Math.Round(_currentControlState.Lines * fontLineHeight + (_currentControlState.Lines - 1) * fontLeading);
                    }
                    else
                    {
                        var fontPointSize   = tmpFontSize;
                        var lineHeightRatio = fontLineHeight / fontPointSize;
                        var leadingRatio    = fontLeading / fontPointSize;
                        tmpFontSize = ((_currentControlState.AvailHeight / (_currentControlState.Lines + leadingRatio * (_currentControlState.Lines - 1))) / lineHeightRatio - 0.1f);
                    }
                }
                else if (_currentControlState.AutoFit == AutoFit.Width)
                {
                    tmpFontSize = F9PTextView.WidthFit(_currentControlState.JavaText, new TextPaint(Control.Paint), _currentControlState.Lines, ModelMinFontSize, tmpFontSize, _currentControlState.AvailWidth, _currentControlState.AvailHeight);
                }
            }

            /*
             * if (_currentControlState.Lines == 0) // && _currentControlState.AutoFit != AutoFit.None)
             *  tmpFontSize = F9PTextView.ZeroLinesFit(_currentControlState.JavaText, new TextPaint(Control.Paint), ModelMinFontSize, tmpFontSize, _currentControlState.AvailWidth, _currentControlState.AvailHeight);
             * else if (_currentControlState.AutoFit == AutoFit.Lines)
             * {
             *
             *  if (_currentControlState.AvailHeight > int.MaxValue / 3)
             *      tmpHt = (int)System.Math.Round(_currentControlState.Lines * fontLineHeight + (_currentControlState.Lines - 1) * fontLeading);
             *  else
             *  {
             *      var fontPointSize = tmpFontSize;
             *      var lineHeightRatio = fontLineHeight / fontPointSize;
             *      var leadingRatio = fontLeading / fontPointSize;
             *      tmpFontSize = ((_currentControlState.AvailHeight / (_currentControlState.Lines + leadingRatio * (_currentControlState.Lines - 1))) / lineHeightRatio - 0.1f);
             *  }
             * }
             * else if (_currentControlState.AutoFit == AutoFit.Width)
             *  tmpFontSize = F9PTextView.WidthFit(_currentControlState.JavaText, new TextPaint(Control.Paint), _currentControlState.Lines, ModelMinFontSize, tmpFontSize, _currentControlState.AvailWidth, _currentControlState.AvailHeight);
             */

            tmpFontSize = BoundTextSize(tmpFontSize);

            // this is the optimal font size.  Let it be known!
            if (tmpFontSize != Element.FittedFontSize)
            {
                //Device.StartTimer(TimeSpan.FromMilliseconds(50), () =>
                //{
                if (Element != null && Control != null)  // multipicker test was getting here with Element and Control both null
                {
                    if (tmpFontSize == Element.FontSize || (Element.FontSize == -1 && tmpFontSize == F9PTextView.DefaultTextSize))
                    {
                        Element.FittedFontSize = -1;
                    }
                    else
                    {
                        Element.FittedFontSize = tmpFontSize;
                    }
                }
                //    return false;
                //});
            }


            var syncFontSize = (float)((ILabel)Element).SynchronizedFontSize;

            if (syncFontSize >= 0 && tmpFontSize != syncFontSize)
            {
                tmpFontSize = syncFontSize;
            }


            Control.TextSize = tmpFontSize;

            var layout = new StaticLayout(_currentControlState.JavaText, new TextPaint(Control.Paint), _currentControlState.AvailWidth, Android.Text.Layout.Alignment.AlignNormal, 1.0f, 0.0f, true);

            int lines = _currentControlState.Lines;

            if (lines == 0 && _currentControlState.AutoFit == AutoFit.None)
            {
                for (int i = 0; i < layout.LineCount; i++)
                {
                    if (layout.GetLineBottom(i) <= _currentControlState.AvailHeight - layout.TopPadding - layout.BottomPadding)
                    {
                        lines++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (layout.Height > _currentControlState.AvailHeight || (lines > 0 && layout.LineCount > lines))
            {
                if (_currentControlState.Lines == 1)
                {
                    Control.SetSingleLine(true);
                    Control.SetMaxLines(1);
                    Control.Ellipsize = _currentControlState.LineBreakMode.ToEllipsize();
                }
                else
                {
                    layout = F9PTextView.Truncate(_currentControlState.Text, Element.F9PFormattedString, new TextPaint(Control.Paint), _currentControlState.AvailWidth, _currentControlState.AvailHeight, Element.AutoFit, Element.LineBreakMode, ref lines, ref text);
                }
            }
            lines = lines > 0 ? System.Math.Min(lines, layout.LineCount) : layout.LineCount;
            for (int i = 0; i < lines; i++)
            {
                tmpHt = layout.GetLineBottom(i);
                var width = layout.GetLineWidth(i);
                //System.Diagnostics.Debug.WriteLine("\t\tright=["+right+"]");
                if (width > tmpWd)
                {
                    tmpWd = (int)System.Math.Ceiling(width);
                }
            }
            if (_currentControlState.AutoFit == AutoFit.None && _currentControlState.Lines > 0)
            {
                Control.SetMaxLines(_currentControlState.Lines);
            }

            //System.Diagnostics.Debug.WriteLine("\tLabelRenderer.GetDesiredSize\ttmp.size=[" + tmpWd + ", " + tmpHt + "]");
            if (Element.IsDynamicallySized && _currentControlState.Lines > 0 && _currentControlState.AutoFit == AutoFit.Lines)
            {
                fontMetrics    = Control.Paint.GetFontMetrics();
                fontLineHeight = fontMetrics.Descent - fontMetrics.Ascent;
                fontLeading    = System.Math.Abs(fontMetrics.Bottom - fontMetrics.Descent);
                tmpHt          = (int)(fontLineHeight * _currentControlState.Lines + fontLeading * (_currentControlState.Lines - 1));
            }

            Control.Gravity = Element.HorizontalTextAlignment.ToHorizontalGravityFlags() | Element.VerticalTextAlignment.ToVerticalGravityFlags();

            if (Element.Text != null)
            {
                Control.Text = text.ToString();
            }
            else
            {
                Control.TextFormatted = text;
            }

            _lastSizeRequest = new SizeRequest(new Xamarin.Forms.Size(tmpWd, tmpHt), new Xamarin.Forms.Size(10, tmpHt));
            if (showDebugMsg)
            {
                Control.SetWidth((int)_lastSizeRequest.Value.Request.Width);
                Control.SetHeight((int)_lastSizeRequest.Value.Request.Height);

                System.Diagnostics.Debug.WriteLine("\t[" + elementText + "] LabelRenderer.GetDesiredSize(" + (_currentControlState.AvailWidth > int.MaxValue / 3 ? "infinity" : _currentControlState.AvailWidth.ToString()) + "," + (_currentControlState.AvailHeight > int.MaxValue / 3 ? "infinity" : _currentControlState.AvailHeight.ToString()) + ") exit (" + _lastSizeRequest.Value + ")");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Visibility=[" + Control.Visibility + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.TextFormatted=[" + Control.TextFormatted + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.TextSize=[" + Control.TextSize + "]");
                //System.Diagnostics.Debug.WriteLine("\t\tControl.ClipBounds=["+Control.ClipBounds.Width()+","+Control.ClipBounds.Height()+"]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Width[" + Control.Width + "]  .Height=[" + Control.Height + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.GetX[" + Control.GetX() + "]  .GetY[" + Control.GetY() + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Alpha[" + Control.Alpha + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Background[" + Control.Background + "]");
                //System.Diagnostics.Debug.WriteLine("\t\tControl.Elevation["+Control.Elevation+"]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Enabled[" + Control.Enabled + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Error[" + Control.Error + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.IsOpaque[" + Control.IsOpaque + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.IsShown[" + Control.IsShown + "]");
                //Control.BringToFront();
                System.Diagnostics.Debug.WriteLine("\t\t");
            }

            if (Element.LineBreakMode == LineBreakMode.NoWrap)
            {
                Control.SetSingleLine(true);
            }

            _lastControlState = new ControlState(_currentControlState);
            return(_lastSizeRequest.Value);
        }
Beispiel #24
0
 void UpdateMaxLines()
 {
     this.SetMaxLines(Element);
     _lastSizeRequest = null;
 }
Beispiel #25
0
        /// <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(int widthConstraint, int heightConstraint)
        {
            if (_currentDrawState == null ||
                _currentDrawState.IsEmpty ||
                Control == null ||
                Element == null ||
                _disposed)
            {
                return(new SizeRequest(Size.Zero));
            }

            var width = MeasureSpec.GetSize(widthConstraint);

            if (MeasureSpec.GetMode(widthConstraint) == Android.Views.MeasureSpecMode.Unspecified)
            {
                width = int.MaxValue / 2;
            }
            var height = MeasureSpec.GetSize(heightConstraint);

            if (MeasureSpec.GetMode(heightConstraint) == Android.Views.MeasureSpecMode.Unspecified)
            {
                height = int.MaxValue / 2;
            }

            if (width <= 0 || height <= 0)
            {
                return(new SizeRequest(Size.Zero));
            }

            _currentMeasureState = new TextControlState(_currentDrawState)
            {
                AvailWidth  = width,
                AvailHeight = height
            };

            if (_lastMeasureResult != null && _lastMeasureResult.HasValue &&
                _lastMeasureState == _currentMeasureState &&
                _lastMeasureResult.Value.Request.Width > 0 &&
                _lastMeasureResult.Value.Request.Height > 0 &&
                _lastMeasureState.RenderedFontSize >= _currentMeasureState.TextSize &&
                _lastMeasureResult.Value.Request.Width <= _currentMeasureState.AvailWidth &&
                _lastMeasureResult.Value.Request.Height <= _currentMeasureState.AvailHeight
                )
            {
                return(_lastMeasureResult.Value);
            }

            if (_lastDrawResult != null && _lastDrawResult.HasValue &&
                _lastDrawState == _currentMeasureState &&
                _lastDrawResult.Value.Request.Width > 0 &&
                _lastDrawResult.Value.Request.Height > 0 &&
                _lastDrawState.RenderedFontSize >= _currentDrawState.TextSize &&
                _lastDrawResult.Value.Request.Width <= _currentMeasureState.AvailWidth &&
                _lastDrawResult.Value.Request.Height <= _currentMeasureState.AvailWidth)
            {
                return(_lastDrawResult.Value);
            }

            _measureControl    = _measureControl ?? new F9PTextView(Settings.Context);
            _lastMeasureResult = InternalLayout(_measureControl, _currentMeasureState);
            _lastMeasureState  = new TextControlState(_currentMeasureState);

            return(_lastMeasureResult.Value);
        }