Exemplo n.º 1
0
        public override Size MeasureText(Font font, string text)
        {
            CTFont sysFont = font.RendererData as CTFont;

            if (sysFont == null || Math.Abs(font.RealSize - font.Size * Scale) > 2)
            {
                FreeFont(font);
                LoadFont(font);
                sysFont = font.RendererData as CTFont;
            }

            var key = new Tuple <String, Font>(text, font);

            if (m_StringCache.ContainsKey(key))
            {
                var tex = m_StringCache[key].Texture;
                return(new Size(tex.Width, tex.Height));
            }

            text = text.Replace("\t", "    ");

            var attributedString = new NSAttributedString(text, new CTStringAttributes()
            {
                Font = sysFont, UnderlineStyle = font.Underline ? CTUnderlineStyle.Single : CTUnderlineStyle.None
            });
            CTLine ctLine = new CTLine(attributedString);
            CGRect cgRect = ctLine.GetBounds(CTLineBoundsOptions.IncludeLanguageExtents | CTLineBoundsOptions.UseHangingPunctuation);

            ctLine.Dispose();
            attributedString.Dispose();

            return(new Size(Util.Ceil((float)cgRect.Width), Util.Ceil((float)cgRect.Height)));
        }
Exemplo n.º 2
0
        public override void RenderText(Font font, Point position, string text)
        {
            Flush();

            CTFont sysFont = font.RendererData as CTFont;

            if (sysFont == null || Math.Abs(font.RealSize - font.Size * Scale) > 2)
            {
                FreeFont(font);
                LoadFont(font);
                sysFont = font.RendererData as CTFont;
            }

            var key = new Tuple <String, Font>(text, font);

            if (!m_StringCache.ContainsKey(key))
            {
                // not cached - create text renderer

                text = text.Replace("\t", "    ");

                var attributedString = new NSAttributedString(text, new CTStringAttributes()
                {
                    ForegroundColorFromContext = true, Font = sysFont, UnderlineStyle = font.Underline ? CTUnderlineStyle.Single : CTUnderlineStyle.None
                });
                CTLine ctLine = new CTLine(attributedString);
                CGRect cgRect = ctLine.GetBounds(CTLineBoundsOptions.IncludeLanguageExtents | CTLineBoundsOptions.UseHangingPunctuation);

                TextRenderer tr = new TextRenderer(Util.Ceil((float)cgRect.Width), Util.Ceil((float)cgRect.Height), this);
                tr.DrawString(ctLine, new Point(0, (int)sysFont.DescentMetric));                 // renders string on the texture

                ctLine.Dispose();
                attributedString.Dispose();

                DrawTexturedRect(tr.Texture, new Rectangle(position.X, position.Y, tr.Texture.Width, tr.Texture.Height));

                m_StringCache[key] = tr;
            }
            else
            {
                TextRenderer tr = m_StringCache[key];
                DrawTexturedRect(tr.Texture, new Rectangle(position.X, position.Y, tr.Texture.Width, tr.Texture.Height));
            }
        }
Exemplo n.º 3
0
        public void DrawText(string text, float x, float y, float width, float height, Xamarin.Forms.TextAlignment hAlignment, Xamarin.Forms.TextAlignment vAlignment)
        {
            if (font == null || font.NativeFont == null)
            {
                return;
            }

            var attributedString = new NSAttributedString(text, new CTStringAttributes()
            {
                Font = font.NativeFont, ForegroundColorFromContext = true
            });
            CTLine ctLine = new CTLine(attributedString);

            CGSize size = new CGSize((float)ctLine.GetTypographicBounds(), font.NativeFont.CapHeightMetric + font.NativeFont.DescentMetric);

            if ((hAlignment & Xamarin.Forms.TextAlignment.Center) != 0)
            {
                x += (width - (float)size.Width) / 2;
            }
            else if ((hAlignment & Xamarin.Forms.TextAlignment.End) != 0)
            {
                x += width - (float)size.Width;
            }

            if ((vAlignment & Xamarin.Forms.TextAlignment.Center) != 0)
            {
                y += (height - (float)size.Height) / 2;
            }
            else if ((vAlignment & Xamarin.Forms.TextAlignment.End) != 0)
            {
                y += height - (float)size.Height;
            }

            context.SaveState();
            context.ScaleCTM(1f, -1f);

            context.TextPosition = new CGPoint(x, -y - font.NativeFont.CapHeightMetric);
            ctLine.Draw(context);

            context.RestoreState();

            ctLine.Dispose();
            attributedString.Dispose();
        }
Exemplo n.º 4
0
        public Xamarin.Forms.Size MeasureText(string text)
        {
            if (font == null || font.NativeFont == null)
            {
                return(Xamarin.Forms.Size.Zero);
            }

            var attributedString = new NSAttributedString(text, new CTStringAttributes()
            {
                Font = font.NativeFont, ForegroundColorFromContext = true
            });
            CTLine ctLine = new CTLine(attributedString);

            Xamarin.Forms.Size size = new Xamarin.Forms.Size((float)ctLine.GetTypographicBounds(), font.NativeFont.CapHeightMetric + font.NativeFont.DescentMetric);

            ctLine.Dispose();
            attributedString.Dispose();

            return(size);
        }
Exemplo n.º 5
0
        public void DrawText(string text, float x, float y)
        {
            if (font == null || font.NativeFont == null)
            {
                return;
            }

            var attributedString = new NSAttributedString(text, new CTStringAttributes()
            {
                Font = font.NativeFont, ForegroundColorFromContext = true
            });
            CTLine ctLine = new CTLine(attributedString);

            context.SaveState();
            context.ScaleCTM(1f, -1f);

            context.TextPosition = new CGPoint(x, -y - font.NativeFont.CapHeightMetric);
            ctLine.Draw(context);

            context.RestoreState();

            ctLine.Dispose();
            attributedString.Dispose();
        }