學習
引擎類別
TextBox

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡


範例程式碼
文本框密碼字
-- 將此代碼放在文本框內的 LocalScript 中
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- 白色
local colorWrong = Color3.new(1, 0, 0) -- 紅色
local colorCorrect = Color3.new(0, 1, 0) -- 綠色
-- 初始化文本框的狀態
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "秘密字是什麼?"
textBox.BackgroundColor3 = colorNormal
local function onFocused()
textBox.BackgroundColor3 = colorNormal
end
local function onFocusLost(enterPressed, _inputObject)
if enterPressed then
local guess = textBox.Text
if guess == secretWord then
textBox.Text = "訪問授權"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "訪問拒絕"
textBox.BackgroundColor3 = colorWrong
end
else
-- 玩家在未按下 Enter 的情況下停止編輯
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)

API 參考
屬性
ClearTextOnFocus
平行讀取
功能: UI
TextBox.ClearTextOnFocus:boolean

ContentText
唯讀
未複製
平行讀取
功能: UI
TextBox.ContentText:string

CursorPosition
平行讀取
功能: UI
TextBox.CursorPosition:number
範例程式碼
文字框選擇
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("沒有選擇")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('選擇內容是:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)

Font
隱藏
未複製
平行讀取
功能: UI
TextBox.Font:Enum.Font

FontFace
平行讀取
功能: UI
TextBox.FontFace:Font

FontSize
已棄用

LineHeight
平行讀取
功能: UI
TextBox.LineHeight:number

MaxVisibleGraphemes
平行讀取
功能: UI
TextBox.MaxVisibleGraphemes:number

MultiLine
平行讀取
功能: UI
TextBox.MultiLine:boolean

OpenTypeFeatures
平行讀取
功能: UI
TextBox.OpenTypeFeatures:string

OpenTypeFeaturesError
唯讀
未複製
平行讀取
功能: UI
TextBox.OpenTypeFeaturesError:string

PlaceholderColor3
平行讀取
功能: UI
TextBox.PlaceholderColor3:Color3

PlaceholderText
平行讀取
功能: UI
TextBox.PlaceholderText:string

RichText
平行讀取
功能: UI
TextBox.RichText:boolean

SelectionStart
平行讀取
功能: UI
TextBox.SelectionStart:number
範例程式碼
文字框選擇
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("沒有選擇")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('選擇內容是:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)

ShowNativeInput
平行讀取
功能: UI
TextBox.ShowNativeInput:boolean

Text
平行讀取
功能: UI
TextBox.Text:string

TextBounds
唯讀
未複製
平行讀取
功能: UI
TextBox.TextBounds:Vector2

TextColor
已棄用

TextColor3
平行讀取
功能: UI
TextBox.TextColor3:Color3
範例程式碼
母音檢測器
local textBox = script.Parent
local function hasVowels(str)
return\ str:lower():find("[aeiou]")
end
local function onTextChanged()
local\ text = textBox.Text
-- 檢查母音
if hasVowels(text) then
\ textBox.TextColor3 = Color3.new(0, 0, 0) -- 黑色
else
textBox.TextColor3\ = Color3.new(1, 0, 0) -- 紅色
end
end
textBox:GetPropertyChangedSignal("\ Text"):Connect(onTextChanged)

TextDirection
平行讀取
功能: UI
TextBox.TextDirection:Enum.TextDirection

TextEditable
平行讀取
功能: UI
TextBox.TextEditable:boolean

TextFits
唯讀
未複製
平行讀取
功能: UI
TextBox.TextFits:boolean

TextScaled
平行讀取
功能: UI
TextBox.TextScaled:boolean

TextSize
平行讀取
功能: UI
TextBox.TextSize:number

TextStrokeColor3
平行讀取
功能: UI
TextBox.TextStrokeColor3:Color3

TextStrokeTransparency
平行讀取
功能: UI
TextBox.TextStrokeTransparency:number

TextTransparency
平行讀取
功能: UI
TextBox.TextTransparency:number

TextTruncate
平行讀取
功能: UI
TextBox.TextTruncate:Enum.TextTruncate

TextWrap
已棄用

TextWrapped
平行讀取
功能: UI
TextBox.TextWrapped:boolean

TextXAlignment
平行讀取
功能: UI
TextBox.TextXAlignment:Enum.TextXAlignment

TextYAlignment
平行讀取
功能: UI
TextBox.TextYAlignment:Enum.TextYAlignment

方法
CaptureFocus
功能: UI
TextBox:CaptureFocus():()
返回
()
範例程式碼
捕獲文本框焦點
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
local input = UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Q then
textBox:CaptureFocus()
end
end)

IsFocused
功能: UI
TextBox:IsFocused():boolean
返回

ReleaseFocus
功能: UI
TextBox:ReleaseFocus(submitted:boolean):()
參數
submitted:boolean
預設值:false
返回
()
範例程式碼
釋放文字框焦點
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
local input = UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Escape and textBox.IsFocused then
textBox:ReleaseFocus()
end
end)

活動
Focused
功能: UI
TextBox.Focused():RBXScriptSignal
範例程式碼
文字框聚焦
local textBox = script.Parent
textBox.Focused:Connect(function()
print("已聚焦!")
end)
textBox.FocusLost:Connect(function()
print("聚焦失去!")
end)

FocusLost
功能: UI
TextBox.FocusLost(
enterPressed:boolean, inputThatCausedFocusLoss:InputObject
參數
enterPressed:boolean
inputThatCausedFocusLoss:InputObject
範例程式碼
文字框聚焦
local textBox = script.Parent
textBox.Focused:Connect(function()
print("已聚焦!")
end)
textBox.FocusLost:Connect(function()
print("聚焦失去!")
end)
聚焦失去
local textBox = script.Parent
local function onFocusLost(enterPressed, inputThatCausedFocusLost)
if enterPressed then
print("玩家按下 Enter")
else
print("玩家按下", inputThatCausedFocusLost.KeyCode)
end
end
textBox.FocusLost:Connect(onFocusLost)
TextBox.FocusLost1
local gui = script.Parent
local textBox = gui.TextBox
local function focusLost(enterPressed)
if enterPressed then
print("焦點因按下 Enter 鍵而丟失!")
else
print("焦點未按下 Enter 鍵而丟失")
end
end
textBox.FocusLost:Connect(focusLost)

ReturnPressedFromOnScreenKeyboard
功能: UI
TextBox.ReturnPressedFromOnScreenKeyboard():RBXScriptSignal

©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。