TextBox
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
กล่องข้อความ สามารถให้ผู้เล่นใส่ข้อความได้ มันทำงานคล้ายกับ TextButton ยกเว้นว่ากล่องข้อความเดียวสามารถเข้าสู่โฟกัสได้โดยการคลิก แตะ หรือเลือกเกมแพดในขณะที่อยู่ในโฟกัสผู้เล่นสามารถใช้แป้นพิมพ์เพื่อเปลี่ยนคุณสมบัติ Text
- หากไม่มีข้อความ PlaceholderText จะปรากฏ ซึ่งเป็นประโยชน์ในการเตือนผู้เล่นเกี่ยวกับประเภทหรือรูปแบบข้อมูลที่พวกเขาควรป้อน
- โดยค่าเริ่มต้นคุณสมบัติ ClearTextOnFocus จะเปิดใช้งานและมั่นใจว่าไม่มีข้อความที่มีอยู่เมื่อกล่องข้อความได้รับการโฟกัสอาจไม่เป็นที่ต้องการสำหรับข้อความที่ควรจะสามารถแก้ไขได้โดยผู้เล่น
- คุณสมบัติ MultiLine ช่วยให้ผู้เล่นสามารถใส่หลายบรรทัดของข้อความด้วยตัวอักษรแถบใหม่ ( \n )
The ContextActionService เกียรติยศ TextBox keybinds และจะป้องกันการกดปุ่มอัตโนมัติจากการส่งเหตุการณ์การกดปุ่มไปยังแอคชันที่ผูกกับ Class.ContextActionService:BindAction()``Class.UserInputService.InputBegan และกิจกรรมที่เกี่ยวข้องจะยังคงยิงในขณะที่กล่องข้อความอยู่ในโฟกัส
โฟกัสสถานะ
เป็นไปได้ที่จะตรวจพบและเปลี่ยนสถานะโฟกัสของ TextBox:
- คุณสามารถใช้ CaptureFocus เมื่อกล่องโต้ตอบปรากฏขึ้นเพื่อให้ผู้เล่นไม่ต้องคลิกที่กล่องข้อความเมื่อมันพร้อมใช้งาน; คุณสามารถใช้ ContextActionService:BindAction() เพื่อผูกกุญแจบางอย่างเพื่อโฟกัสกล่องข้อความโดยใช้ฟังก์ชันนี้เมื่อกล่องข้อความเข้าสู่โฟกัสแล้ว การเหตุการณ์ Focused จะเกิดขึ้น
- คุณสามารถตรวจสอบได้ว่ากล่องข้อความบางอย่างอยู่ในโฟกัสโดยใช้ IsFocused หรือสามารถใช้ UserInputService:GetFocusedTextBox() เพื่อตรวจสอบว่ากล่องข้อความใดก็ได้อยู่ในโฟกัส
- เมื่อผู้เล่นทำการป้อนข้อความเสร็จแล้ว อีเวนต์ FocusLost จะเกิดขึ้นระบุว่าผู้ใช้กด Enter เพื่อส่งข้อความพร้อมกับ InputObject ที่ทําให้เกิดการสูญเสียการโฟกัสเมื่อใช้แป้นพิมพ์บนหน้าจอบนมือถือและคอนโซล ReturnPressedFromOnScreenKeyboard อาจยิงได้เช่นกัน
- หากมีเรื่องสำคัญเพิ่มเติมเกิดขึ้นในระหว่างการเล่นเกม คุณสามารถ ReleaseFocus ของกล่องข้อความเพื่อให้การป้อนข้อมูลของแป้นพิมพ์ของผู้เล่นกลับไปยังเกมของคุณ
การแก้ไขข้อความ
กล่องข้อความสนับสนุนการเลือกข้อความผ่านคุณสมบัติ CursorPosition และ SelectionStart ของมันโดยใช้ GetPropertyChangedSignal คุณสามารถตรวจพบเมื่อมีการเปลี่ยนแปลงการเลือกนอกจากนี้ยังเป็นไปได้สำหรับผู้เล่นที่จะคัดลอกและวางข้อความภายใน TextBox เพื่อเปิดใช้งานการสนับสนุนบัฟเฟอร์พื้นฐาน
แจ้งการกรองข้อความ เกมที่ช่วยให้การสื่อสารระหว่างผู้เล่นเป็นไปได้โดยใช้ข้อความ เช่น การแชทแบบกําหนดเองหรือแท็กชื่อ ต้องกรองข้อความดังกล่าวอย่างถูกต้องโดยใช้ TextService:FilterStringAsync() หรือ แอ็คชัน
ตัวอย่างโค้ด
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)
สรุป
คุณสมบัติ
กำหนดว่าการคลิกที่กล่องข้อความจะล้างคุณสมบัติ TextBox.Text ของมันหรือไม่
กำหนดการเลื่อนของเคอร์เซอร์ข้อความในไบต์หรือ -1 หากไม่มีเคอร์เซอร์
กำหนดฟอนต์ที่ใช้เพื่อแสดงข้อความ
กำหนดฟอนต์ที่ใช้เพื่อแสดงข้อความ
ขยายช่องว่างระหว่างบรรทัดข้อความใน TextBox
จํานวนสูงสุดของ graphemes ที่ TextBox สามารถแสดงได้
เมื่อตั้งค่าเป็นจริง ข้อความภายในกล่องข้อความสามารถย้ายไปยังหลายบรรทัดได้ ซึ่งยังช่วยให้ผู้เล่นสามารถใช้ปุ่ม Enter เพื่อย้ายไปยังบรรทัดใหม่ได้
ตั้งสีข้อความที่ใช้เมื่อยังไม่มีข้อความถูกป้อนลงใน TextBox
ตั้งข้อความที่จะแสดงเมื่อยังไม่มีข้อความใดๆ ถูกป้อนลงใน TextBox
กำหนดว่า TextBox แสดงสตริง TextBox.Text โดยใช้รูปแบบการจัดรูปแบบข้อความที่มีรายละเอียดหรือไม่
กำหนดตำแหน่งเริ่มต้นของการเลือกข้อความหรือ -1 หากไม่มีข้อความที่เลือก
หากตั้งค่าเป็นจริง การใส่ข้อมูลเดิมสู่แพลตฟอร์มจะใช้แทนแป้นพิมพ์ที่สร้างไว้ล่วงหน้าของ Roblox
กำหนดสตริงที่แสดงโดยองค์ประกอบ UI
ขนาดของข้อความขององค์ประกอบ UI ในออฟเซ็ต
กำหนดสีของข้อความที่เรนเดอร์ได้รับ
กำหนดว่าผู้ใช้สามารถเปลี่ยน Text ได้หรือไม่
ว่าข้อความสอดคล้องกับข้อจำกัดของ TextBox หรือไม่
เปลี่ยนว่าข้อความจะถูกปรับขนาดเพื่อให้พอดีกับวัตถุ GUI ที่แสดงผล
กำหนดความสูงของเส้นข้อความในออฟเซ็ต
กำหนดสีของเส้นข้อความ (เส้นขอบ)
กำหนดความโปร่งใสของเส้นข้อความ (เส้นขอบ)
กำหนดความโปร่งใสของข้อความที่เรนเดอร์ได้รับ
ควบคุมการตัดส่วนของข้อความที่แสดงในกล่องข้อความนี้
กำหนดว่าข้อความจะห่อหุ้มไปยังหลายบรรทัดภายในพื้นที่องค์ประกอบ GuiObject โดยตัดข้อความที่เกิน
กำหนดการจัดตำแหน่งแนวนอนของข้อความที่เรนเดอร์ได้รับ
กำหนดการจัดตำแหน่งแนวตั้งของข้อความที่เรนเดอร์ได้รับ
กำหนดว่าองค์ประกอบ UI นี้จะรับอินพุตหรือไม่
กำหนดจุดต้นกำเนิดของ GuiObject เมื่อเทียบกับขนาดสัมบูรณ์ของมัน
กำหนดว่าการปรับขนาดจะเกิดขึ้นตามเนื้อหาลูกหรือไม่
กำหนดสีพื้นหลัง GuiObject
กำหนดความโปร่งใสของพื้นหลังและขอบ GuiObject
กำหนดสีขอบ GuiObject ได้
กำหนดวิธีที่ขอบ GuiObject ถูกวางในลักษณะที่เกี่ยวข้องกับขนาด
กำหนดความกว้างของพิกเซลของขอบ GuiObject
กำหนดว่าถ้าลูกหลาน GuiObjects นอกขอบเขตขององค์ประกอบ GUI พ่อจะต้องแสดงผลหรือไม่
กำหนดว่าเมาส์ของผู้เล่นกดอยู่อย่างใดอย่างหนึ่งบน GuiObject หรือไม่
กำหนดว่า GuiButton สามารถโต้ตอบได้หรือไม่ หรือถ้า GuiState ของ GuiObject กำลังเปลี่ยนแปลงหรือไม่
ควบคุมลำดับการจัดเรียงของ GuiObject เมื่อใช้กับ UIGridStyleLayout
ตั้งค่า GuiObject ซึ่งจะถูกเลือกเมื่อตัวเลือกเกมแพดถูกเลื่อนลง
ตั้งค่า GuiObject ซึ่งจะถูกเลือกเมื่อตัวเลือกเกมแพดถูกย้ายไปทางซ้าย
ตั้งค่า GuiObject ซึ่งจะถูกเลือกเมื่อตัวเลือกเกมแพดถูกย้ายไปทางขวา
ตั้งค่า GuiObject ซึ่งจะถูกเลือกเมื่อตัวเลือกเกมแพดถูกย้ายขึ้น
กำหนดพิกเซลและตําแหน่งเวกเตอร์ของ GuiObject
กำหนดจำนวนองศาที่ GuiObject หมุนไป
ตรวจสอบว่า GuiObject สามารถเลือกได้โดยคอนโซลเกมหรือไม่
แทนที่การเลือกเริ่มต้นที่ใช้สำหรับเกมแพด
ลำดับของ GuiObjects ที่เลือกโดยการเลือก UI ของเกมแพด
กำหนดพิกเซลและขนาดสเกลาร์ของ GuiObject
ตั้งแกน Size ที่ GuiObject จะอ้างอิงจากขนาดของพ่อของมัน เมื่อเทียบกับขนาดของพ่อ
คุณสมบัติผสมของ BackgroundTransparency และ TextTransparency .
กำหนดว่า GuiObject และบรรดาลูกหลานจะถูกแสดงหรือไม่
กำหนดลำดับที่เรนเดอร์ GuiObject จะแสดงเป็นค่าสัมพันธ์กับคนอื่น
อธิบายตำแหน่งจอแสดงจริงขององค์ประกอบ GuiBase2d ในพิกเซล
อธิบายการหมุนหน้าจอที่เกิดขึ้นจริงขององค์ประกอบ GuiBase2d ในองศา
อธิบายขนาดหน้าจอที่เป็นจริงขององค์ประกอบ GuiBase2d ในพิกเซล
เมื่อตั้งค่าเป็น true การแปลท้องถิ่นจะถูกใช้กับ GuiBase2d และบรรดาลูกหลานของมัน
การอ้างอิงถึง LocalizationTable ที่จะใช้เพื่อใช้การแปลงภาษาท้องถิ่นอัตโนมัติกับ GuiBase2d และบรรดาลูกหลานของมัน
ปรับพฤติกรรมการเลือกเกมแพดในทิศทางลง
ปรับพฤติกรรมการเลือกเกมแพดในทิศทางซ้าย
ปรับพฤติกรรมการเลือก gamepad ในทิศทางที่ถูกต้อง
ปรับพฤติกรรมการเลือกเกมแพดในทิศทางขึ้น
อนุญาตให้ปรับแต่งการเคลื่อนไหวการเลือกเกมแพดได้
วิธีการ
บังคับให้ไคลเอนต์โฟกัสที่ TextBox
ส่งคืนจริงหากกล่องข้อความได้รับการโฟกัส หรือเท็จหากไม่ได้
บังคับให้ไคลเอนต์ละเลย TextBox
- TweenPosition(endPosition : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : boolean,callback : function):boolean
เคลื่อน GUI ไปยังใหม่ 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
เคลื่อน GUI ไปยังขนาดและตำแหน่งใหม่อย่างราบรื่น
อีเวนต์
อีเวนต์รับทอดมาจากGuiObjectยิงเมื่อผู้ใช้เริ่มโต้ตอบผ่านอุปกรณ์อินเทอร์เฟซมนุษย์-คอมพิวเตอร์ (ปุ่มเมาส์ลง, แตะเริ่ม, ปุ่มแป้นพิมพ์ลง, เป็นต้น)
ยิงเมื่อผู้ใช้เปลี่ยนวิธีการโต้ตอบผ่านอุปกรณ์อินเทอร์เฟซมนุษย์-คอมพิวเตอร์ (ปุ่มเมาส์ลง, แตะเริ่ม, ปุ่มแป้นพิมพ์ลง, เป็นต้น)
ยิงเมื่อผู้ใช้หยุดโต้ตอบผ่านอุปกรณ์อินเทอร์เฟซมนุษย์-คอมพิวเตอร์ (ปุ่มเมาส์ลง, แตะเริ่ม, ปุ่มแป้นพิมพ์ลง, เป็นต้น)
เกิดไฟไหม้เมื่อผู้ใช้ย้ายเมาส์ไปยังองค์ประกอบ GUI
เกิดไฟไหม้เมื่อผู้ใช้ย้ายเมาส์ออกจากองค์ประกอบ GUI
เกิดไฟไหม้เมื่อผู้ใช้ย้ายเมาส์ในขณะที่อยู่ภายในองค์ประกอบ GUI ใดๆ
เกิดไฟไหม้เมื่อผู้ใช้เลื่อนล้อเมาส์กลับเมื่อเมาส์อยู่เหนือองค์ประกอบ GUI
เกิดไฟไหม้เมื่อผู้ใช้เลื่อนล้อเมาส์ไปข้างหน้าเมื่อเมาส์อยู่เหนือองค์ประกอบ GUI
ยิงเมื่อ GuiObject ได้รับการโฟกัสด้วยตัวเลือก Gamepad
ยิงเมื่อตัวเลือก Gamepad หยุดโฟกัสกับ GuiObject
เกิดไฟไหม้เมื่อผู้เล่นเริ่มต้น ดำเนินการต่อ และหยุดการกดค้างไว้นานบนองค์ประกอบ UI
- TouchPan(touchPositions : Array,totalTranslation : Vector2,velocity : Vector2,state : Enum.UserInputState):RBXScriptSignal
เกิดไฟไหม้เมื่อผู้เล่นย้ายนิ้วบนองค์ประกอบ UI
- TouchPinch(touchPositions : Array,scale : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
เกิดไฟไหม้เมื่อผู้เล่นดําเนินการสับหรือดึงด้วยนิ้วสองนิ้วบนองค์ประกอบ UI
- TouchRotate(touchPositions : Array,rotation : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
เกิดไฟไหม้เมื่อผู้เล่นดําเนินการทําท่าหมุนโดยใช้นิ้วสองนิ้วบนองค์ประกอบ UI
เกิดไฟไหม้เมื่อผู้เล่นดําเนินการสับข้อมูลบนองค์ประกอบ UI
เกิดไฟไหม้เมื่อผู้เล่นดําเนินการสัมผัสท่าทางบนองค์ประกอบ UI
- SelectionChanged(amISelected : boolean,previousSelection : GuiObject,newSelection : GuiObject):RBXScriptSignal
ไฟเมื่อการเลือกเกมแพดย้ายไปที่ ออก หรือเปลี่ยนภายในเชื่อมต่อ GuiBase2d หรือทายาทใดๆ GuiObjects
คุณสมบัติ
ContentText
CursorPosition
คุณสมบัตินี้กำหนดค่าเบี่ยงของเคอร์เซอร์ข้อความในไบต์หรือ -1 หาก TextBox ไม่ได้ถูกแก้ไขอยู่ในปัจจุบันค่าของ 1 แทนที่ตำแหน่งก่อนบายต์แรกในคุณสมบัติ Textเมื่อใช้ร่วมกับคุณสมบัติ SelectionStart คุณสามารถรับและตั้งข้อความที่เลือกภายใน TextBox ได้
โปรดทราบว่าหน่วยของคุณสมบัตินี้คือ ไบต์ และตัวละครยูนิโค้ดหลายตัวเช่นอีโมจิมีความยาวมากกว่า 1 อินสแตนซ์หากผู้เล่นพิมพ์ "สวัสดี👋" ("สวัสดี" ทันทีด้วยสัญลักษณ์มือโบกมือ) ตำแหน่งเคอร์เซอร์จะเป็น 10 ไม่ใช่ 7 เนื่องจากอีโมจิใช้ 4 ไบต์
ตัวอย่างโค้ด
This code sample demonstrates reading the current selection of a TextBox using CursorPosition() and SelectionStart().
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
คุณสมบัติฟอนต์เลือกหนึ่งในหลายรายการที่กําหนดไว้ล่วงหน้า fonts ซึ่งองค์ประกอบ UI จะแสดงข้อความบางฟอนต์มีตัวหนา, ตัวอักษรอิตาลิค และ/หรือตัวเลือกแสง (เนื่องจากไม่มีคุณสมบัติน้ำหนักฟอนต์หรือสไตล์ฟอนต์)
ยกเว้นฟอนต์ "มรดก" แต่ละฟอนต์จะแสดงข้อความด้วยความสูงของเส้นเท่ากับคุณสมบัติ TextBox.TextSizeแบบอักษร "รหัส" เป็นแบบอักษรพื้นที่เดียวเท่านั้นมันมีคุณสมบัติที่ไม่ซ้ำกันที่แต่ละตัวละครมีอัตราส่วนความกว้างและความสูงที่ถูกต้องเท่ากับ 1:2ความกว้างของแต่ละตัวอักษรคือประมาณครึ่งหนึ่งของคุณสมบัติ TextBox.TextSize
คุณสมบัตินี้จะถูกซิงค์กับคุณสมบัติ TextBox.FontFace เมื่อตั้งค่าฟอนต์ ฟอนต์เฟสจะถูกตั้งเป็น Font.fromEnum(value)
ตัวอย่างโค้ด
This code sample sets a parent TextLabel's Font and Text properties to all the different fonts available.
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
This code sample renders a list of all the available fonts.
local frame = script.Parent
-- Create a TextLabel displaying each font
for _, font in pairs(Enum.Font:GetEnumItems()) do
local textLabel = Instance.new("TextLabel")
textLabel.Name = font.Name
-- Set the text properties
textLabel.Text = font.Name
textLabel.Font = font
-- Some rendering properties
textLabel.TextSize = 24
textLabel.TextXAlignment = Enum.TextXAlignment.Left
-- Size the frame equal to the height of the text
textLabel.Size = UDim2.new(1, 0, 0, textLabel.TextSize)
-- Add to the parent frame
textLabel.Parent = frame
end
-- Layout the frames in a list (if they aren't already)
if not frame:FindFirstChildOfClass("UIListLayout") then
local uiListLayout = Instance.new("UIListLayout")
uiListLayout.Parent = frame
end
FontFace
คุณสมบัติ FontFace คล้ายกับคุณสมบัติ Font แต่สามารถตั้งค่าฟอนต์ที่ไม่มีอยู่ในรายการฟอนต์ได้
คุณสมบัตินี้จะซิงค์กับคุณสมบัติ TextBox.Font อย่างต่อเนื่องเมื่อตั้งค่า FontFace ตัวอักษรจะถูกตั้งค่าเป็นค่า enum ที่ตรงกันหรือเป็น Enum.Font.Unknown หากไม่มีการจับคู่
LineHeight
ควบคุมความสูงของบรรทัดโดยเป็นหลายเท่าของขนาดพื้นที่รูปแบบของฟอนต์โดยการปรับขนาดช่องว่างระหว่างบรรทัดข้อความใน TextBoxค่าที่ถูกต้องอยู่ระหว่าง 1.0 ถึง 3.0 โดยค่าเริ่มต้นคือ 1.0
MaxVisibleGraphemes
คุณสมบัตินี้ควบคุมจํานวนสูงสุดของ graphemes (หรือหน่วยข้อความ) ที่แสดงบน TextBox ไม่ว่าจะแสดง TextBox.PlaceholderText หรือ TextBox.Text ก็ตาม
การเปลี่ยนคุณสมบัติจะไม่เปลี่ยนตำแหน่งหรือขนาดของ graphemes ที่มองเห็นได้ - เลย์เอาต์จะถูกคำนวณเหมือนว่า graphemes ทั้งหมดสามารถมองเห็นได้
การตั้งค่าคุณสมบัติเป็น -1 จะปิดการจำกัดและแสดงความสมบูรณ์ทั้งหมดของ TextBox.Text
MultiLine
เมื่อตั้งค่าเป็นจริง ข้อความภายในกล่องข้อความสามารถย้ายไปยังหลายบรรทัดได้ ซึ่งยังช่วยให้ผู้เล่นสามารถใช้ปุ่ม Enter เพื่อย้ายไปยังบรรทัดใหม่ได้
OpenTypeFeatures
OpenTypeFeaturesError
RichText
คุณสมบัตินี้กำหนดว่า TextBox แสดงสตริง TextBox.Text โดยใช้รูปแบบการจัดรูปข้อความที่มีรายละเอียดหรือไม่ข้อความที่มีรูปแบบรวยใช้แท็กเครื่องหมายง่ายๆ เพื่อสร้างส่วนของสตริงในแบบพิมพ์เขียว, อิตาลิค, สีเฉพาะ, และอื่นๆ
เพื่อใช้ข้อความที่มีรายละเอียด เพียงแค่รวมแท็กการจัดรูปแบบในสตริง TextBox.Text
โปรดทราบว่าเมื่อ TextBox มีคุณสมบัตินี้เปิดใช้งานและกล่องได้รับการโฟกัสแล้ว ผู้ใช้จะสามารถแก้ไขและโต้ตอบกับสตริง XML ที่สมบูรณ์ได้ รวมถึงแท็กรูปแบบทั้งหมดเมื่อสูญเสียการโฟกัสแล้ว ข้อความจะแยกและแสดงแท็กเป็นข้อความที่มีรายละเอียดอย่างอัตโนมัติ
SelectionStart
กำหนดตำแหน่งเริ่มต้นของการเลือกข้อความหรือ -1 ถ้า TextBox ไม่มีช่วงข้อความที่เลือกหากค่าเป็น -1 หรือเทียบเท่ากับ CursorPosition ไม่มีช่วงข้อความที่เลือกคุณสมบัตินี้ใช้โลจิสติกการจัดตำแหน่งเดียวกับ CursorPositionการเลือกเริ่มต้นจะมากกว่าตำแหน่งเคอร์เซอร์หากเคอร์เซอร์อยู่ตรงต้นของการเลือกและน้อยกว่าตำแหน่งเคอร์เซอร์หากเคอร์เซอร์อยู่ที่ด้านท้าย
ตัวอย่างโค้ด
This code sample demonstrates reading the current selection of a TextBox using CursorPosition() and SelectionStart().
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
หากตั้งค่าเป็นจริง การใส่ข้อมูลเดิมสู่แพลตฟอร์มจะใช้แทนแป้นพิมพ์ที่สร้างไว้ล่วงหน้าของ Roblox
Text
คุณสมบัติข้อความกำหนดเนื้อหาที่แสดงโดยองค์ประกอบ UIคุณสมบัติทางภาพของสตริงที่แสดงบนหน้าจอถูกกำหนดโดย TextBox.TextColor3 , TextBox.TextTransparency , TextBox.TextSize , TextBox.Font , TextBox.TextScaled , TextBox.TextWrapped , TextBox.TextXAlignment และ TextBox.TextYAlignment .
เป็นไปได้ที่จะเรนเดอร์อีโมจิ (ตัวอย่างเช่น 😃) และสัญลักษณ์อื่นๆสัญลักษณ์พิเศษเหล่านี้ไม่ได้รับผลกระทบจากคุณสมบัติ TextBox.TextColor3สิ่งเหล่านี้สามารถวางลงในวัตถุ Script และ LocalScript รวมทั้งฟิลด์ภายในหน้าต่างคุณสมบัติ
คุณสมบัตินี้อาจมีตัวอักษรบรรทัดใหม่ แต่ไม่สามารถพิมพ์ตัวอักษรบรรทัดใหม่ภายในหน้าต่างคุณสมบัติได้เช่นเดียวกันคุณสมบัตินี้อาจมีตัวละครแท็บ แต่จะแสดงเป็นพื้นที่แทน
ตัวอย่างโค้ด
This code sample creates a fading banner for a TextLabel. It fades text out, chooses a random string (avoiding repetition), and fades back in.
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
This code sample repeatedly tweens a TextLabel's TextSize from 5 to 100 and fades out the text as it grows in size.
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
This code sample renders a list of all the available fonts.
local frame = script.Parent
-- Create a TextLabel displaying each font
for _, font in pairs(Enum.Font:GetEnumItems()) do
local textLabel = Instance.new("TextLabel")
textLabel.Name = font.Name
-- Set the text properties
textLabel.Text = font.Name
textLabel.Font = font
-- Some rendering properties
textLabel.TextSize = 24
textLabel.TextXAlignment = Enum.TextXAlignment.Left
-- Size the frame equal to the height of the text
textLabel.Size = UDim2.new(1, 0, 0, textLabel.TextSize)
-- Add to the parent frame
textLabel.Parent = frame
end
-- Layout the frames in a list (if they aren't already)
if not frame:FindFirstChildOfClass("UIListLayout") then
local uiListLayout = Instance.new("UIListLayout")
uiListLayout.Parent = frame
end
This code sample demonstrates TextWrap by spelling out a long chunk of text progressively. If the text doesn't fit, it turns a different color.
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
This code sample demonstrates emoji rendering using the Text property.
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
คุณสมบัติอ่านได้เท็กซ์ขอบเขตสะท้อนขนาดสัมบูรณ์ของข้อความที่เรนเดอร์แสดงในออฟเซ็ตคือถ้าคุณพยายามที่จะใส่ข้อความลงในรูปสี่เหลี่ยม คุณสมบัตินี้จะสะท้อนขนาดขั้นต่ำของรูปสี่เหลี่ยมที่คุณต้องการเพื่อให้ข้อความพอดี
โดยใช้ TextService:GetTextSize() , คุณสามารถคาดการณ์สิ่งที่ TextBounds จะเป็นบนแท็กข้อความที่ให้สตริง, TextBox.Font , TextBox.TextSize และขนาดกรอบ
ตัวอย่างโค้ด
This code sample dynamically resizes a TextLabel, TextButton or TextBox to match the size of its TextBounds. Try changing the minimum width/height and pasting into a LocalScript in a TextBox.
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
คุณสมบัตินี้กำหนดสีของข้อความทั้งหมดที่แสดงโดยธาตุ GuiObjectคุณสมบัตินี้ร่วมกับ TextBox.Font , TextBox.TextSize และ TextBox.TextTransparency จะกำหนดคุณสมบัติทางสายตาของข้อความข้อความจะถูกแสดงหลังจากที่ขอบข้อความ ( TextBox.TextStrokeColor3 )
สำคัญที่ข้อความจะอ่านได้ง่ายโดยผู้เล่น! ตรวจสอบให้แน่ใจว่าเลือกสีที่มีความอิ่มต่ำถึงปานกลาง เช่น สีขาว สีเทา หรือสีดำตรวจสอบให้แน่ใจว่าสีข้อความของคุณมีความแตกต่างโดย TextBox.BackgroundColor3 ขององค์ประกอบ UIหากองค์ประกอบมีพื้นหลังโปร่งใส ลองใช้สีดำ TextBox.TextStrokeColor3 เพื่อช่วยเพิ่มความแตกต่างระหว่างข้อความกับโลก 3D ที่อยู่เบื้องหลัง
ตัวอย่างโค้ด
This code sample, when placed within a TextBox, will turn the text color red if the typed string contains no vowels (A, E, I, O or U).
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)
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)
This code sample makes a TextLabel or TextButton count backwards from 10, setting the text color as it does so.
-- Place this code in a LocalScript within a TextLabel/TextButton
local textLabel = script.Parent
-- Some colors we'll use with TextColor3
local colorNormal = Color3.new(0, 0, 0) -- black
local colorSoon = Color3.new(1, 0.5, 0.5) -- red
local colorDone = Color3.new(0.5, 1, 0.5) -- green
-- Loop infinitely
while true do
-- Count backwards from 10 to 1
for i = 10, 1, -1 do
-- Set the text
textLabel.Text = "Time: " .. i
-- Set the color based on how much time is left
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
This code sample mirrors the contents of a StringValue into a TextLabel, updating and setting the color of the text as it changes.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Place a StringValue called "GameState" in the ReplicatedStorage
local vGameState = ReplicatedStorage:WaitForChild("GameState")
-- Place this code in a TextLabel
local textLabel = script.Parent
-- Some colors we'll use with TextColor3
local colorNormal = Color3.new(0, 0, 0) -- black
local colorCountdown = Color3.new(1, 0.5, 0) -- orange
local colorRound = Color3.new(0.25, 0.25, 1) -- blue
-- We'll run this function to update the TextLabel as the state of the
-- game changes.
local function update()
-- Update the text
textLabel.Text = "State: " .. vGameState.Value
-- Set the color of the text based on the current game state
if vGameState.Value == "Countdown" then
textLabel.TextColor3 = colorCountdown
elseif vGameState.Value == "Round" then
textLabel.TextColor3 = colorRound
else
textLabel.TextColor3 = colorNormal
end
end
-- Pattern: update once when we start and also when vGameState changes
-- We should always see the most updated GameState.
update()
vGameState.Changed:Connect(update)
TextDirection
TextEditable
ข้อความที่สามารถแก้ไขได้ กำหนดว่าผู้ใช้สามารถเปลี่ยน Text ผ่านอินพุตได้หรือไม่ขอแนะนำให้ปิดใช้งาน ClearTextOnFocus เมื่อคุณสมบัตินี้ถูกปิดใช้งาน มิฉะนั้นข้อความอาจถูกล้างในขณะโฟกัสคุณสมบัตินี้มีประโยชน์ในการสร้าง TextBox ที่อ่านได้ซึ่งเนื้อหาสามารถคัดลอกได้ในเกม
TextScaled
แทนที่จะใช้ TextScaled เราขอแนะนำให้คุณพิจารณาใช้ AutomaticSize วิธีใหม่ในการปรับขนาด UI อย่างไดนามิกที่จะให้ผลลัพธ์ทางสายตาที่ดีที่สุดที่เป็นไปได้
คุณสมบัติ TextScaled ตัดสินว่าข้อความจะถูกขยายเพื่อให้เติมพื้นที่ทั้งหมดขององค์ประกอบ UI หรือไม่เมื่อเปิดใช้งานแล้ว TextBox.TextSize จะถูกเพิกเฉยและ TextBox.TextWrapped จะเปิดใช้งานอัตโนมัติคุณสมบัตินี้มีประโยชน์สำหรับองค์ประกอบ UI สําหรับการแสดงข้อความภายใน BillboardGuis
เมื่อคุณสมบัตินี้ใช้สำหรับ UI ในพื้นที่หน้าจอ อาจเป็นที่ต้องการที่จะใช้ UITextSizeConstraint เพื่อจำกัดขอบเขตของขนาดข้อความที่เป็นไปได้
ขนาดข้อความที่ปรับและขนาดอัตโนมัติ
ขอแนะนำให้นักพัฒนาหลีกเลี่ยงการใช้ TextScaled และปรับ UI เพื่อใช้ประโยชน์จากคุณสมบัติ AutomaticSize แทนนี่คือความแตกต่างหลักระหว่างสองคุณสมบัติ:
- ขนาดข้อความขยายขนาดเนื้อหา (ข้อความ) เพื่อรองรับ UI โดยไม่ต้องพิจารณาอย่างระมัดระวัง ข้อความบางส่วนอาจกลายเป็นข้อความที่อ่านไม่ออกหากมีขนาดเล็กเกินไป
- ขนาดอัตโนมัติขยาย UI เพื่อรองรับเนื้อหา
ด้วยขนาดอัตโนมัติคุณสามารถปรับ UI ของคุณเพื่อรองรับเนื้อหา (ข้อความ) ในขณะที่รักษาขนาดฟอนต์คงที่ไว้สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการใช้การขยายขนาดอัตโนมัติ โปรดดูบทความ UI ขนาดอัตโนมัติ
เราแนะนำให้คุณไม่ใช้ทั้ง TextScaled และ AutomaticSize บนวัตถุ UI เดียวกัน หากคุณใช้ทั้งสองคุณสมบัติ:
- ขนาดอัตโนมัติกำหนดจำนวนพื้นที่สูงสุดที่ GuiObject สามารถใช้ได้ (ในกรณีนี้คือข้อความ)
- การขยายข้อความใช้พื้นที่ที่มีอยู่ที่กำหนดโดย AutomaticSize เพื่อขยายขนาดฟอนต์ให้พอดีกับพื้นที่ที่มีอยู่ซึ่งจะขยายไปถึงขนาดฟอนต์สูงสุด (100) หากไม่มีข้อจำกัดด้านขนาด
- ผลลัพธ์สุดท้ายจะเป็น: ข้อความจะไปที่ขนาดฟอนต์ 100 และวัตถุ UI จะขยายเพื่อให้พอดีกับข้อความนั้น
การใช้ทั้ง AutomaticSize และ TextScaled ในเวลาเดียวกันอาจทำให้เกิดความแตกต่างในการปรับขนาดอย่างมากกว่าเมื่อ AutomaticSize ปิด
ตัวอย่างโค้ด
This code sample demonstrates TextWrap by spelling out a long chunk of text progressively. If the text doesn't fit, it turns a different color.
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
คุณสมบัติ TextSize กำหนดความสูงในการเลื่อนของบรรทัดข้อความหนึ่งบรรทัดที่เรนเดอร์แสดงหน่วยอยู่ในออฟเซ็ตไม่ใช่จุด (ซึ่งใช้ในโปรแกรมแก้ไขเอกสารส่วนใหญ่)ฟอนต์ "มรดก" ไม่มีคุณสมบัตินี้
ตัวอย่างโค้ด
This code sample repeatedly tweens a TextLabel's TextSize from 5 to 100 and fades out the text as it grows in size.
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
คุณสมบัติ TextStrokeColor3 ตั้งสีของสีขอบหรือขอบของข้อความที่เรนเดอร์แสดงออกคุณสมบัตินี้และ TextBox.TextStrokeTransparency กำหนดคุณสมบัติทางสายตาของเส้นข้อความ
เส้นขอบข้อความจะถูกแสดงก่อนข้อความปกติและเป็นเพียง 4 การแสดงของข้อความเดียวกันใน +/- 1 พิกเซลในแต่ละทิศทางการเรนเดอร์ขอบข้อความทำงานอย่างอิสระและเหมือนกันกับ TextBox.TextColor3 และ TextBox.TextTransparency
ตัวอย่างโค้ด
This code sample oscillates a TextLabel's TextStrokeTransparency so that it blinks the highlight of a text.
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
คุณสมบัติ TextStrokeTransparency ตั้งความโปร่งใสของสีขอบหรือสีขอบของข้อความที่เรนเดอร์แสดงออกคุณสมบัตินี้และ TextBox.TextStrokeColor3 กำหนดคุณสมบัติทางสายตาของเส้นข้อความ
เส้นขอบข้อความจะถูกแสดงก่อนข้อความปกติและเป็นเพียง 4 การแสดงของข้อความเดียวกันใน +/- 1 พิกเซลในแต่ละทิศทางการเรนเดอร์ขอบข้อความทำงานอย่างอิสระและเหมือนกันกับ TextBox.TextColor3 และ TextBox.TextTransparencyเนื่องจากเส้นขอบข้อความเป็นเพียงการเรนเดอร์หลายครั้งของความโปร่งใสเดียวกัน คุณสมบัตินี้จึงเป็นอัตราส่วนเพิ่มขึ้นสี่เท่า (เช่นความโปร่งใสของสีข้อความ 0.5 ปรากฏเหมือนกับความโปร่งใสของข้อความ 0.0625 หรือ 0.5^4)ดังนั้นจึงแนะนำให้ตั้งค่า TextStrokeTransparency เป็นค่าในช่วง 0.75 ถึง 1 เพื่อให้ได้ผลที่ละเอียดอ่อนมากขึ้น
ตัวอย่างโค้ด
This code sample oscillates a TextLabel's TextStrokeTransparency so that it blinks the highlight of a text.
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
คุณสมบัติ TextColor3 กำหนดความโปร่งใสของข้อความทั้งหมดที่แสดงโดยองค์ประกอบ UIคุณสมบัตินี้ร่วมกับ TextBox.Font , TextBox.TextSize และ TextBox.TextColor3 จะกำหนดคุณสมบัติทางสายตาของข้อความข้อความจะถูกแสดงหลังจากที่ขอบข้อความ ( TextBox.TextStrokeTransparency )
การจางหายของข้อความในการใช้ลูป for เป็นวิธีที่ยอดเยี่ยมในการดึงความสนใจของผู้เล่นไปที่ข้อความที่ปรากฏบนหน้าจอ
-- Count backwards from 1 to 0, decrementing by 0.1for i = 1, 0, -0.1 dotextLabel.TextTransparency = itask.wait(0.1)end
ตัวอย่างโค้ด
This code sample creates a fading banner for a TextLabel. It fades text out, chooses a random string (avoiding repetition), and fades back in.
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
This code sample repeatedly tweens a TextLabel's TextSize from 5 to 100 and fades out the text as it grows in size.
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
TextWrapped
เมื่อเปิดใช้งานแล้ว คุณสมบัตินี้จะแสดงข้อความบนหลายบรรทัดภายในพื้นที่ขององค์ประกอบ TextBox เพื่อให้ TextBox.TextBounds ไม่เกิน GuiBase2d.AbsoluteSize ขององค์ประกอบ GUI
สิ่งนี้จะบรรลุได้โดยการแยกเส้นข้อความยาวเป็นหลายบรรทัดการหยุดชะงักบรรทัดจะชอบพื้นที่ว่าง; หากคําที่ยาวไม่ถูกหยุดมีขนาดเกินความกว้างขององค์ประกอบนั้น คํานั้นจะถูกแบ่งออกเป็นหลายบรรทัด
หากการละเมิดบรรทัดต่อไปจะทำให้ความสูงแนวตั้งของข้อความ (ส่วนประกอบ Y ของ TextBox.TextBounds) เกินความสูงแนวตั้งขององค์ประกอบ (ส่วนประกอบ Y ของ GuiBase2d.AbsoluteSize) แล้วบรรทัดนั้นจะไม่ถูกแสดงออกเลย
ตัวอย่างโค้ด
This code sample demonstrates TextWrap by spelling out a long chunk of text progressively. If the text doesn't fit, it turns a different color.
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
การจัดตำแหน่งข้อความจะกำหนดการจัดตำแหน่งแนวนอน (แกน X) ของข้อความที่แสดงภายในพื้นที่ขององค์ประกอบ UIมันทำงานคล้ายกับคุณสมบัติการจัดเรียงข้อความของ CSS ด้วยค่าซ้าย ขวา และกลาง (ไม่มีตัวเลือกการจัดเรียง)สำหรับซ้ายและขวาข้อความจะถูกแสดงให้เหมือนว่าขอบข้อความด้านซ้าย/ขวาเพียงแค่สัมผัสขอบของรูปสี่เหลี่ยมขององค์ประกอบ UIสำหรับเซ็นเตอร์แต่ละบรรทัดข้อความจะถูกจัดให้อยู่ตรงกลางของรูปสี่เหลี่ยมขององค์ประกอบ UI มากที่สุด
คุณสมบัตินี้ใช้ร่วมกับ TextBox.TextYAlignment เพื่อกำหนดการจัดเรียงข้อความอย่างเต็มที่ในทั้งสองแกนคุณสมบัตินี้จะไม่ส่งผลต่อคุณสมบัติอ่านได้เฉพาะ TextBox.TextBounds และ TextBox.TextFits
ตัวอย่างโค้ด
This code sample shows all the different text alignment combinations by iterating over each enum item. It is meant to be placed within a TextLabel, TextButton or TextBox.
-- Paste this in a LocalScript within a 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
-- Iterate over both TextXAlignment and TextYAlignment enum items
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
การจัดตำแหน่งข้อความจะกำหนดการจัดตำแหน่งแนวตั้ง (แกน Y) ของข้อความที่แสดงภายในพื้นที่ขององค์ประกอบ UIสำหรับด้านบนและด้านล่าง ข้อความจะถูกแสดงให้เหมือนว่าขอบเขตข้อความด้านบน/ด้านล่างเพียงแค่สัมผัสขอบขององค์ประกอบ UI เท่านั้นสำหรับเซ็นเตอร์ข้อความจะถูกแสดงให้มีพื้นที่เท่ากันจากขอบบนของข้อความไปยังด้านบนขององค์ประกอบและขอบล่างของข้อความไปยังด้านล่างขององค์ประกอบ
คุณสมบัตินี้ใช้ร่วมกับ TextBox.TextXAlignment เพื่อกำหนดการจัดเรียงข้อความอย่างเต็มที่ในทั้งสองแกนคุณสมบัตินี้จะไม่ส่งผลต่อคุณสมบัติอ่านได้เฉพาะ TextBox.TextBounds และ TextBox.TextFits
ตัวอย่างโค้ด
This code sample shows all the different text alignment combinations by iterating over each enum item. It is meant to be placed within a TextLabel, TextButton or TextBox.
-- Paste this in a LocalScript within a 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
-- Iterate over both TextXAlignment and TextYAlignment enum items
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
วิธีการ
CaptureFocus
บังคับให้ไคลเอนต์โฟกัสที่ TextBox
ส่งค่ากลับ
ตัวอย่างโค้ด
This code sample causes the client to focus on the parent TextBox when the Q key is pressed by the player.
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
บังคับให้ไคลเอนต์ไม่โฟกัสกล่องข้อความ พารามิเตอร์ submitted ช่วยให้คุณสามารถเขียนทับพารามิเตอร์ enterPressed ในอีเวนต์ TextBox.FocusLost ได้
รายการนี้ควรใช้กับ LocalScript เพื่อทำงานตามที่คาดไว้ในโหมดออนไลน์
รหัสที่แสดงด้านล่างจะบังคับให้ไคลเอนต์ไม่โฟกัส 'TextBox' ภายใน 5 วินาทีหลังจากที่เลือก:
local TextBox = script.Parent
TextBox.Focused:Connect(function()
wait(5)
TextBox:ReleaseFocus()
end)
โปรดทราบว่าตัวอย่างด้านบนคาดว่ามันอยู่ใน LocalScript เป็นลูกของ TextBox
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
The code shown below will force the client to unfocus the 'TextBox' 5 seconds after it's selected:
local textBox = script.Parent
local function onFocused()
task.wait(5)
textBox:ReleaseFocus()
end
textBox.Focused:Connect(onFocused)
อีเวนต์
FocusLost
ไฟเมื่อไคลเอนต์ปล่อยให้โฟกัสออกจาก TextBox - โดยปกติเมื่อไคลเอนต์คลิก/แตะนอก TextBoxนอกจากนี้ยังจะยิงหาก TextBox บังคับให้โฟกัสกับผู้ใช้
สามารถใช้ร่วมกับ TextBox.Focused เพื่อติดตามเมื่อกล่องข้อความได้รับและสูญเสียโฟกัส
ดูเพิ่มเติมเกี่ยวกับ UserInputService.TextBoxFocused และ UserInputService.TextBoxFocusReleased สำหรับฟังก์ชันที่คล้ายกันที่ใช้บริการ UserInputService
อีเวนต์นี้จะยิงเฉพาะเมื่อใช้ใน LocalScript
พารามิเตอร์
เป็นไบนารีที่ระบุว่าไคลเอนต์กด Enter เพื่อสูญเสียโฟกัส ( จริง ) หรือไม่ ( ไม่ )
ตัวอย่าง InputObject บ่งบอกถึงประเภทของการใส่ที่ทําให้ TextBox สูญเสียโฟกัส
ตัวอย่างโค้ด
The example shown below will print "Focus was lost because enter was pressed!" whenever the TextBox loses focus as a result of the enter key being pressed.
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)
This example works when placed in a LocalScript that is the child of a TextBox. When the TextBox loses focus, the example prints either:
- "Player pressed Enter" - if the TextBox lost focus because the player pressed the Enter key. or
- "Player pressed InputObject.KeyCode" - where "KeyCode" is the InputObject KeyCode property of the input that caused the TextBox to lose focus. For example, pressing the Escape (esc) key to exit TextBox focus returns an InputObject instance with the KeyCode 'InputObject.Escape'.
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
ไฟเมื่อ TextBox ได้รับความสนใจ - โดยปกติเมื่อไคลเอนต์คลิก/แตะที่ TextBox เพื่อเริ่มการป้อนข้อความนอกจากนี้ยังจะยิงหาก TextBox บังคับให้โฟกัสกับผู้ใช้
สามารถใช้ร่วมกับ TextBox.FocusLost เพื่อติดตามเมื่อกล่องข้อความได้รับและสูญเสียโฟกัส
ดูเพิ่มเติมเกี่ยวกับ UserInputService.TextBoxFocused และ UserInputService.TextBoxFocusReleased สำหรับฟังก์ชันที่คล้ายกันที่ใช้บริการ UserInputService
อีเวนต์นี้จะยิงเฉพาะเมื่อใช้ใน LocalScript
ตัวอย่างโค้ด
This example works when placed in a LocalScript that is the child of a TextBox. When the TextBox gains focus, the example prints "Focus".
local textBox = script.Parent
local function onFocused()
print("Focused")
end
textBox.Focused:Connect(onFocused)