Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCG/CoreText/CoreText Test
Function:
Required plugins for this example: MBS MacBase Plugin, MBS MacCF Plugin, MBS MacCG Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCG/CoreText/CoreText Test
This example is the version from Mon, 8th Jun 2014.
Project "CoreText Test.rbp"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control Canvas1 Inherits Canvas
ControlInstance Canvas1 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) // create a font, quasi systemFontWithSize:24.0 dim sysUIFont as CTFontMBS = CTFontMBS.CreateUIFontForLanguage(CTFontMBS.kCTFontUIFontSystem, 24.0) // create a naked string dim text as string = "Some Text. Please resize window." // blue dim cgColor as CGColorMBS = CGColorMBS.CreateGenericRGB(0.0, 0.0, 1.0) // single underline dim underline as integer = CoreTextMBS.kCTUnderlineStyleSingle // pack it into attributes dictionary dim attributesDict as new Dictionary attributesDict.Value(CoreTextMBS.kCTFontAttributeName) = sysUIFont attributesDict.Value(CoreTextMBS.kCTForegroundColorAttributeName) = cgColor attributesDict.Value(CoreTextMBS.kCTUnderlineStyleAttributeName) = underline // make the attributed string dim cfDic as new CFDictionaryMBS(attributesDict) dim cfStr as new CFStringMBS(text) dim stringToDraw as CFAttributedStringMBS = CFAttributedStringMBS.Create( cfStr, cfDic) // now for the actual drawing dim CGContextHandle as integer = g.Handle(g.HandleTypeCGContextRef) dim CGContext as CGContextMBS = CGContextMBS.contextWithCGContext(CGContextHandle) CGContext.SaveGState // reset text matrix dim a as CGAffineTransformMBS = CGAffineTransformMBS.Identity CGContext.TextMatrix = a // draw dim line as CTLineMBS = CTLineMBS.CreateWithAttributedString(stringToDraw) dim x as integer = 10 dim y as integer = 10 // plus text height y = y + 24 // swap y y = g.Height - y CGContext.TextPosition = new CGPointMBS(x, y) line.Draw(CGContext) CGContext.RestoreGState CGContext.Flush // clean up End EventHandler
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action() Canvas1.Invalidate End EventHandler
End Control
Control Canvas2 Inherits Canvas
ControlInstance Canvas2 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) dim longText as string = TextArea1.Text dim s as new NSMutableAttributedStringMBS call s.initWithString(longText) // make a few words bold dim helvetica as CTFontMBS = CTFontMBS.CreateWithName("Helvetica", 14.0) dim helveticaBold as CTFontMBS = CTFontMBS.CreateWithName("Helvetica-Bold", 14.0) dim r1 as new NSRangeMBS(0, s.Length) s.addattribute(CoreTextMBS.kCTFontAttributeName, helvetica, r1) dim r2 as new NSRangeMBS(6, 5) s.addattribute(CoreTextMBS.kCTFontAttributeName, helveticaBold, r2) dim r3 as new NSRangeMBS(109, 12) s.addattribute(CoreTextMBS.kCTFontAttributeName, helveticaBold, r3) dim r4 as new NSRangeMBS(223, 6) s.addattribute(CoreTextMBS.kCTFontAttributeName, helveticaBold, r4) // add some color dim redColor as CGColorMBS = CGColorMBS.CreateGenericRGB(1, 0, 0) dim greenColor as CGColorMBS = CGColorMBS.CreateGenericRGB(0, 1, 0) dim blueColor as CGColorMBS = CGColorMBS.CreateGenericRGB(0, 0, 1) dim r5 as new NSRangeMBS(18, 3) s.addattribute(CoreTextMBS.kCTForegroundColorAttributeName, redColor, r5) dim r6 as new NSRangeMBS(657, 7) s.addattribute(CoreTextMBS.kCTForegroundColorAttributeName, greenColor, r6) dim r7 as new NSRangeMBS(155, 8) s.addattribute(CoreTextMBS.kCTForegroundColorAttributeName, blueColor, r7) // layout master dim cs as CFAttributedStringMBS = s.AsCFAttributedString dim framesetter as CTFramesetterMBS = CTFramesetterMBS.CreateWithAttributedString(cs) // left column form dim leftColumnPath as new CGMutablePathMBS dim leftRect as new CGRectMBS(0, 0, g.Width/2, g.Height) leftColumnPath.AddRect nil, leftRect // left column frame dim leftFrame as CTFrameMBS = framesetter.CreateFrame(0, 0, leftColumnPath, nil) // right column form dim rightColumnPath as new CGMutablePathMBS dim rightRect as new CGRectMBS(g.width/2, 0, g.width/2, g.height) rightColumnPath.AddRect nil, rightRect dim rightColumStart as integer = leftFrame.VisibleStringRangeLength // right column frame dim rightFrame as CTFrameMBS = framesetter.CreateFrame(rightColumStart, 0, rightColumnPath, nil) // get context dim CGContextHandle as integer = g.Handle(g.HandleTypeCGContextRef) dim CGContext as CGContextMBS = CGContextMBS.contextWithCGContext(CGContextHandle) CGContext.SaveGState // reset text matrix dim a as CGAffineTransformMBS = CGAffineTransformMBS.Identity CGContext.TextMatrix = a // draw leftFrame.Draw(CGContext) rightFrame.Draw(CGContext) // cleanup CGContext.RestoreGState CGContext.Flush End EventHandler
End Control
Control TextArea1 Inherits TextArea
ControlInstance TextArea1 Inherits TextArea
End Control
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
End Project

See also:

Feedback, Comments & Corrections

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




Links
MBS Xojo blog