void CalculateSize () { NSString str = new NSString (badgeNumber.ToString()); NSDictionary attibutedStringAttributed = NSDictionary.FromObjectAndKey(Font, NSAttributedString.FontAttributeName); numberSize = str.StringSize (attibutedStringAttributed); Frame = new RectangleF (Frame.Location, new SizeF (Math.Max (numberSize.Width, height), height)); }
public static SizeF MeasureText(CGContext context, string text, string fontName, float fontSize) { NSString str = new NSString(text); var dict = new NSMutableDictionary(); dict.Add(NSAttributedString.FontAttributeName, NSFont.FromFontName(fontName, fontSize)); var size = str.StringSize(dict); return size; }
// Create the set of display lists for the bitmaps bool MakeGLDisplayListFirst (char first, int count, int baseDL) { int curListIndex; NSColor blackColor; NSMutableDictionary attribDict; int dListNum; NSString currentChar; char currentUnichar; SizeF charSize; RectangleF charRect; NSImage theImage; bool retval; // Make sure the list isn't already under construction GL.GetInteger (GetPName.ListIndex, out curListIndex); if (curListIndex != 0) { Console.WriteLine ("Display list already under construction"); return false; } // Save pixel unpacking state GL.PushClientAttrib (ClientAttribMask.ClientPixelStoreBit); GL.PixelStore (PixelStoreParameter.UnpackSwapBytes, 0); GL.PixelStore (PixelStoreParameter.UnpackLsbFirst, 0); GL.PixelStore (PixelStoreParameter.UnpackSkipPixels, 0); GL.PixelStore (PixelStoreParameter.UnpackSkipRows, 0); GL.PixelStore (PixelStoreParameter.UnpackRowLength, 0); GL.PixelStore (PixelStoreParameter.UnpackAlignment, 0); blackColor = NSColor.Black; attribDict = new NSMutableDictionary (); attribDict.SetValueForKey (font, NSAttributedString.FontAttributeName); attribDict.SetValueForKey (NSColor.White, NSAttributedString.ForegroundColorAttributeName); attribDict.SetValueForKey (blackColor, NSAttributedString.BackgroundColorAttributeName); charRect.Location.X = charRect.Location.Y = 0; theImage = new NSImage (new SizeF (0,0)); retval = true; for (dListNum = baseDL, currentUnichar = first; currentUnichar < first + count; dListNum++, currentUnichar++) { currentChar = new NSString (Char.ToString (currentUnichar)); charSize = currentChar.StringSize (attribDict); charRect.Size = charSize; charRect = charRect.Integral (); if (charRect.Size.Width > 0 && charRect.Size.Height > 0) { theImage.Size = charRect.Size; theImage.LockFocus (); NSGraphicsContext.CurrentContext.ShouldAntialias = false; blackColor.Set (); NSBezierPath.FillRect (charRect); currentChar.DrawString (charRect, attribDict); theImage.UnlockFocus (); if (!MakeDisplayList(dListNum, theImage)) { retval = false; break; } } } return retval; }
private void DrawStringOutline(string text, NSColor color, RectangleF rect, int align) { NSString nsString = new NSString (text); int halign = align % 3; int valign = align / 3; var objectsText = new object[] { m_font, color }; var keysText = new object[] { NSAttributedString.FontAttributeName, NSAttributedString.ForegroundColorAttributeName }; var attributesText = NSDictionary.FromObjectsAndKeys(objectsText, keysText); var objectsOutline = new object[] { m_font, NSColor.White }; var keysOutline = new object[] { NSAttributedString.FontAttributeName, NSAttributedString.ForegroundColorAttributeName }; var attributesOutline = NSDictionary.FromObjectsAndKeys(objectsOutline, keysOutline); SizeF size = nsString.StringSize (attributesText); if (halign == 0) { } else if (halign == 1) { rect.X = (rect.Left + rect.Right) / 2 - size.Width / 2; } else if (halign == 2) { rect.X = rect.Right - size.Width; } rect.Width = size.Width; if (valign == 0) { } else if (valign == 1) { rect.Y = (rect.Top + rect.Bottom) / 2 - size.Height / 2; } else if (valign == 2) { rect.Y = rect.Bottom - size.Height; } rect.Height = size.Height; NSColor.Black.Set (); for (int ox = -1; ox <= 1; ox++) { for (int oy = -1; oy <= 1; oy++) { RectangleF rectString = rect; rectString.Offset (new PointF (ox, oy)); nsString.DrawString (Invert (rectString), attributesOutline); } } nsString.DrawString(Invert(rect), attributesText); }