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: /MacFrameworks/Data Detectors
This example is the version from Sun, 20th Feb 2021.
Project "Data Detectors.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
Control TextArea1 Inherits TextArea
ControlInstance TextArea1 Inherits TextArea
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control MyButton Inherits PushButton
ControlInstance MyButton Inherits PushButton
EventHandler Sub Action()
Check
End EventHandler
End Control
Private Sub Check()
// create detector for Address
Dim types As Integer = _
NSTextCheckingResultMBS.NSTextCheckingTypeAddress + _
NSTextCheckingResultMBS.NSTextCheckingTypeDate + _
NSTextCheckingResultMBS.NSTextCheckingTypeLink + _
NSTextCheckingResultMBS.NSTextCheckingTypePhoneNumber + _
NSTextCheckingResultMBS.NSTextCheckingTypeTransitInformation
List.DeleteAllRows
Dim Error As NSErrorMBS
Dim DataDetector As New NSDataDetectorMBS(types, error)
Dim s As String = TextArea1.Text
Dim st As StyledText = TextArea1.StyledText
st.TextColor(0, s.Len) = Color.Black
// analyze now
Dim matches() As NSTextCheckingResultMBS = DataDetector.matches(s, 0)
// show Address
For Each result As NSTextCheckingResultMBS In matches
Dim r As NSRangeMBS = result.range
Dim resultType As String
Select Case result.resultType
Case NSTextCheckingResultMBS.NSTextCheckingTypeAddress
resultType = "Address"
st.TextColor(r.Location, r.Length) = Color.Red
Case NSTextCheckingResultMBS.NSTextCheckingTypeDate
resultType = "Date"
st.TextColor(r.Location, r.Length) = Color.Green
Case NSTextCheckingResultMBS.NSTextCheckingTypeLink
resultType = "Link"
st.TextColor(r.Location, r.Length) = Color.Blue
Case NSTextCheckingResultMBS.NSTextCheckingTypePhoneNumber
resultType = "PhoneNumber"
st.TextColor(r.Location, r.Length) = Color.Purple
Case NSTextCheckingResultMBS.NSTextCheckingTypeTransitInformation
resultType = "TransitInformation"
st.TextColor(r.Location, r.Length) = Color.Brown
Else
Break
End Select
List.AddRow "Found "+resultType+" at "+Str(r.Location+1)
List.AddRow "Text: "+s.Mid(r.Location+1, r.Length)
// show details_
Select Case result.resultType
Case NSTextCheckingResultMBS.NSTextCheckingTypeAddress, _
NSTextCheckingResultMBS.NSTextCheckingTypeTransitInformation
Dim dic As Dictionary = result.components
Dim lines() As String
For Each key As Variant In dic.keys
list.AddRow key.StringValue+": "+dic.Value(key).StringValue
Next
Case NSTextCheckingResultMBS.NSTextCheckingTypeDate
Dim d As date = Result.date
If d <> Nil Then
List.AddRow "Date: "+d.AbbreviatedDate +" "+d.LongTime
End If
Dim du As Double = result.duration
If du <> 0 Then
List.AddRow "Duration: "+Str(du)
End If
Case NSTextCheckingResultMBS.NSTextCheckingTypeLink
List.AddRow "URL: "+result.URL
Case NSTextCheckingResultMBS.NSTextCheckingTypePhoneNumber
List.AddRow "PhoneNumber: "+result.phoneNumber
Else
Break
End Select
List.AddRow ""
Next
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
The items on this page are in the following plugins: MBS MacFrameworks Plugin.