TextButton

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

A TextButton 在渲染方面與 TextLabel 相似,並具有額外的 GuiButton 行為。

您可以通過設置 TextTransparency1 來禁用文字渲染。這將導致一個簡單的長方形,可用作按鈕。

概要

屬性

屬性 繼承自 GuiButton屬性 繼承自 GuiObject屬性 繼承自 GuiBase2d

方法

方法 繼承自 GuiObject

活動

活動 繼承自 GuiButton活動 繼承自 GuiObject活動 繼承自 GuiBase2d

屬性

ContentText

唯讀
未複製
平行讀取

此屬性提供一個複製的 Text 包含正確由 TextButton 渲染的內容。這對於消除用於 富文本 標記的風格標籤有用;例如,當 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>
富文本
隱藏
未複製
平行讀取

此屬性會選擇 TextButton 將渲染其文字的多個預定義字體之一。有些字體有粗體、義大利體和/或淺體變體。

除了 Enum.Font.Legacy 字體外,每個字體都會渲染文字的線高與 TextSize 屬性相等。

Enum.Font.Code 字體是唯一的單空格字體。它具有每個字元的正確相同寬度和高度比率為 1:2,每個字元的寬度約為 TextSize 屬性的一半。

此屬性與 FontFace 屬性保持同步。

範例程式碼

This code sample renders a list of all the available fonts.

Show All 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 如果沒有匹配。

LineHeight

平行讀取

控制線的高度作為字體的 em 方塊尺寸的倍數,通過在 TextButton 中調整文本線之間的間距來調整線的高度。有效值範圍從 1.03.0 , 預設為 1.0

LocalizedText

隱藏
唯讀
未複製
平行讀取

此屬性設置是否應該將 TextButton 視為 GuiBase2d.Localize 或不。

MaxVisibleGraphemes

平行讀取

此屬性控制在 TextButton 上顯示的最大字元數量 (或文字單位)。主要提供簡單的方法來創建 打字機效果 ,其中字符一次出現。

更改屬性不會更改可見的字元位置或尺寸;布局將被計算為所有字元都可見的情況。

將屬性設為 -1 將限制禁用並顯示 Text 的整體。

OpenTypeFeatures

平行讀取

OpenTypeFeaturesError

唯讀
未複製
平行讀取

RichText

平行讀取

此屬性決定是否使用 TextButton 將其文字渲染為使用 富文本 標記來裝飾字串的段落,以粗體、斜體、特定顏色等形式。

若要使用豐富文字,請在 Text 字串中包含豐富文字格式標籤。

Text

平行讀取

此屬性決定由 TextButton 渲染的文字內容。渲染到畫面的字串的視覺特性由 , , , > , > 和 > 決定。

可以渲染不受 TextColor3 屬性影響的表情符號,例如 🔒 和其他符號。這些可以貼到 ScriptLocalScript 對象以及 屬性窗口 內的欄位。

此屬性可能包含新行字元。相同地,此屬性可能包含一個標籤字元,但它會以空格顯示。

範例程式碼

This code sample creates a fading banner for a TextLabel. It fades text out, chooses a random string (avoiding repetition), and fades back in.

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

This code sample demonstrates emoji rendering using the Text property.

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

唯讀
未複製
平行讀取

這個只讀屬性反映了渲染文字的絕對大小在偏移中,這意味著如果你嘗試將文字放入長方形,這個屬性將反映你需要在長方形中放置文字的最小尺寸,以便將文字放置。

使用 TextService:GetTextSize() , 您可以預測 TextBounds 將會得到什麼字串、FontTextSize 和框架大小。

TextColor3

平行讀取

此屬性決定 TextButton 元素渲染的所有文字的顏色。

範例程式碼

This code sample makes a TextLabel or TextButton count backwards from 10, setting the text color as it does so.

Countdown Text

-- 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

TextDirection

平行讀取

Enum.TextDirection 在其中文字被渲染。

TextFits

唯讀
未複製
平行讀取

代表按鈕文字是否符合按鈕尺寸的 boolean 表示。

TextScaled

平行讀取

此屬性決定是否將文字縮放至填滿按鈕的整個空間。啟用時,TextSize被忽略,TextWrapped自動啟用。此屬性對於在 BillboardGuis 內渲染文字元素有用。當此屬性用於 在畫面上的用戶介面 時,可能有用使用 來限制可能的文字尺寸範圍。

自動縮放

建議您避免使用 TextScaled 並調整介面以利用 AutomaticSize 屬性。以下是兩個屬性之間的核心差異:

  • TextScaled 將內容(文字)縮放以容納介面,如果沒有仔細考慮,一些文字可能會因縮放太小而變得難以讀取。

  • AutomaticSize 將UI縮放以容納內容,同時保持一致的字體尺寸。有關更多信息,請參閱這裡

此外,建議您避免同時應用 AutomaticSizeTextScaled 以及同一個 TextButtonAutomaticSize 決定 GuiObject 可以使用的最大空間(在這種情況下是文字),而 TextScaled 使用由 AutomaticSize 決定的可用空間來將字體尺寸縮放到最大字體尺寸(100),如果沒有尺寸限制。

TextSize

平行讀取

此屬性決定一行渲染的文字高度。單位是在偏移中,而不是點(這是大多數文件編輯程序使用的)。Enum.Font.Legacy 字體沒有這個屬性。

TextStrokeColor3

平行讀取

此屬性設置渲染的文字筆或外部線的顏色。與 TextStrokeTransparency 一起,它決定文字筆跡的最終視覺外觀。

作為一種強大的替代方案,支持顏色渐變,請參閱 UIStroke

TextStrokeTransparency

平行讀取

此屬性設置渲染的文字筆或輪廓的透明度。與 TextStrokeColor3 一起,它決定文字筆跡的最終視覺外觀。

請注意,文字輪廓是多種渲染同一透明度的方式,因此此屬性在自身上基本上翻倍四次。因此,建議將 TextStrokeTransparency 設為範圍內的值 0.751 的值,以獲得更細微的效果。

作為一種強大的替代方案,支持顏色渐變,請參閱 UIStroke

TextTransparency

平行讀取

此屬性決定 TextButton 所渲染的所有文字的透明度。

TextTruncate

平行讀取

控制在 TextButton 中顯示的文字的截斷。

TextWrapped

平行讀取

啟用時,此屬性會在 TextButton 元素的空間內渲染多條線上的文字,使 TextBounds 永遠不會超過元素的 GuiBase2d.AbsoluteSize 。這可以通過將長篇文字拆分為多個線來實現。

行間空格會被首選;如果長而未被分割的單詞超出元素寬度,那個單詞將被分為多個線條。

如果進一步斷裂線將導致文本( Y 元素的TextBounds)的垂直高度超過元素的垂直高度( Y 元素的GuiBase2d.AbsoluteSize),那條線將不會被渲染。

TextXAlignment

平行讀取

此屬性決定對物件空間內渲染的文字的水平對齊。它與 TextYAlignment 一起使用,可完全確定在兩個軸上的文字對齊。

請注意,此屬性不會影響閱讀僅屬性 TextBoundsTextFits

TextYAlignment

平行讀取

此屬性決定對物件空間內渲染的文字的垂直對齊。它與 TextXAlignment 一起使用,可完全確定在兩個軸上的文字對齊。

請注意,此屬性不會影響閱讀僅屬性 TextBoundsTextFits

方法

活動