TextButton

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

一个 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 属性影响的表情符号,例如 🔒 和其他符号。这些可以粘贴到 和 对象以及 属性窗口内的字段。

该属性可能包含新行字符。相同,该属性可能包含一个标签字符,但它将以空格形式渲染。

代码示例

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

读取并联

此属性设置渲染文本的stroke或轮廓颜色。与 TextStrokeTransparency 一起,它决定了文本杆的最终视觉外观。

作为一个强大的替代方案,支持颜色渐变,请参阅 UIStroke

TextStrokeTransparency

读取并联

此属性设置渲染文本的stroke或轮廓透明度。与 TextStrokeColor3 一起,它决定了文本杆的最终视觉外观。

请注意,文本stroke是同一透明度的多个渲染,因此此属性实际上是四倍增加的。因此,建议将 TextStrokeTransparency 设置为范围内的值 0.751 的更多微妙效果。

作为一个强大的替代方案,支持颜色渐变,请参阅 UIStroke

TextTransparency

读取并联

该属性决定了 TextButton 渲染的所有文本的透明度。

TextTruncate

读取并联

控制在 TextButton 中显示的文本的截断。

TextWrapped

读取并联

启用后,该属性将在 TextButton 元素空间内的多个线上渲染文本,以便 TextBounds 永远不会超过元素的 GuiBase2d.AbsoluteSize 。这可以通过将长条文本拆分为多个线来实现。

行间空格会被首选;如果一个长而未被分割的单词超出元素宽度,该单词将被分为多个线段。

如果进一步的行分割会导致文本( Y 组件的TextBounds)的垂直高度超过元素( Y 组件的GuiBase2d.AbsoluteSize)的垂直高度,那条线将不会被渲染。

TextXAlignment

读取并联

该属性决定对象空间内渲染的文本的横向对齐。它与 TextYAlignment 结合使用,可以完全确定在两个轴上的文本对齐。

请注意,此属性不会影响 read-only 属性 TextBoundsTextFits

TextYAlignment

读取并联

该属性决定对象空间内渲染的文本的垂直对齐。它与 TextXAlignment 结合使用,可以完全确定在两个轴上的文本对齐。

请注意,此属性不会影响 read-only 属性 TextBoundsTextFits

方法

活动