Example #1
0
        public void Update()
        {
            if (View?.LayoutParameters == null && _hasLayoutOccurred)
            {
                return;
            }

            if (View != null && !_elementAlreadyChanged)
            {
                _defaultTransformationMethod = View.TransformationMethod;
                _elementAlreadyChanged       = true;
            }

            if (!UpdateTextAndImage())
            {
                UpdateImage();
            }

            UpdatePadding();
        }
Example #2
0
        // Test if the suggested size fits the control boundaries
        public int OnTestSize(int suggestedSize, RectF availableSpace)
        {
            RectF textRect = new RectF();

            paint.TextSize = suggestedSize;
            ITransformationMethod transformationMethod = targetControl.TransformationMethod;
            string text = null;

            if (transformationMethod != null)
            {
                text = transformationMethod.GetTransformation(targetControl.Text, targetControl);
            }

            // If text is null, use the value from Text object
            if (text == null)
            {
                text = targetControl.Text;
            }

            bool singleLine = targetControl.MaxLines == 1;

            if (singleLine)
            {
                textRect.Bottom = paint.FontSpacing;
                textRect.Right  = paint.MeasureText(text);
            }
            else
            {
                StaticLayout layout = new StaticLayout(text, paint, widthLimit, Alignment.AlignNormal, spacingMult, spacingAdd, true);

                if (targetControl.MaxLines != NO_LINE_LIMIT && layout.LineCount > targetControl.MaxLines)
                {
                    return(1);
                }

                textRect.Bottom = layout.Height;
                int maxWidth  = -1;
                int lineCount = layout.LineCount;
                for (int i = 0; i < lineCount; i++)
                {
                    int end = layout.GetLineEnd(i);
                    if (i < lineCount - 1 && end > 0 && !IsValidWordWrap(text[end - 1], text[end]))
                    {
                        return(1);
                    }
                    if (maxWidth < layout.GetLineRight(i) - layout.GetLineLeft(i))
                    {
                        maxWidth = (int)layout.GetLineRight(i) - (int)layout.GetLineLeft(i);
                    }
                }
                textRect.Right = maxWidth;
            }
            textRect.OffsetTo(0, 0);

            if (availableSpace.Contains(textRect))
            {
                return(-1);
            }

            return(1);
        }