Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/Font PopupMenu in Cocoa


Required plugins for this example: MBS MacBase Plugin, MBS MacControls Plugin, MBS MacCocoa Plugin, MBS Main Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/Font PopupMenu in Cocoa

This example is the version from Sat, 19th Dec 2014.

Project "Font PopupMenu in Cocoa.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control PopupMenu1 Inherits PopupMenu
ControlInstance PopupMenu1 Inherits PopupMenu
EventHandler Sub Open() // get font list from Xojo dim fonts() as string dim fu as integer = FontCount-1 for fi as integer = 0 to fu fonts.append font(fi) next fonts.Sort for each name as string in fonts me.AddRow name next ApplyFontsToItems me End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control PopupMenu2 Inherits PopupMenu
ControlInstance PopupMenu2 Inherits PopupMenu
EventHandler Sub Open() // getting font list from NSFontManager. dim fm as new NSFontManagerMBS dim fonts() as string = fm.availableFontFamilies fonts.Sort for each name as string in fonts me.AddRow name next ApplyFontsToItems me End EventHandler
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Sub ApplyFontsToItems(xp as PopupMenu) // Apply font to menu items in a popupmenu #if TargetCocoa then // graphics for measurement of height dim g as new NSGraphicsMBS dim si as new NSSizeMBS(1000, 1000) // the Cocoa popup control and it's menu dim p as NSPopUpButtonMBS = xp.NSPopUpButtonMBS dim m as NSMenuMBS = p.menu // font size to use? dim fontSize as Double = xp.TextSize if fontsize <= 8 then fontsize = NSFontMBS.systemFontSize end if // walk over all items dim mu as integer = m.numberOfItems-1 for mi as integer = 0 to mu dim item as NSMenuItemMBS = m.Item(mi) // get the title and use it to make font dim name as string = item.Title dim f as NSFontMBS = NSFontMBS.fontWithName(name, fontSize) // now build attributed string with that font dim a as new NSMutableAttributedStringMBS if a.initWithString(name) then dim r as new NSRangeMBS(0, a.length) a.addAttribute(a.NSFontAttributeName, f, r) // calculate bounding Rectangle dim re as NSRectMBS = g.boundingRectWithSize(a, si, 0) if re.Height <= 22 then // only apply font if height is not too much item.attributedTitle = a end if end if next #endif End Sub
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project

See also:

The items on this page are in the following plugins: MBS MacCocoa Plugin.


The biggest plugin in space...