コード例 #1
0
        public static RectangleF GetTextSize(CTFrame frame)
        {
            var   minY  = float.MaxValue;
            var   maxY  = float.MinValue;
            float width = 0;

            var lines   = frame.GetLines();
            var origins = new CGPoint[lines.Length];

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

            for (var i = 0; i < lines.Length; i++)
            {
                var line      = lines[i];
                var lineWidth = (float)line.GetTypographicBounds(out var ascent, out var descent, out var leading);

                if (lineWidth > width)
                {
                    width = lineWidth;
                }

                var origin = origins[i];

                minY = (float)Math.Min(minY, origin.Y - ascent);
                maxY = (float)Math.Max(maxY, origin.Y + descent);

                lines[i].Dispose();
            }

            return(new RectangleF(0f, minY, width, Math.Max(0, maxY - minY)));
        }
コード例 #2
0
        // Helper method for drawing the current selection range (as a simple filled rect)
        void DrawRangeAsSelection(NSRange selectionRange)
        {
            // If not in editing mode, we do not draw selection rects
            if (!IsEditing)
            {
                return;
            }

            // If selection range empty, do not draw
            if (selectionRange.Length == 0 || selectionRange.Location == NSRange.NotFound)
            {
                return;
            }

            // set the fill color to the selection color
            SelectionColor.SetFill();

            // Iterate over the lines in our CTFrame, looking for lines that intersect
            // with the given selection range, and draw a selection rect for each intersection
            var lines = frame.GetLines();

            for (int i = 0; i < lines.Length; i++)
            {
                CTLine  line         = lines [i];
                NSRange lineRange    = line.StringRange;
                NSRange range        = new NSRange(lineRange.Location, lineRange.Length);
                NSRange intersection = RangeIntersection(range, selectionRange);
                if (intersection.Location != NSRange.NotFound && intersection.Length > 0)
                {
                    // The text range for this line intersects our selection range
                    nfloat xStart = line.GetOffsetForStringIndex(intersection.Location);
                    nfloat xEnd   = line.GetOffsetForStringIndex(intersection.Location + intersection.Length);
                    var    origin = new CGPoint [lines.Length];
                    // Get coordinate and bounds information for the intersection text range
                    frame.GetLineOrigins(new NSRange(i, 0), origin);
                    nfloat ascent, descent, leading;
                    line.GetTypographicBounds(out ascent, out descent, out leading);
                    // Create a rect for the intersection and draw it with selection color
                    CGRect selectionRect = new CGRect(xStart, origin [0].Y - descent, xEnd - xStart, ascent + descent);
                    UIGraphics.RectFill(selectionRect);
                }
            }
        }