TextBox
*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 CursorPosition và SelectionStart 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.
-- 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
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.
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ỏ.
Xác định phông chữ được sử dụng để render văn bản.
Xác định phông chữ được sử dụng để render văn bản.
Tăng khoảng cách giữa các dòng văn bản trong TextBox .
Số tối đa các grapheme mà TextBox có thể hiển trình diễn.
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.
Đặ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.
Đặ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.
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.
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.
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.
Xác định chuỗi được hiển thị bởi thành phần UI.
Kích thước của văn bản của một thành phần UI trong khấu trừ.
Xác định màu của văn bản được hiển thị.
Xác định xem người dùng có thể thay đổi Text hay không.
Xem có phù hợp với các hạn chế của TextBox không.
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ó.
Xác định chiều cao dòng của văn bản trong khấu trừ.
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).
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.
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
Xác định xem yếu tố UI này có chìm nhập hay không.
Xác định điểm nguồn của một GuiObject , so với kích thước tuyệt đối của nó.
Xác định xem có thay đổi kích thước xảy ra dựa trên nội dung con không.
Xác định màu nền GuiObject .
Xác định độ trong suốt của nền và biên giới GuiObject .
Xác định màu của biên giới GuiObject .
Xác định theo cách nào biên giới GuiObject được xếp theo kích thước của nó.
Xác định chiều rộng pixel của biên giới GuiObject .
Xác định xem con cháu GuiObjects bên ngoài giới hạn của một yếu tố GUI cha có nên hiển thị hay không.
Xác định xem chuột của người chơi đang được nhấn chủ động trên GuiObject hay không.
Xác định liệu GuiButton có thể tương tác với nó hay không, hoặc nếu GuiState của GuiObject đang thay đổi hay không.
Kiểm soát thứ tự sắp xếp của GuiObject khi sử dụng với UIGridStyleLayout .
Bộ GuiObject được chọn khi công tắc gamepad được di chuyển xuống dưới.
Bộ GuiObject được chọn khi chọn gamepad bị di chuyển sang bên trái.
Bộ GuiObject được chọn khi chọn gamepad bị di chuyển sang bên phải.
Bộ GuiObject được chọn khi công tắc gamepad được di chuyển lên trên.
Xác định vị trí pixel và vectơ của GuiObject .
Xác định số độ mà GuiObject được xoay.
Xác định xem liệu GuiObject có thể được chọn bởi gamepad hay không.
Thay thế trang trí lựa chọn mặc định được sử dụng cho gamepad.
Thứ tự của GuiObjects được chọn bởi lựa chọn UI gamepad.
Xác định kích thước pixel và phương trình của GuiObject .
Đặt các trục Size mà GuiObject sẽ dựa trên, so với kích thước của cha của nó.
Một tính chất hỗn hợp của BackgroundTransparency và TextTransparency .
Xác định xem GuiObject và con cháu của nó sẽ được hiển thị hay không.
Xác định thứ tự mà một GuiObject render so với những người khác.
Thuộc Tính
Mô tả vị trí màn hình thực của một thành phần GuiBase2d , trong điểm ảnh.
Mô tả sự xoay màn hình thực sự của một thành phần GuiBase2d , trong độ.
Mô tả kích thước màn hình thực của một thành phần GuiBase2d , bằng像素.
Khi được đặt thành true , bản địa hóa sẽ được áp dụng cho GuiBase2d và con cháu của nó.
Một tham chiếu đến một LocalizationTable để được sử dụng để áp dụng lok hóa tự động cho GuiBase2d và con cháu của nó.
Tùy chỉnh hành vi lựa chọn gamepad theo chiều xuống.
Tùy chỉnh hành vi lựa chọn gamepad theo hướng bên trái.
Tùy chỉnh hành vi lựa chọn gamepad theo hướng bên phải.
Tùy chỉnh hành vi lựa chọn gamepad trong hướng lên.
Cho phép tùy chỉnh chuyển động lựa chọn gamepad.
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.
Buộc khách hàng loại bỏ TextBox.
Phương Pháp
- TweenPosition(endPosition : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : boolean,callback : function):boolean
Di chuyển một GUI mượt mà sang một mới UDim2 .
- TweenSize(endSize : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : boolean,callback : function):boolean
- TweenSizeAndPosition(endSize : UDim2,endPosition : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : boolean,callback : function):boolean
Di chuyển một GUI mượt mà sang một kích cỡ và vị trí mới.
Sự Kiện
Bắt lửa khi khách hàng cho phép tập trung của họ rời khỏi TextBox .
Bắt lửa khi TextBox nhận được sự tập trung.
Sự Kiện
Bị sa thải khi người dùng bắt đầu tương tác thông qua thiết bị Giao diện Con người - Máy tính ( nút chuột xuống, chạm bắt đầu, nút bàn phím xuống, v.v.).
Bị sa thải khi người dùng thay đổi cách họ tương tác thông qua thiết bị Giao diện Con người - Máy tính ( nút chuột xuống, chạm bắt đầu, nút bàn phím xuống, v.v.).
Bị sa thả khi người dùng ngừng tương tác thông qua thiết bị Giao diện Con người - Máy tính ( nút chuột xuống, chạm bắt đầu, nút bàn phím xuống, v.v.).
Bắt lửa khi người dùng di chuyển con trỏ vào một thành phần GUI.
Bắt lửa khi người dùng di chuyển con trỏ ra khỏi một thành phần GUI.
Bắt lửa mỗi khi người dùng di chuyển con trỏ trong khi nó nằm bên trong thành phần GUI.
Bắt lửa khi người dùng cuộn bánh xe chuột trở lại khi chuột ở trên một thành phần GUI.
Bắt lửa khi một người dùng cuộn bánh xoay chuột của họ về phía trước khi chuột ở trên một thành phần GUI.
Bị sa thả khi GuiObject được tập trung vào với lựa chọn Gamepad.
Bị sa thả khi lựa chọn Gamepad ngừng tập trung vào GuiObject.
Bắt lửa khi người chơi bắt đầu, tiếp tục và dừng việc giữ lâu UI.
- TouchPan(touchPositions : Array,totalTranslation : Vector2,velocity : Vector2,state : Enum.UserInputState):RBXScriptSignal
Bắt lửa khi người chơi di chuyển ngón tay trên thành phần UI.
- TouchPinch(touchPositions : Array,scale : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
Bắt lửa khi người chơi thực hiện cử chỉ kéo hoặc kéo bằng hai ngón tay trên thành phần UI.
- TouchRotate(touchPositions : Array,rotation : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
Bắt lửa khi người chơi thực hiện cử chỉ xoay bằng hai ngón tay trên thành phần UI.
Bắt lửa khi người chơi thực hiện cử chỉ vuốt trên thành phần UI.
Bắt lửa khi người chơi thực hiện một cử chỉ vuốt trên thành phần UI.
Sự Kiện
- SelectionChanged(amISelected : boolean,previousSelection : GuiObject,newSelection : GuiObject):RBXScriptSignal
Bắt lửa khi lựa chọn gamepad di chuyển đến, rời hoặc thay đổi trong khu vực kết nối GuiBase2d hoặc bất kỳ con trai nào GuiObjects .
Thuộc Tính
ClearTextOnFocus
ContentText
CursorPosition
Mẫu mã
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)
Font
Mẫu mã
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
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
LineHeight
MaxVisibleGraphemes
MultiLine
OpenTypeFeatures
OpenTypeFeaturesError
PlaceholderColor3
PlaceholderText
RichText
SelectionStart
Mẫu mã
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
Text
Mẫu mã
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
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
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
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
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
Mẫu mã
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
Mẫu mã
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)
-- 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ã 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
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
TextEditable
TextFits
TextScaled
Mẫu mã
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
Mẫu mã
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
Mẫu mã
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
Mẫu mã
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
Mẫu mã
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
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
TextWrapped
Mẫu mã
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
Mẫu mã
-- 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
Mẫu mã
-- 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ã
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)
ReleaseFocus
Tham Số
Lợi Nhuận
Mẫu mã
local textBox = script.Parent
local function onFocused()
task.wait(5)
textBox:ReleaseFocus()
end
textBox.Focused:Connect(onFocused)
Sự Kiện
FocusLost
Tham Số
Mẫu mã
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)
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ã
local textBox = script.Parent
local function onFocused()
print("Focused")
end
textBox.Focused:Connect(onFocused)