Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS MacBase 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: /MacControls/Label with Color
This example is the version from Mon, 22th Nov 2015.
Project "Label with Color.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
EventHandler Sub Open()
// get a font
dim font as NSFontMBS = NSFontMBS.fontWithName("Lucida Grande", 18.0)
// get some colors
dim RedColor as NSColorMBS = NSColorMBS.colorWithDeviceRGB(1.0, 0.0, 0.0)
dim GreenColor as NSColorMBS = NSColorMBS.colorWithDeviceRGB(0.0, 0.8, 0.0)
dim BlueColor as NSColorMBS = NSColorMBS.colorWithDeviceRGB(0.0, 0.0, 1.0)
// now make an attributed string
dim a as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithString("red green blue")
dim t as NSMutableAttributedStringMBS = a.mutableCopy
// set font for all characters
t.addAttribute(NSAttributedStringMBS.NSFontAttributeName, font, new NSRangeMBS(0, t.length))
// set red as foreground color for the range with the text "red". Ranges start with zero!
t.addAttribute(NSAttributedStringMBS.NSForegroundColorAttributeName, RedColor, new NSRangeMBS(0, 3))
// set green as foreground color for the range with the text "green"
t.addAttribute(NSAttributedStringMBS.NSForegroundColorAttributeName, GreenColor, new NSRangeMBS(4, 5))
// set blue as foreground color for the range with the text "blue"
t.addAttribute(NSAttributedStringMBS.NSForegroundColorAttributeName, BlueColor, new NSRangeMBS(10, 4))
// Now assign the label
dim lbl as NSTextfieldMBS = label1.NSTextfieldMBS
lbl.attributedStringValue = t
End EventHandler
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project
The items on this page are in the following plugins: MBS MacControls Plugin.