Ejemplo n.º 1
0
        public SizeF GetStringSize(string aString, string aFontName, float aFontSize, HorizontalAlignment aHorizontalAlignment, VerticalAlignment aVerticalAlignment)
        {
            if (aString == null)
            {
                return(new SizeF());
            }

            var vTextPaint = new TextPaint {
                TextSize = aFontSize
            };

            vTextPaint.SetTypeface(NativeFontService.Instance.GetTypeface(aFontName));

            Layout.Alignment vAlignment;
            switch (aHorizontalAlignment)
            {
            case HorizontalAlignment.Center:
                vAlignment = Layout.Alignment.AlignCenter;
                break;

            case HorizontalAlignment.Right:
                vAlignment = Layout.Alignment.AlignOpposite;
                break;

            default:
                vAlignment = Layout.Alignment.AlignNormal;
                break;
            }

            StaticLayout vLayout = TextLayoutUtils.CreateLayout(aString, vTextPaint, null, vAlignment);
            SizeF        vSize   = vLayout.GetTextSizeAsSizeF(false);

            vLayout.Dispose();
            return(vSize);
        }
Ejemplo n.º 2
0
        public SizeF GetStringSize(string value, string fontName, float fontSize)
        {
            if (value == null)
            {
                return(new SizeF());
            }

            var textPaint = new TextPaint {
                TextSize = fontSize
            };

            textPaint.SetTypeface(NativeFontService.Instance.GetTypeface(fontName));

            var staticLayout = TextLayoutUtils.CreateLayout(value, textPaint, null, Layout.Alignment.AlignNormal);
            var size         = staticLayout.GetTextSizeAsSizeF(false);

            staticLayout.Dispose();
            return(size);
        }