TextBox

Hiển Thị Bản Đã Lỗi Thời

*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.

Một TextBox cho phép người chơi cung cấp đầu vào văn bản.Nó hành xử tương tự như một TextButton , ngoại trừ rằng một TextBox duy nhất có thể được đưa vào trọng tâm bằng cách nhấp chuột, chạm hoặc lựa chọn gamepad.Trong khi tập trung, người chơi có thể sử dụng bàn phím để thay đổi thuộc tính Text.

  • Nếu không có văn bản, PlaceholderText sẽ hiển thị. Điều này hữu ích khi yêu cầu người chơi loại hoặc định dạng dữ liệu họ nên nhập.
  • Mặc định, thuộc tính ClearTextOnFocus được bật và đảm bảo không có văn bản hiện có khi một TextBox được tập trung.Điều này có thể không mong muốn đối với văn bản nên được chỉnh sửa bởi người chơi.
  • Thuộc tính MultiLine cho phép người chơi nhập nhiều dòng văn bản với các ký tự dòng mới ( \n ).

The ContextActionService tôn vinh các phím TextBox và sẽ tự động ngăn chặn sự kiện nhấn phím không được chuyển đến các hành động được gắn với ContextActionService:BindAction() .UserInputService.InputBegan và các sự kiện liên quan vẫn sẽ bắt lửa trong khi một TextBox đang ở trong tâm trí.

Tập trung trạng thái

Việc phát hiện và thay đổi trạng thái tập trung của một TextBox là có thể:

  • Bạn có thể sử dụng CaptureFocus khi một hộp thoại xuất hiện để người chơi không phải nhấp vào một TextBox khi nó trở nên có sẵn; bạn có thể sử dụng ContextActionService:BindAction() để gắn một phím cụ thể vào tập trung một TextBox bằng cách sử dụng chức năng này.Khi một TextBox đi vào tập trung, sự kiện Focused bắt lửa.
  • Bạn có thể phát hiện xem một TextBox nhất định có đang ở trong tâm điểm hay không bằng cách sử dụng IsFocused . Hoặc thay thế, UserInputService:GetFocusedTextBox() có thể được sử dụng để kiểm tra xem có bất kỳ TextBox nào đang ở trong tâm điểm hay không.
  • Khi người chơi hoàn thành nhập văn bản, sự kiện FocusLost xảy ra, cho thấy nếu người dùng đã nhấn Enter để gửi văn bản cùng với InputObject gây mất tập trung.Khi sử dụng bàn phím trên màn hình trên di động và bảng điều khiển, ReturnPressedFromOnScreenKeyboard cũng có thể bắn.
  • Nếu một số vấn đề quan trọng hơn xuất hiện trong lúc chơi trải nghiệm trò chơi, bạn có thể ReleaseFocus của TextBox để đảm bảo rằng lượt nhập của bàn phím của một người chơi trở lại trò chơi của bạn.

Chỉnh sửa văn bản

Một TextBox hỗ trợ lựa chọn văn bản thông qua các thuộc tính CursorPositionSelectionStart của nó.Sử dụng GetPropertyChangedSignal, bạn có thể phát hiện khi một lựa chọn thay đổi.Ngoài ra, người chơi có thể sao chép và dán văn bản vào một TextBox, bật khả năng hỗ trợ chuyển nhượng cơ bản.

Thông báo lọc văn bản Trò chơi dễ dàng giao tiếp người chơi với người chơi bằng văn bản, chẳng hạn như trò chuyện tùy chỉnh hoặc thẻ tên, phải lọc văn bản đó một cách thích hợp bằng TextService:FilterStringAsync() hoặc Chat:FilterStringAsync() .Nếu điều này không được thực hiện đúng cách, trò chơi của bạn có thể nhận được hành động kiểm duyệt.

Mẫu mã

This code sample creates a password-like interface for a TextBox, giving visual feedback on the player's input.

TextBox Secret Word

-- Place this code in a LocalScript inside a TextBox
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- white
local colorWrong = Color3.new(1, 0, 0) -- red
local colorCorrect = Color3.new(0, 1, 0) -- green
-- Initialize the state of the textBox
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "What is the secret word?"
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 = "ACCESS GRANTED"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "ACCESS DENIED"
textBox.BackgroundColor3 = colorWrong
end
else
-- The player stopped editing without pressing Enter
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)

Tóm Tắt

Thuộc Tính

  • Đọc Song Song

    Xác định xem nhấp vào TextBox có xóa bỏ tính chất TextBox.Text của nó hay không.

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song
  • Đọc Song Song

    Xác định offset của con trỏ văn bản bằng byte hoặc -1 nếu không có con trỏ.

  • Ẩn
    Không Sao Chép
    Đọc Song Song

    Xác định phông chữ được sử dụng để render văn bản.

  • Đọc Song Song

    Xác định phông chữ được sử dụng để render văn bản.

  • Đọc Song Song

    Tăng khoảng cách giữa các dòng văn bản trong TextBox .

  • Đọc Song Song

    Số tối đa các grapheme mà TextBox có thể hiển trình diễn.

  • Đọc Song Song

    Khi được đặt thành true, văn bản bên trong TextBox có thể di chuyển sang nhiều dòng. Điều này cũng cho phép người chơi sử dụng phím enter để di chuyển sang một dòng mới.

  • Đọc Song Song
  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song
  • Đọc Song Song

    Đặt màu văn bản được sử dụng khi chưa có văn bản nào được nhập vào TextBox.

  • Đọc Song Song

    Đặt văn bản được hiển thị khi chưa có văn bản nào được nhập vào TextBox.

  • Đọc Song Song

    Xác định xem TextBox có hiển thị chuỗi TextBox.Text bằng cách sử dụng định dạng dữ liệu giàu hay không.

  • Đọc Song Song

    Xác định vị trí bắt đầu của một lựa chọn văn bản, hoặc -1 nếu không có văn bản được lựa chọn.

  • Đọc Song Song

    Nếu được đặt thành true, nhập bản địa vào nền tảng được sử dụng thay vì bàn phím mặc định của Roblox.

  • Đọc Song Song

    Xác định chuỗi được hiển thị bởi thành phần UI.

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song

    Kích thước của văn bản của một thành phần UI trong khấu trừ.

  • Đọc Song Song

    Xác định màu của văn bản được hiển thị.

  • Đọc Song Song

    Xác định xem người dùng có thể thay đổi Text hay không.

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song

    Xem có phù hợp với các hạn chế của TextBox không.

  • Đọc Song Song

    Thay đổi xem liệu văn bản có được thay đổi kích cỡ để phù hợp với đối tượng GUI hiển thị nó.

  • Đọc Song Song

    Xác định chiều cao dòng của văn bản trong khấu trừ.

  • Đọc Song Song

    Xác định màu của đường viền văn bản (đường viền).

  • Xác định độ trong suốt của đường viền văn bản (đường viền).

  • Đọc Song Song

    Xác định độ trong suốt của văn bản được hiển thị.

  • Kiểm soát việc rút ngắn văn bản được hiển thị trong TextBox này.

  • Đọc Song Song

    Xác định xem văn bản cuộn vào nhiều dòng trong không gian thành phần GuiObject, cắt bỏ văn bản dư thừa.

  • Xác định sự xếp hàng ngang của văn bản được hiển thị.

  • Xác định độ dọc của văn bản được hiển thị.

Thuộc Tính kế thừa từ GuiObject

Thuộc Tính

Thuộc Tính kế thừa từ GuiBase2d

Thuộc Tính

Phương Pháp

  • Buộc khách hàng tập trung vào TextBox.

  • Trả về true nếu hộp văn bản được tập trung, hoặc false nếu không.

  • ReleaseFocus(submitted : boolean):()

    Buộc khách hàng loại bỏ TextBox.

Phương Pháp kế thừa từ GuiObject

Phương Pháp

Sự Kiện

Sự Kiện kế thừa từ GuiObject

Sự Kiện

Sự Kiện kế thừa từ GuiBase2d

Sự Kiện

Thuộc Tính

ClearTextOnFocus

Đọc Song Song

ContentText

Chỉ Đọc
Không Sao Chép
Đọc Song Song

CursorPosition

Đọc Song Song

Mẫu mã

Lựa chọn TextBox

local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("No selection")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('The selection is:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)
Ẩn
Không Sao Chép
Đọc Song Song

Mẫu mã

Cycle Font

local textLabel = script.Parent
while true do
-- Iterate over all the different fonts
for _, font in pairs(Enum.Font:GetEnumItems()) do
textLabel.Font = font
textLabel.Text = font.Name
task.wait(1)
end
end
Hiển thị tất cả các phông chữ

local frame = script.Parent
-- Tạo một Thẻ văn bản hiển thị mỗi phông chữ
for _, font in pairs(Enum.Font:GetEnumItems()) do
local textLabel = Instance.new("TextLabel")
textLabel.Name = font.Name
-- Đặt tính chất văn bản
textLabel.Text = font.Name
textLabel.Font = font
-- Một số thuộc tính render
textLabel.TextSize = 24
textLabel.TextXAlignment = Enum.TextXAlignment.Left
-- Kích thước khung bằng chiều cao của văn bản
textLabel.Size = UDim2.new(1, 0, 0, textLabel.TextSize)
-- Thêm vào khung cha
textLabel.Parent = frame
end
-- Bố trí các khung trong một danh sách (nếu chúng chưa có)
if not frame:FindFirstChildOfClass("UIListLayout") then
local uiListLayout = Instance.new("UIListLayout")
uiListLayout.Parent = frame
end

FontFace

Đọc Song Song

LineHeight

Đọc Song Song

MaxVisibleGraphemes

Đọc Song Song

MultiLine

Đọc Song Song

OpenTypeFeatures

Đọc Song Song

OpenTypeFeaturesError

Chỉ Đọc
Không Sao Chép
Đọc Song Song

PlaceholderColor3

Đọc Song Song

PlaceholderText

Đọc Song Song

RichText

Đọc Song Song

SelectionStart

Đọc Song Song

Mẫu mã

Lựa chọn TextBox

local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("No selection")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('The selection is:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)

ShowNativeInput

Đọc Song Song

Text

Đọc Song Song

Mẫu mã

Fading Banner

local TweenService = game:GetService("TweenService")
local textLabel = script.Parent
local content = {
"Welcome to my game!",
"Be sure to have fun!",
"Please give suggestions!",
"Be nice to other players!",
"Don't grief other players!",
"Check out the shop!",
"Tip: Don't die!",
}
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local RNG = Random.new()
local fadeIn = TweenService:Create(textLabel, tweenInfo, {
TextTransparency = 0,
})
local fadeOut = TweenService:Create(textLabel, tweenInfo, {
TextTransparency = 1,
})
local lastIndex
while true do
-- Step 0: Fade out before doing anything
fadeOut:Play()
task.wait(tweenInfo.Time)
-- Step 1: pick content that wasn't the last displayed
local index
repeat
index = RNG:NextInteger(1, #content)
until lastIndex ~= index
-- Make sure we don't show the same thing next time
lastIndex = index
-- Step 2: show the content
textLabel.Text = content[index]
fadeIn:Play()
task.wait(tweenInfo.Time + 1)
end
Văn bản "Kaboom!"

local textLabel = script.Parent
textLabel.Text = "Kaboom!"
while true do
for size = 5, 100, 5 do
textLabel.TextSize = size
textLabel.TextTransparency = size / 100
task.wait()
end
task.wait(1)
end
Hiển thị tất cả các phông chữ

local frame = script.Parent
-- Tạo một Thẻ văn bản hiển thị mỗi phông chữ
for _, font in pairs(Enum.Font:GetEnumItems()) do
local textLabel = Instance.new("TextLabel")
textLabel.Name = font.Name
-- Đặt tính chất văn bản
textLabel.Text = font.Name
textLabel.Font = font
-- Một số thuộc tính render
textLabel.TextSize = 24
textLabel.TextXAlignment = Enum.TextXAlignment.Left
-- Kích thước khung bằng chiều cao của văn bản
textLabel.Size = UDim2.new(1, 0, 0, textLabel.TextSize)
-- Thêm vào khung cha
textLabel.Parent = frame
end
-- Bố trí các khung trong một danh sách (nếu chúng chưa có)
if not frame:FindFirstChildOfClass("UIListLayout") then
local uiListLayout = Instance.new("UIListLayout")
uiListLayout.Parent = frame
end
Long Text Wrapping

local textLabel = script.Parent
-- This text wrapping demo is best shown on a 200x50 px rectangle
textLabel.Size = UDim2.new(0, 200, 0, 50)
-- Some content to spell out
local content = "Here's a long string of words that will "
.. "eventually exceed the UI element's width "
.. "and form line breaks. Useful for paragraphs "
.. "that are really long."
-- A function that will spell text out two characters at a time
local function spellTheText()
-- Iterate from 1 to the length of our content
for i = 1, content:len() do
-- Get a substring of our content: 1 to i
textLabel.Text = content:sub(1, i)
-- Color the text if it doesn't fit in our box
if textLabel.TextFits then
textLabel.TextColor3 = Color3.new(0, 0, 0) -- Black
else
textLabel.TextColor3 = Color3.new(1, 0, 0) -- Red
end
-- Wait a brief moment on even lengths
if i % 2 == 0 then
task.wait()
end
end
end
while true do
-- Spell the text with scale/wrap off
textLabel.TextWrapped = false
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- Spell the text with wrap on
textLabel.TextWrapped = true
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- Spell the text with text scaling on
-- Note: Text turns red (TextFits = false) once text has to be
-- scaled down in order to fit within the UI element.
textLabel.TextScaled = true
-- Note: TextWrapped is enabled implicitly when TextScaled = true
--textLabel.TextWrapped = true
spellTheText()
task.wait(1)
end
Emoji in Text

local textLabel = script.Parent
local moods = {
["happy"] = "😃",
["sad"] = "😢",
["neutral"] = "😐",
["tired"] = "😫",
}
while true do
for mood, face in pairs(moods) do
textLabel.Text = "I am feeling " .. mood .. "! " .. face
task.wait(1)
end
end

TextBounds

Chỉ Đọc
Không Sao Chép
Đọc Song Song

Mẫu mã

Dynamic TextBox Size

local textBox = script.Parent
-- The smallest the TextBox will go
local minWidth, minHeight = 10, 10
-- Set alignment so our text doesn't wobble a bit while we type
textBox.TextXAlignment = Enum.TextXAlignment.Left
textBox.TextYAlignment = Enum.TextYAlignment.Top
local function updateSize()
textBox.Size = UDim2.new(0, math.max(minWidth, textBox.TextBounds.X), 0, math.max(minHeight, textBox.TextBounds.Y))
end
textBox:GetPropertyChangedSignal("TextBounds"):Connect(updateSize)

TextColor3

Đọc Song Song

Mẫu mã

Vowel Detector

local textBox = script.Parent
local function hasVowels(str)
return str:lower():find("[aeiou]")
end
local function onTextChanged()
local text = textBox.Text
-- Check for vowels
if hasVowels(text) then
textBox.TextColor3 = Color3.new(0, 0, 0) -- Black
else
textBox.TextColor3 = Color3.new(1, 0, 0) -- Red
end
end
textBox:GetPropertyChangedSignal("Text"):Connect(onTextChanged)
TextBox Secret Word

-- Place this code in a LocalScript inside a TextBox
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- white
local colorWrong = Color3.new(1, 0, 0) -- red
local colorCorrect = Color3.new(0, 1, 0) -- green
-- Initialize the state of the textBox
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "What is the secret word?"
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 = "ACCESS GRANTED"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "ACCESS DENIED"
textBox.BackgroundColor3 = colorWrong
end
else
-- The player stopped editing without pressing Enter
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)
Văn bản đếm ngược

-- Đặt mã này vào một LocalScript trong một TextLabel/TextButton
local textLabel = script.Parent
-- Một số màu chúng ta sẽ sử dụng với TextColor3
local colorNormal = Color3.new(0, 0, 0) -- đen
local colorSoon = Color3.new(1, 0.5, 0.5) -- đỏ
local colorDone = Color3.new(0.5, 1, 0.5) -- xanh lá
-- Vòng lặp vô cực
while true do
-- Đếm ngược từ 10 đến 1
for i = 10, 1, -1 do
-- Đặt văn bản
textLabel.Text = "Time: " .. i
-- Chỉnh màu dựa trên bao nhiêu thời gian còn lại
if i > 3 then
textLabel.TextColor3 = colorNormal
else
textLabel.TextColor3 = colorSoon
end
task.wait(1)
end
textLabel.Text = "GO!"
textLabel.TextColor3 = colorDone
task.wait(2)
end
Văn bản trạng thái trò chơi

local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Đặt một giá trị chuỗi có tên là "GameState" trong ReplicatedStorage
local vGameState = ReplicatedStorage:WaitForChild("GameState")
-- Đặt mã này vào một Thẻ Văn Bản
local textLabel = script.Parent
-- Một số màu chúng ta sẽ sử dụng với TextColor3
local colorNormal = Color3.new(0, 0, 0) -- đen
local colorCountdown = Color3.new(1, 0.5, 0) -- màu cam
local colorRound = Color3.new(0.25, 0.25, 1) -- màu xanh lam
-- Chúng tôi sẽ chạy chức năng này để cập nhật TextLabel như tình trạng của
-- thay đổi trò chơi.
local function update()
-- Cập nhật văn bản
textLabel.Text = "State: " .. vGameState.Value
-- Đặt màu của văn bản dựa trên tình trạng trò chơi hiện tại
if vGameState.Value == "Countdown" then
textLabel.TextColor3 = colorCountdown
elseif vGameState.Value == "Round" then
textLabel.TextColor3 = colorRound
else
textLabel.TextColor3 = colorNormal
end
end
-- Mẫu: cập nhật một lần khi chúng tôi bắt đầu và cũng khi vGameState thay đổi
-- Chúng ta nên luôn luôn xem GameState được cập nhật nhất.
update()
vGameState.Changed:Connect(update)

TextDirection

Đọc Song Song

TextEditable

Đọc Song Song

TextFits

Chỉ Đọc
Không Sao Chép
Đọc Song Song

TextScaled

Đọc Song Song

Mẫu mã

Long Text Wrapping

local textLabel = script.Parent
-- This text wrapping demo is best shown on a 200x50 px rectangle
textLabel.Size = UDim2.new(0, 200, 0, 50)
-- Some content to spell out
local content = "Here's a long string of words that will "
.. "eventually exceed the UI element's width "
.. "and form line breaks. Useful for paragraphs "
.. "that are really long."
-- A function that will spell text out two characters at a time
local function spellTheText()
-- Iterate from 1 to the length of our content
for i = 1, content:len() do
-- Get a substring of our content: 1 to i
textLabel.Text = content:sub(1, i)
-- Color the text if it doesn't fit in our box
if textLabel.TextFits then
textLabel.TextColor3 = Color3.new(0, 0, 0) -- Black
else
textLabel.TextColor3 = Color3.new(1, 0, 0) -- Red
end
-- Wait a brief moment on even lengths
if i % 2 == 0 then
task.wait()
end
end
end
while true do
-- Spell the text with scale/wrap off
textLabel.TextWrapped = false
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- Spell the text with wrap on
textLabel.TextWrapped = true
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- Spell the text with text scaling on
-- Note: Text turns red (TextFits = false) once text has to be
-- scaled down in order to fit within the UI element.
textLabel.TextScaled = true
-- Note: TextWrapped is enabled implicitly when TextScaled = true
--textLabel.TextWrapped = true
spellTheText()
task.wait(1)
end

TextSize

Đọc Song Song

Mẫu mã

Văn bản "Kaboom!"

local textLabel = script.Parent
textLabel.Text = "Kaboom!"
while true do
for size = 5, 100, 5 do
textLabel.TextSize = size
textLabel.TextTransparency = size / 100
task.wait()
end
task.wait(1)
end

TextStrokeColor3

Đọc Song Song

Mẫu mã

Text Highlight Oscillation

local textLabel = script.Parent
-- How fast the highlight ought to blink
local freq = 2
-- Set to yellow highlight color
textLabel.TextStrokeColor3 = Color3.new(1, 1, 0)
while true do
-- math.sin oscillates from -1 to 1, so we change the range to 0 to 1:
local transparency = math.sin(workspace.DistributedGameTime * math.pi * freq) * 0.5 + 0.5
textLabel.TextStrokeTransparency = transparency
task.wait()
end

TextStrokeTransparency

Đọc Song Song

Mẫu mã

Text Highlight Oscillation

local textLabel = script.Parent
-- How fast the highlight ought to blink
local freq = 2
-- Set to yellow highlight color
textLabel.TextStrokeColor3 = Color3.new(1, 1, 0)
while true do
-- math.sin oscillates from -1 to 1, so we change the range to 0 to 1:
local transparency = math.sin(workspace.DistributedGameTime * math.pi * freq) * 0.5 + 0.5
textLabel.TextStrokeTransparency = transparency
task.wait()
end

TextTransparency

Đọc Song Song

Mẫu mã

Fading Banner

local TweenService = game:GetService("TweenService")
local textLabel = script.Parent
local content = {
"Welcome to my game!",
"Be sure to have fun!",
"Please give suggestions!",
"Be nice to other players!",
"Don't grief other players!",
"Check out the shop!",
"Tip: Don't die!",
}
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local RNG = Random.new()
local fadeIn = TweenService:Create(textLabel, tweenInfo, {
TextTransparency = 0,
})
local fadeOut = TweenService:Create(textLabel, tweenInfo, {
TextTransparency = 1,
})
local lastIndex
while true do
-- Step 0: Fade out before doing anything
fadeOut:Play()
task.wait(tweenInfo.Time)
-- Step 1: pick content that wasn't the last displayed
local index
repeat
index = RNG:NextInteger(1, #content)
until lastIndex ~= index
-- Make sure we don't show the same thing next time
lastIndex = index
-- Step 2: show the content
textLabel.Text = content[index]
fadeIn:Play()
task.wait(tweenInfo.Time + 1)
end
Văn bản "Kaboom!"

local textLabel = script.Parent
textLabel.Text = "Kaboom!"
while true do
for size = 5, 100, 5 do
textLabel.TextSize = size
textLabel.TextTransparency = size / 100
task.wait()
end
task.wait(1)
end

TextTruncate

Đọc Song Song

TextWrapped

Đọc Song Song

Mẫu mã

Long Text Wrapping

local textLabel = script.Parent
-- This text wrapping demo is best shown on a 200x50 px rectangle
textLabel.Size = UDim2.new(0, 200, 0, 50)
-- Some content to spell out
local content = "Here's a long string of words that will "
.. "eventually exceed the UI element's width "
.. "and form line breaks. Useful for paragraphs "
.. "that are really long."
-- A function that will spell text out two characters at a time
local function spellTheText()
-- Iterate from 1 to the length of our content
for i = 1, content:len() do
-- Get a substring of our content: 1 to i
textLabel.Text = content:sub(1, i)
-- Color the text if it doesn't fit in our box
if textLabel.TextFits then
textLabel.TextColor3 = Color3.new(0, 0, 0) -- Black
else
textLabel.TextColor3 = Color3.new(1, 0, 0) -- Red
end
-- Wait a brief moment on even lengths
if i % 2 == 0 then
task.wait()
end
end
end
while true do
-- Spell the text with scale/wrap off
textLabel.TextWrapped = false
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- Spell the text with wrap on
textLabel.TextWrapped = true
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- Spell the text with text scaling on
-- Note: Text turns red (TextFits = false) once text has to be
-- scaled down in order to fit within the UI element.
textLabel.TextScaled = true
-- Note: TextWrapped is enabled implicitly when TextScaled = true
--textLabel.TextWrapped = true
spellTheText()
task.wait(1)
end

TextXAlignment

Đọc Song Song

Mẫu mã

Bố trí văn bản

-- Sao chép cái này vào một LocalScript trong một TextLabel/TextButton/TextBox
local textLabel = script.Parent
local function setAlignment(xAlign, yAlign)
textLabel.TextXAlignment = xAlign
textLabel.TextYAlignment = yAlign
textLabel.Text = xAlign.Name .. " + " .. yAlign.Name
end
while true do
-- Lặp lại trên cả mục Item TextXAlignment và TextYAlignment
for _, yAlign in pairs(Enum.TextYAlignment:GetEnumItems()) do
for _, xAlign in pairs(Enum.TextXAlignment:GetEnumItems()) do
setAlignment(xAlign, yAlign)
task.wait(1)
end
end
end

TextYAlignment

Đọc Song Song

Mẫu mã

Bố trí văn bản

-- Sao chép cái này vào một LocalScript trong một TextLabel/TextButton/TextBox
local textLabel = script.Parent
local function setAlignment(xAlign, yAlign)
textLabel.TextXAlignment = xAlign
textLabel.TextYAlignment = yAlign
textLabel.Text = xAlign.Name .. " + " .. yAlign.Name
end
while true do
-- Lặp lại trên cả mục Item TextXAlignment và TextYAlignment
for _, yAlign in pairs(Enum.TextYAlignment:GetEnumItems()) do
for _, xAlign in pairs(Enum.TextXAlignment:GetEnumItems()) do
setAlignment(xAlign, yAlign)
task.wait(1)
end
end
end

Phương Pháp

CaptureFocus

()

Lợi Nhuận

()

Mẫu mã

TextBox:CaptureFocus

local ContextActionService = game:GetService("ContextActionService")
local ACTION_NAME = "FocusTheTextBox"
local textBox = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_NAME and inputState == Enum.UserInputState.End then
textBox:CaptureFocus()
end
end
ContextActionService:BindAction(ACTION_NAME, handleAction, false, Enum.KeyCode.Q)

IsFocused


Lợi Nhuận

ReleaseFocus

()

Tham Số

submitted: boolean
Giá Trị Mặc Định: false

Lợi Nhuận

()

Mẫu mã

TextBox:ReleaseFocus

local textBox = script.Parent
local function onFocused()
task.wait(5)
textBox:ReleaseFocus()
end
textBox.Focused:Connect(onFocused)

Sự Kiện

FocusLost

Tham Số

enterPressed: boolean
inputThatCausedFocusLoss: InputObject

Mẫu mã

TextBox.FocusLost1

local gui = script.Parent
local textBox = gui.TextBox
local function focusLost(enterPressed)
if enterPressed then
print("Focus was lost because enter was pressed!")
else
print("Focus was lost without enter being pressed")
end
end
textBox.FocusLost:Connect(focusLost)
Tập trung mất

local textBox = script.Parent
local function onFocusLost(enterPressed, inputThatCausedFocusLost)
if enterPressed then
print("Player pressed Enter")
else
print("Player pressed", inputThatCausedFocusLost.KeyCode)
end
end
textBox.FocusLost:Connect(onFocusLost)

Focused


Mẫu mã

Focus

local textBox = script.Parent
local function onFocused()
print("Focused")
end
textBox.Focused:Connect(onFocused)

ReturnPressedFromOnScreenKeyboard