A TextLabel 渲染一個長方形,如 Frame ,帶有格式化文字。矩形可用於定義文字邊界、文字縮放()、包裝(>和對齊(>)。
此類別包含控制文字顯示的屬性,例如 Font 和 TextColor3 。要顯示只有文字並隱藏背景矩形,請將 BackgroundTransparency 設為 1 。
一個 UITextSizeConstraint 對象可以用來限制啟用了 TextScaled 的文字大小。建議文字大小不低於 9 ,否則可能不會對許多用戶可見。
概要
屬性
包含 TextLabel.Text 的副本,包含正在被 TextLabel 渲染的內容。
決定用於渲染文字的字體。
決定用於渲染文字的字體。
在 TextLabel 中縮放文字行間的距離。
設置是否應該將 TextLabel 設為 GuiBase2d.Localize 或不。
TextLabel 可以顯示的最大字元數量。
決定是否使用 TextLabel 富文本格式來渲染文字。
決定由 TextLabel 渲染的字串。
只讀屬性,反映偏移中渲染文字的絕對大小。
決定渲染的文字顏色。
文字渲染的方向。
是否標籤的文字是否符合標籤的尺寸的 boolean 表示。
變更文字是否會縮放以符合 TextLabel 內。
決定抵消時文字的行高度。
決定文字筆跡(外部)的顏色。
決定文字筆跡(輪廓)的透明度。
決定渲染的文字透明度。
控制在 TextLabel 中顯示的文字的截斷。
決定是否將文字包裝到 TextLabel 元素空間內的多個線上,刪除多餘的文字。
決定渲染文字的水平對齊。
決定渲染文字的垂直對齊。
決定這個介面元素是否會沉沒輸入。
決定 GuiObject 的起始點,相對於其絕對尺寸。
決定是否基於兒童內容進行縮放。
決定 GuiObject 背景顏色。
決定 GuiObject 背景和邊框的透明度。
決定 GuiObject 邊框的顏色。
決定在哪種方式下 GuiObject 邊界被布置在其尺寸相對。
決定 GuiObject 邊框的像素寬度。
決定是否在父 GUI 元素的範圍之外成像子元素 GuiObjects 。
決定玩家的鼠標是否在 GuiObject 上被主動按下。
控制使用 GuiObject 與 UIGridStyleLayout 時的排序順序。
設置在遊戲控制器選擇器向下移動時會選擇的 GuiObject 。
設置在遊戲控制器選擇器移動到左側時會選擇的 GuiObject 。
設置在遊戲控制器選擇器移動到右側時會選擇的 GuiObject 。
設置在遊戲控制器選擇器向上移動時會選擇的 GuiObject 。
決定 GuiObject 的像素和向量位置。
決定旋轉 GuiObject 的角度數量。
確定是否可以由遊戲控制器選擇 GuiObject 。
覆蓋使用於遊戲板的預設選擇裝飾。
由遊戲控制器介面選擇所選擇的 GuiObjects 順序。
決定 GuiObject 的像素和積分尺寸。
混合屬性 BackgroundTransparency 和 TextTransparency 。
決定是否渲染 GuiObject 和其子孫。
決定 GuiObject 對其他人的相對順序進行渲染。
方法
方法 繼承自 GuiObject- 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 聚焦時,發射。
當遊戲控制器選擇器停止關注 GuiObject 時發射。
當玩家開始、繼續並停止長按 UI 元素時,發生火災。
- TouchPan(touchPositions : Array,totalTranslation : Vector2,velocity : Vector2,state : Enum.UserInputState):RBXScriptSignal
當玩家在介面元素上移動手指時發生火災。
- TouchPinch(touchPositions : Array,scale : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
當玩家使用介面元素上的兩個手指執行撥動或拉動動作時,發生火焰。
- TouchRotate(touchPositions : Array,rotation : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
當玩家使用介面元素上的兩個手指執行旋轉動作時,發生火災。
當玩家在介面元素上執行滑動動作時,發生火災。
當玩家在介面元素上執行點擊動作時,發生火焰。
- SelectionChanged(amISelected : boolean,previousSelection : GuiObject,newSelection : GuiObject):RBXScriptSignal
當遊戲控制器選擇移動到、離開或變更連接的 GuiBase2d 或任何子控制器 GuiObjects 時發生火災。
屬性
ContentText
此屬性提供一個複製的 Text 包含正確由 TextLabel 渲染的內容。這對於消除用於 富文本 標記的風格標籤有用;例如,當 RichText 啟用時,ContentText 屬性會顯示用戶看到的文字。
<th>文字</th><th>內容文字</th></tr></thead><tbody><tr><td><code>否</code></td><td><b>你好,<br/> 世界!</b></td><td><b>你好,<br/> 世界!</b></td></tr><tr><td><code>真的</code></td><td><b>你好,<br/> 世界!</b></td><td>你好,世界!</td></tr></tbody>
富文本 |
---|
Font
此屬性會選擇 TextLabel 將渲染其文字的多個預定義字體之一。有些字體有粗體、義大利體和/或淺體變體。
除了 Enum.Font.Legacy 字體外,每個字體都會渲染文字的線高與 TextSize 屬性相等。
Enum.Font.Code 字體是唯一的單空格字體。它具有每個字元的正確相同寬度和高度比率為 1:2,每個字元的寬度約為 TextSize 屬性的一半。
此屬性與 FontFace 屬性保持同步。
範例程式碼
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
此屬性與 Font 屬性相似,但允許設置不存在於 Enum.Font 中的字體。
此屬性與 Font 屬性保持同步,使當設置 FontFace 時,字體設為相應的 Enum.Font 值或 Enum.Font.Unknown 如果沒有匹配。
MaxVisibleGraphemes
此屬性控制在 TextLabel 上顯示的最大字元數量 (或文字單位)。主要提供簡單的方法來創建 打字機效果 ,其中字符一次出現。
更改屬性不會更改可見的字元位置或尺寸;布局將被計算為所有字元都可見的情況。
將屬性設為 -1 將限制禁用並顯示 Text 的整體。
OpenTypeFeatures
OpenTypeFeaturesError
RichText
此屬性決定是否使用 TextLabel 將其文字渲染為使用 富文本 標記來裝飾字串的段落,以粗體、斜體、特定顏色等形式。
若要使用豐富文字,請在 Text 字串中包含豐富文字格式標籤。
Text
此屬性決定由 TextLabel 渲染的文字內容。渲染到畫面的字串的視覺特性由 , , , > , > 和 > 決定。
可以渲染不受 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 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 將會得到什麼字串、Font 、TextSize 和框架大小。
TextColor3
此屬性決定 TextLabel 元素渲染的所有文字的顏色。
範例程式碼
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
TextScaled
此屬性決定是否將文字縮放至填滿標籤的整個空間。啟用時,TextSize被忽略,TextWrapped自動啟用。此屬性對於在 BillboardGuis 內渲染文字元素有用。當此屬性用於 在畫面上的用戶介面 時,可能有用使用 來限制可能的文字尺寸範圍。
自動縮放
建議您避免使用 TextScaled 並調整介面以利用 AutomaticSize 屬性。以下是兩個屬性之間的核心差異:
TextScaled 將內容(文字)縮放以容納介面,如果沒有仔細考慮,一些文字可能會因縮放太小而變得難以讀取。
AutomaticSize 將UI縮放以容納內容,同時保持一致的字體尺寸。有關更多信息,請參閱這裡。
此外,建議您避免同時應用 AutomaticSize 和 TextScaled 以及同一個 TextLabel 。AutomaticSize 決定 GuiObject 可以使用的最大空間(在這種情況下是文字),而 TextScaled 使用由 AutomaticSize 決定的可用空間來將字體尺寸縮放到最大字體尺寸(100),如果沒有尺寸限制。
TextStrokeColor3
此屬性設置渲染的文字筆或外部線的顏色。與 TextStrokeTransparency 一起,它決定文字筆跡的最終視覺外觀。
作為一種強大的替代方案,支持顏色渐變,請參閱 UIStroke 。
TextStrokeTransparency
此屬性設置渲染的文字筆或輪廓的透明度。與 TextStrokeColor3 一起,它決定文字筆跡的最終視覺外觀。
請注意,文字輪廓是多種渲染同一透明度的方式,因此此屬性在自身上基本上翻倍四次。因此,建議將 TextStrokeTransparency 設為範圍內的值 0.75 到 1 的值,以獲得更細微的效果。
作為一種強大的替代方案,支持顏色渐變,請參閱 UIStroke 。
TextWrapped
啟用時,此屬性會在 TextLabel 元素的空間內渲染多條線上的文字,使 TextBounds 永遠不會超過元素的 GuiBase2d.AbsoluteSize 。這可以通過將長篇文字拆分為多個線來實現。
行間空格會被首選;如果長而未被分割的單詞超出元素寬度,那個單詞將被分為多個線條。
如果進一步斷裂線將導致文本( Y 元素的TextBounds)的垂直高度超過元素的垂直高度( Y 元素的GuiBase2d.AbsoluteSize),那條線將不會被渲染。
TextXAlignment
此屬性決定對物件空間內渲染的文字的水平對齊。它與 TextYAlignment 一起使用,可完全確定在兩個軸上的文字對齊。
請注意,此屬性不會影響閱讀僅屬性 TextBounds 和 TextFits 。
TextYAlignment
此屬性決定對物件空間內渲染的文字的垂直對齊。它與 TextXAlignment 一起使用,可完全確定在兩個軸上的文字對齊。
請注意,此屬性不會影響閱讀僅屬性 TextBounds 和 TextFits 。