Another open source module for you guys!
I hate TextBoxes and their total lack of important functionality.
Reasons for hating, in order of most to least hated
TextWrapped doesn’t add control chars to mark where it added newlines, so any line related code is left in the dark. This is my main aggravation.
It doesn’t actually allow tabs. It just renders them as a single space, ruining everything.
There’s no Up/Down arrows to move the cursor.
It’s missing so many simple shortcut controls like Ctrl-D.
There’s no Ctrl-Z/Y for undo/redo. This is fair enough, but I want it anyway so I made it.
Therefore, I wrote this module to make them usable. Not great, but good enough.
Features:
-
Undo/Redo support.
Ctrl-Z and Ctrl-Y are done automatically, and you can call:Undo()
and:Redo()
on the controller object. Snapshots are taken when you stop typing, or if you haven’t taken one in 3 seconds, or if you’ve added 10+ chars since last snapshot. This works well, without having to snapshot every single changed character. -
Ctrl-D to select current word.
-
Built into a ScrollingFrame that autosizes to fit the text.
You can do Settings.Padded = true so that you can scroll down to bring the last line to the top of the frame. -
Up/Down arrows cursor movement.
This only works when.MultiLine
is true and.TextWrapped
is false, because.TextWrapped
doesn’t mark where it created newlines sostring.split(Text,"\n")
doesn’t work anymore.
API:
TBPlus:
function TBPlus.new(Frame, Settings)
Creates a TBPlusObject
Parameters:
-
Frame
[GuiObject]
The GuiObject to parent the TBPlus object to -
Settings
[Optional Table]- number
Settings.TextSize
(Defaults to 16) - boolean
Settings.Padded
(Defaults to true) - boolean
Settings.TextWrapped
(Defaults to true) - boolean
Settings.MultiLine
(Defaults to true) - Color3
Settings.TextColor3
(Defaults to black) - Enum
Settings.Font
(Defaults to SourceSans) - boolean
Settings.ClearTextOnFocus
(Defaults to false) - Enum
Settings.TextXAlignment
(Defaults to Left) - Enum
Settings.TextYAlignment
(Defaults to Top) - string
Settings.PlaceholderText
(Defaults to blank) - Color3
Settings.PlaceholderColor3
(Defaults to dark grey)
- number
Returns:
TBPlusObject
Note that the TBPlusObject has .TextBox
which is a pointer to the actual TextBox, so you can easily tweak properties like .PlaceholderText
without needing to send via Settings.
TBPlusObject:
object TBPlusObject.TextBox
Points to the actual TextBox object
Returns:
TextBox
This is mostly used to check the current .Text
of the TextBox or do something with the events/functions of the TextBox.
function TBPlusObject.Write(Text,Start,End)
Writes the Text in at the given start and end position
Parameters:
-
Text
[string]
The text to write in -
Start
[number]
The position to start the text -
End
[number]
The position to end the text
Returns:
void
Note that if Start and End are the same, it’s just inserting text at that point, whereas if they are different then it’s overwriting text that was in between those positions.
function TBPlusObject.SetContent(Text)
Sets the TextBox’s Text and clears the Undo/Redo stack
Parameters:
-
Text
[string]
The text to write in
Returns:
void
function TBPlusObject:Undo()
Undoes by one snapshot
Parameters:
N/A
Returns:
void
function TBPlusObject:Redo()
Redoes by one snapshot
Parameters:
N/A
Returns:
void
Example Code:
local TBPlusObject = require(script.TextBoxPlus).new(
script.Parent.Frame,
{
TextSize = 15;
TextWrapped = false;
Font = Enum.Font.Code;
}
)
TBPlusObject.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
print(TBPlusObject.TextBox.CursorPosition)
end)
Module:
I hope you find this useful! I use a slightly modified version for the markdown editor in Lua Learning, so I figured it’s worth sharing.