public object SetSize(object handle, double size, FontSizeUnit sizeUnit) { NSFont f = (NSFont)handle; var matrix = f.FontDescriptor.Matrix ?? new NSAffineTransform(); return(NSFont.FromDescription(f.FontDescriptor.FontDescriptorWithSize((float)size), matrix)); }
static NSFont _ToNativeFont(string family, float size, FontAttributes attributes) { NSFont defaultFont = NSFont.SystemFontOfSize(size); NSFont font = null; NSFontDescriptor descriptor = null; var bold = (attributes & FontAttributes.Bold) != 0; var italic = (attributes & FontAttributes.Italic) != 0; if (family != null && family != DefaultFontName) { try { descriptor = new NSFontDescriptor().FontDescriptorWithFamily(family); font = NSFont.FromDescription(descriptor, size); if (font == null) { var cleansedFont = CleanseFontName(family); font = NSFont.FromFontName(cleansedFont, size); } } catch { Debug.WriteLine("Could not load font named: {0}", family); } } //if we didn't found a Font or Descriptor for the FontFamily use the default one if (font == null) { font = defaultFont; descriptor = defaultFont.FontDescriptor; } if (descriptor == null) { descriptor = defaultFont.FontDescriptor; } if (bold || italic) { var traits = (NSFontSymbolicTraits)0; if (bold) { traits |= NSFontSymbolicTraits.BoldTrait; } if (italic) { traits |= NSFontSymbolicTraits.ItalicTrait; } var fontDescriptorWithTraits = descriptor.FontDescriptorWithSymbolicTraits(traits); font = NSFont.FromDescription(fontDescriptorWithTraits, size); } return(font.ScreenFontWithRenderingMode(NSFontRenderingMode.AntialiasedIntegerAdvancements)); }
protected void UpdateFont(NSControl control, int sizeDelta = 0) { var font = TreeView.CustomFont; if (sizeDelta != 0) { control.Font = NSFont.FromDescription(font.FontDescriptor, font.PointSize + sizeDelta); } else { control.Font = font; } }
public override void AwakeFromNib() { var frame = Frame; while (true) { var size = AttributedStringValue.Size; if (size.Width <= frame.Width && size.Height <= frame.Height) { break; } Font = NSFont.FromDescription(Font.FontDescriptor, Font.PointSize - 0.25f); } }
public object SetWeight(object handle, FontWeight weight) { NSFont f = (NSFont)handle; NSFontSymbolicTraits traits = f.FontDescriptor.SymbolicTraits; if (weight > FontWeight.Normal) { traits |= NSFontSymbolicTraits.BoldTrait; } else { traits &= ~NSFontSymbolicTraits.BoldTrait; } return(NSFont.FromDescription(f.FontDescriptor.FontDescriptorWithSymbolicTraits(traits), f.FontDescriptor.Matrix)); }
public object SetStyle(object handle, FontStyle style) { NSFont f = (NSFont)handle; NSFontSymbolicTraits traits = f.FontDescriptor.SymbolicTraits; if (style == FontStyle.Italic || style == FontStyle.Oblique) { traits |= NSFontSymbolicTraits.ItalicTrait; } else { traits &= ~NSFontSymbolicTraits.ItalicTrait; } return(NSFont.FromDescription(f.FontDescriptor.FontDescriptorWithSymbolicTraits(traits), f.FontDescriptor.Matrix)); }
void UpdateFont() { if (Control == null || Element == null) { return; } var newFont = Element.ToNSFont(); // The font needs to have the default font size to avoid clipping var originalFontSize = (NSNumber)Control.Font.FontDescriptor.FontAttributes[NSFont.SizeAttribute]; // Recreate the font with the default size newFont = NSFont.FromDescription(newFont.FontDescriptor, originalFontSize.FloatValue); // Apply the font Control.Font = newFont; }
private PlatformFont GetFont() { if (_native == null) { #if __MACOS__ using (var descriptor = !string.IsNullOrEmpty(FontFamily) ? NSFontDescriptor.FromNameSize(FontFamily, FontSize) : new NSFontDescriptor()) { if (IsItalic) { using (var descriptor2 = descriptor.FontDescriptorWithSymbolicTraits(NSFontSymbolicTraits.ItalicTrait)) { _native = NSFont.FromDescription(descriptor2, FontSize); } } else { _native = NSFont.FromDescription(descriptor, FontSize); } } #else using (var descriptor = !string.IsNullOrEmpty(FontFamily) ? UIFontDescriptor.FromName(FontFamily, FontSize) : new UIFontDescriptor()) { if (IsItalic) { using (var descriptor2 = descriptor.CreateWithTraits(UIFontDescriptorSymbolicTraits.Italic)) { _native = UIFont.FromDescriptor(descriptor2, FontSize); } } else { _native = UIFont.FromDescriptor(descriptor, FontSize); } } #endif } return(_native); }
public object SetStretch(object handle, FontStretch stretch) { NSFont f = (NSFont)handle; NSFontSymbolicTraits traits = f.FontDescriptor.SymbolicTraits; if (stretch < FontStretch.Normal) { traits |= NSFontSymbolicTraits.CondensedTrait; traits &= ~NSFontSymbolicTraits.ExpandedTrait; } else if (stretch > FontStretch.Normal) { traits |= NSFontSymbolicTraits.ExpandedTrait; traits &= ~NSFontSymbolicTraits.CondensedTrait; } else { traits &= ~NSFontSymbolicTraits.ExpandedTrait; traits &= ~NSFontSymbolicTraits.CondensedTrait; } return(NSFont.FromDescription(f.FontDescriptor.FontDescriptorWithSymbolicTraits(traits), f.FontDescriptor.Matrix)); }
protected static nfloat GetWidthForString(NSFont font, string text, int sizeDelta = 0) { NSFont modified = null; nfloat width; if (sizeDelta != 0) { modified = NSFont.FromDescription(font.FontDescriptor, font.PointSize + sizeDelta); } using (var str = new NSAttributedString(text, font: modified ?? font)) width = str.Size.Width; modified?.Dispose(); width = NMath.Ceiling(width); // Note: All code-paths that use sizeDelta == 0 are for NSTextField labels and the only code-path // that uses sizeDelta != 0 is for the Show More label in an NSButton. if (sizeDelta == 0) { // Note: In order to match NSTextField.Frame.Width after calling TextField.SizeToFit(), add 4px. width += 4; // Note: NSTextField also seems to need an extra 8px to actually fit the entire text w/o clipping. width += 8; } else { // Oddly enough, NSButton padding around the label is also +12px (matched after button.SizeToFit() // and checking the resulting button.Frame.Width). width += 12; } return(width); }
public static UIFont ToNSFont(this Font self) #endif { var size = (float)self.FontSize; if (self.UseNamedSize) { switch (self.NamedSize) { case NamedSize.Micro: size = 12; break; case NamedSize.Small: size = 14; break; case NamedSize.Medium: size = 17; // as defined by iOS documentation break; case NamedSize.Large: size = 22; break; default: size = 17; break; } } var bold = self.FontAttributes.HasFlag(FontAttributes.Bold); var italic = self.FontAttributes.HasFlag(FontAttributes.Italic); if (self.FontFamily != null && self.FontFamily != DefaultFontName) { try { #if __MOBILE__ if (UIFont.FamilyNames.Contains(self.FontFamily)) { var descriptor = new UIFontDescriptor().CreateWithFamily(self.FontFamily); if (bold || italic) { var traits = (UIFontDescriptorSymbolicTraits)0; if (bold) { traits = traits | UIFontDescriptorSymbolicTraits.Bold; } if (italic) { traits = traits | UIFontDescriptorSymbolicTraits.Italic; } descriptor = descriptor.CreateWithTraits(traits); return(UIFont.FromDescriptor(descriptor, size)); } } return(UIFont.FromName(self.FontFamily, size)); #else var descriptor = new NSFontDescriptor().FontDescriptorWithFamily(self.FontFamily); if (bold || italic) { var traits = (NSFontSymbolicTraits)0; if (bold) { traits = traits | NSFontSymbolicTraits.BoldTrait; } if (italic) { traits = traits | NSFontSymbolicTraits.ItalicTrait; } descriptor = descriptor.FontDescriptorWithSymbolicTraits(traits); return(NSFont.FromDescription(descriptor, size)); } return(NSFont.FromFontName(self.FontFamily, size)); #endif } catch { Debug.WriteLine("Could not load font named: {0}", self.FontFamily); } } if (bold && italic) { var defaultFont = UIFont.SystemFontOfSize(size); #if __MOBILE__ var descriptor = defaultFont.FontDescriptor.CreateWithTraits(UIFontDescriptorSymbolicTraits.Bold | UIFontDescriptorSymbolicTraits.Italic); return(UIFont.FromDescriptor(descriptor, 0)); } if (italic) { return(UIFont.ItalicSystemFontOfSize(size)); } #else var descriptor = defaultFont.FontDescriptor.FontDescriptorWithSymbolicTraits( NSFontSymbolicTraits.BoldTrait | NSFontSymbolicTraits.ItalicTrait); return(NSFont.FromDescription(descriptor, 0)); }
public override void DrawRect(CGRect dirtyRect) { // Don't draw if we don't have a font or a title. if (Font == null || Title == string.Empty) { return; } // Initialize the text matrix to a known value CGContext context = NSGraphicsContext.CurrentContext.GraphicsPort; context.TextMatrix = CGAffineTransform.MakeIdentity(); // Draw a white background NSColor.White.Set(); context.FillRect(dirtyRect); CTLine line = new CTLine(AttributedString); int glyphCount = (int)line.GlyphCount; if (glyphCount == 0) { return; } GlyphArcInfo[] glyphArcInfo = new GlyphArcInfo[glyphCount]; PrepareGlyphArcInfo(line, glyphCount, glyphArcInfo); // Move the origin from the lower left of the view nearer to its center. context.SaveState(); context.TranslateCTM(dirtyRect.GetMidX(), dirtyRect.GetMidY() - Radius / 2); // Stroke the arc in red for verification. context.BeginPath(); context.AddArc(0, 0, Radius, (float)Math.PI, 0, true); context.SetStrokeColor(1, 0, 0, 1); context.StrokePath(); // Rotate the context 90 degrees counterclockwise. context.RotateCTM((float)PI_2); /* * Now for the actual drawing. The angle offset for each glyph relative to the previous * glyph has already been calculated; with that information in hand, draw those glyphs * overstruck and centered over one another, making sure to rotate the context after each * glyph so the glyphs are spread along a semicircular path. */ CGPoint textPosition = new CGPoint(0, Radius); context.TextPosition = textPosition; var runArray = line.GetGlyphRuns(); var runCount = runArray.Count(); var glyphOffset = 0; var runIndex = 0; for (; runIndex < runCount; runIndex++) { var run = runArray [runIndex]; var runGlyphCount = run.GlyphCount; bool drawSubstitutedGlyphsManually = false; CTFont runFont = run.GetAttributes().Font; // Determine if we need to draw substituted glyphs manually. Do so if the runFont is not // the same as the overall font. var description = NSFontDescriptor.FromNameSize(runFont.FamilyName, runFont.Size); NSFont rrunFont = NSFont.FromDescription(description, runFont.Size); // used for comparison if (DimsSubstitutedGlyphs && Font != rrunFont) { drawSubstitutedGlyphsManually = true; } var runGlyphIndex = 0; for (; runGlyphIndex < runGlyphCount; runGlyphIndex++) { var glyphRange = new NSRange(runGlyphIndex, 1); context.RotateCTM(-(glyphArcInfo [runGlyphIndex + glyphOffset].angle)); // Center this glyph by moving left by half its width. var glyphWidth = glyphArcInfo [runGlyphIndex + glyphOffset].width; var halfGlyphWidth = glyphWidth / 2.0; var positionForThisGlyph = new CGPoint(textPosition.X - (float)halfGlyphWidth, textPosition.Y); // Glyphs are positioned relative to the text position for the line, so offset text position leftwards by this glyph's // width in preparation for the next glyph. textPosition.X -= glyphWidth; CGAffineTransform textMatrix = run.TextMatrix; textMatrix.x0 = positionForThisGlyph.X; textMatrix.y0 = positionForThisGlyph.Y; context.TextMatrix = textMatrix; if (!drawSubstitutedGlyphsManually) { run.Draw(context, glyphRange); } else { // We need to draw the glyphs manually in this case because we are effectively applying a graphics operation by // setting the context fill color. Normally we would use kCTForegroundColorAttributeName, but this does not apply // as we don't know the ranges for the colors in advance, and we wanted demonstrate how to manually draw. var cgFont = runFont.ToCGFont(); var glyph = run.GetGlyphs(glyphRange); var position = run.GetPositions(glyphRange); context.SetFont(cgFont); context.SetFontSize(runFont.Size); context.SetFillColor(0.25f, 0.25f, 0.25f, 1); context.ShowGlyphsAtPositions(glyph, position, 1); } // Draw the glyph bounds if (ShowsGlyphBounds) { var glyphBounds = run.GetImageBounds(context, glyphRange); context.SetStrokeColor(0, 0, 1, 1); context.StrokeRect(glyphBounds); } // Draw the bounding boxes defined by the line metrics if (ShowsLineMetrics) { var lineMetrics = new CGRect(); nfloat ascent = 0; nfloat descent = 0; nfloat leading = 0; run.GetTypographicBounds(glyphRange, out ascent, out descent, out leading); // The glyph is centered around the y-axis lineMetrics.Location = new CGPoint(-(float)halfGlyphWidth, positionForThisGlyph.Y - descent); lineMetrics.Size = new CGSize(glyphWidth, ascent + descent); context.SetStrokeColor(0, 1, 0, 1); context.StrokeRect(lineMetrics); } } glyphOffset += (int)runGlyphCount; } context.RestoreState(); }
public object SetFamily(object handle, string family) { NSFont f = (NSFont)handle; return(NSFont.FromDescription(f.FontDescriptor.FontDescriptorWithFamily(family), f.FontDescriptor.Matrix)); }
public object Copy(object handle) { NSFont f = (NSFont)handle; return(NSFont.FromDescription(f.FontDescriptor, f.FontDescriptor.Matrix)); }
public object SetSize(object handle, double size) { NSFont f = (NSFont)handle; return(NSFont.FromDescription(f.FontDescriptor.FontDescriptorWithSize((float)size), f.FontDescriptor.Matrix)); }