Beispiel #1
0
        void ClearPreviousLayoutInformation()
        {
            if (framesetter != null)
            {
                framesetter.Dispose();
                framesetter = null;
            }

            if (frame != null)
            {
                frame.Dispose();
                frame = null;
            }
        }
Beispiel #2
0
        public SizeF GetStringSize(string aString, string aFontName, float aFontSize, HorizontalAlignment aHorizontalAlignment, VerticalAlignment aVerticalAlignment)
        {
            var   fontSize = aFontSize;
            float factor   = 1;

            while (fontSize > 10)
            {
                fontSize /= 10;
                factor   *= 10;
            }

            var vPath = new CGPath();

            vPath.AddRect(new CGRect(0, 0, 512, 512));
            vPath.CloseSubpath();

            var vAttributedString = new NSMutableAttributedString(aString);

            var vAttributes = new CTStringAttributes();

            // Load the font
            var vFont = NativeFontService.Instance.LoadFont(aFontName ?? _systemFontName, (float)fontSize);

            vAttributes.Font = vFont;

            // Set the horizontal alignment
            var vParagraphSettings = new CTParagraphStyleSettings();

            switch (aHorizontalAlignment)
            {
            case HorizontalAlignment.Left:
                vParagraphSettings.Alignment = CTTextAlignment.Left;
                break;

            case HorizontalAlignment.Center:
                vParagraphSettings.Alignment = CTTextAlignment.Center;
                break;

            case HorizontalAlignment.Right:
                vParagraphSettings.Alignment = CTTextAlignment.Right;
                break;

            case HorizontalAlignment.Justified:
                vParagraphSettings.Alignment = CTTextAlignment.Justified;
                break;
            }

            var vParagraphStyle = new CTParagraphStyle(vParagraphSettings);

            vAttributes.ParagraphStyle = vParagraphStyle;

            // Set the attributes for the complete length of the string
            vAttributedString.SetAttributes(vAttributes, new NSRange(0, aString.Length));

            // Create the framesetter with the attributed string.
            var vFrameSetter = new CTFramesetter(vAttributedString);

            var textBounds = GetTextSize(vFrameSetter, vPath);

            //Logger.Debug("{0} {1}",vSize,aString);

            vFrameSetter.Dispose();
            vAttributedString.Dispose();
            vParagraphStyle.Dispose();
            //vFont.Dispose();
            vPath.Dispose();

            textBounds.Width  *= factor;
            textBounds.Height *= factor;

            //vSize.Width = Math.Ceiling(vSize.Width);
            //vSize.Height = Math.Ceiling(vSize.Height);

            return(textBounds.Size);
        }
        public SizeF GetStringSize(
            string value,
            string fontName,
            float fontSize,
            HorizontalAlignment horizontalAlignment,
            VerticalAlignment verticalAlignment)
        {
            float factor = 1;

            while (fontSize > 10)
            {
                fontSize /= 10;
                factor   *= 10;
            }

            var path = new CGPath();

            path.AddRect(new Drawing.RectangleF(0, 0, 512, 512));
            path.CloseSubpath();

            var attributedString = new NSMutableAttributedString(value);

            var attributes = new CTStringAttributes();

            // Load the font
            var font = NativeFontService.Instance.LoadFont(fontName ?? SystemFontName, fontSize);

            attributes.Font = font;

            // Set the horizontal alignment
            var paragraphSettings = new CTParagraphStyleSettings();

            switch (horizontalAlignment)
            {
            case HorizontalAlignment.Left:
                paragraphSettings.Alignment = CTTextAlignment.Left;
                break;

            case HorizontalAlignment.Center:
                paragraphSettings.Alignment = CTTextAlignment.Center;
                break;

            case HorizontalAlignment.Right:
                paragraphSettings.Alignment = CTTextAlignment.Right;
                break;

            case HorizontalAlignment.Justified:
                paragraphSettings.Alignment = CTTextAlignment.Justified;
                break;
            }

            var paragraphStyle = new CTParagraphStyle(paragraphSettings);

            attributes.ParagraphStyle = paragraphStyle;

            // Set the attributes for the complete length of the string
            attributedString.SetAttributes(attributes, new NSRange(0, value.Length));

            // Create the frame setter with the attributed string.
            var frameSetter = new CTFramesetter(attributedString);

            var textBounds = GetTextSize(frameSetter, path);

            //Logger.Debug("{0} {1}",vSize,aString);

            frameSetter.Dispose();
            attributedString.Dispose();
            paragraphStyle.Dispose();
            //font.Dispose();
            path.Dispose();

            textBounds.Width  *= factor;
            textBounds.Height *= factor;

            //size.Width = Math.Ceiling(vSize.Width);
            //size.Height = Math.Ceiling(vSize.Height);

            return(textBounds.Size);
        }
        public override void Draw(CGRect rect)
        {
            var framesetter = new CTFramesetter(AttributedText);

            var path = new CGPath();

            path.AddRect(new CGRect(0, 0, Bounds.Size.Width, Bounds.Size.Height));

            var totalFrame = framesetter.GetFrame(new NSRange(0, 0), path, null);

            var context = UIGraphics.GetCurrentContext();

            context.TextMatrix = CGAffineTransform.MakeIdentity();
            context.TranslateCTM(0, Bounds.Size.Height);
            context.ScaleCTM(1.0f, -1.0f);

            var lines     = totalFrame.GetLines();
            var lineCount = lines.Length;

            var origins = new CGPoint[lineCount];

            totalFrame.GetLineOrigins(new NSRange(0, 0), origins);

            for (var index = 0; index < lineCount; index++)
            {
                var line = lines[index];

                foreach (var run in line.GetGlyphRuns())
                {
                    var attributes = run.GetAttributes();

                    var borderColor     = attributes.Dictionary[RoundedBorders];
                    var backgroundColor = attributes.Dictionary[RoundedBackground];
                    if (backgroundColor == null && borderColor == null)
                    {
                        continue;
                    }

                    var x = line.GetOffsetForStringIndex(run.StringRange.Location, out var _) + whiteSpaceOffset;
                    var y = origins[index].Y - (tokenHeight * 0.7);

                    var tokenPath = UIBezierPath.FromRoundedRect(new CGRect(
                                                                     x: x, y: y,
                                                                     width: run.GetTypographicBounds(new NSRange(0, 0), out var _, out var _, out var _) - whiteSpaceOffset,
                                                                     height: tokenHeight
                                                                     ), tokenCornerRadius);

                    context.AddPath(tokenPath.CGPath);

                    if (backgroundColor != null)
                    {
                        var color = (UIColor)backgroundColor;
                        context.SetFillColor(color.CGColor);
                        context.DrawPath(CGPathDrawingMode.Fill);

                        var circle = UIBezierPath.FromRoundedRect(new CGRect(
                                                                      x: x + circlePadding,
                                                                      y: y + circleYOffset,
                                                                      width: circleDiameter,
                                                                      height: circleDiameter
                                                                      ), circleRadius);

                        context.AddPath(circle.CGPath);
                        context.SetFillColor(color.ColorWithAlpha(1.0f).CGColor);
                        context.DrawPath(CGPathDrawingMode.Fill);
                    }
                    else
                    {
                        var color = ((UIColor)borderColor).CGColor;
                        context.SetStrokeColor(color);
                        context.DrawPath(CGPathDrawingMode.Stroke);
                    }
                }
            }

            path.Dispose();
            totalFrame.Dispose();
            framesetter.Dispose();
        }