Esempio n. 1
0
 public CGSize GetSize()
 {
     using (var TextLayout = new NSLayoutManager())
     {
         TextLayout.AddTextContainer(TextContainer);
         TextStorage.AddLayoutManager(TextLayout);
         TextLayout.GlyphRangeForBoundingRect(new CGRect(CGPoint.Empty, TextContainer.Size), TextContainer);
         var s = TextLayout.GetUsedRectForTextContainer(TextContainer);
         TextStorage.RemoveLayoutManager(TextLayout);
         TextLayout.RemoveTextContainer(0);
         return(s.Size);
     }
 }
Esempio n. 2
0
 public CGPoint GetCoordinateFromIndex(int index)
 {
     using (var TextLayout = new NSLayoutManager())
     {
         TextLayout.AddTextContainer(TextContainer);
         TextStorage.AddLayoutManager(TextLayout);
         TextLayout.GlyphRangeForBoundingRect(new CGRect(CGPoint.Empty, TextContainer.Size), TextContainer);
         var glyphIndex = TextLayout.GlyphIndexForCharacterAtIndex(index);
         var p          = TextLayout.LocationForGlyphAtIndex((nint)glyphIndex);
         TextStorage.RemoveLayoutManager(TextLayout);
         TextLayout.RemoveTextContainer(0);
         return(p);
     }
 }
Esempio n. 3
0
 public nuint GetIndexFromCoordinates(double x, double y)
 {
     using (var TextLayout = new NSLayoutManager())
     {
         TextLayout.AddTextContainer(TextContainer);
         TextStorage.AddLayoutManager(TextLayout);
         TextLayout.GlyphRangeForBoundingRect(new CGRect(CGPoint.Empty, TextContainer.Size), TextContainer);
         nfloat fraction = 0;
         var    index    = TextLayout.CharacterIndexForPoint(new CGPoint(x, y), TextContainer, ref fraction);
         TextStorage.RemoveLayoutManager(TextLayout);
         TextLayout.RemoveTextContainer(0);
         return(index);
     }
 }
Esempio n. 4
0
            public void Draw(CGContext ctx, CGColor foregroundColor, double x, double y)
            {
                bool tempForegroundSet = false;

                // if no color attribute is set for the whole string,
                // NSLayoutManager will use the default control foreground color.
                // To override the default color we need to apply the current CGContext stroke color
                // before all other attributes are set, otherwise it will remove all other foreground colors.
                if (foregroundColor != null && !Attributes.Any(a => a is ColorTextAttribute && a.StartIndex == 0 && a.Count == Text.Length))
                {
                    // FIXME: we need to find a better way to accomplish this without the need to reset all attributes.
                    ResetAttributes(NSColor.FromCGColor(foregroundColor));
                    tempForegroundSet = true;
                }

                ctx.SaveState();
                NSGraphicsContext.GlobalSaveGraphicsState();
                var nsContext = NSGraphicsContext.FromCGContext(ctx, true);

                NSGraphicsContext.CurrentContext = nsContext;

                using (var TextLayout = new NSLayoutManager())
                {
                    TextLayout.AddTextContainer(TextContainer);
                    TextStorage.AddLayoutManager(TextLayout);

                    TextLayout.DrawBackgroundForGlyphRange(new NSRange(0, Text.Length), new CGPoint(x, y));
                    TextLayout.DrawGlyphsForGlyphRange(new NSRange(0, Text.Length), new CGPoint(x, y));
                    TextStorage.RemoveLayoutManager(TextLayout);
                    TextLayout.RemoveTextContainer(0);
                }

                // reset foreground color change
                if (tempForegroundSet)
                {
                    ResetAttributes();
                }

                NSGraphicsContext.GlobalRestoreGraphicsState();
                ctx.RestoreState();
            }