学习
引擎类
TextBox

*此内容使用人工智能(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)
文本框.失去焦点1
local gui = script.Parent
local textBox = gui.TextBox
local function focusLost(enterPressed)
if enterPressed then
print("焦点因按下回车键而丢失!")
else
print("焦点在没有按下回车的情况下丢失")
end
end
textBox.FocusLost:Connect(focusLost)

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

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。