TextService

非推奨を表示

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

作成できません
サービス
複製されていません

TextService は、ゲーム内のテキストの表示を処理するために内部で責任を負っています。

このクラスには 2つのメンバー機能があります:

Class.TextService:GetTextSize() 機能は、指定された形式の特定のテキストストリングに必要なスペースを計算するための開発者の能力を提供します。Vector2 ピクセルサイズを返します。

Class.TextService:FilterStringAsync() 機能は、ユーザーの特定のテキスト (チャットメッセージや他の入力など) をユーザーの安全性の観点から適切にフィルターするために必要です。Roblox のデフォルトの Class.Chat を使用していない開発者、またはユーザーが入力したテキストを他の方法で入力することを許可している

概要

方法

プロパティ

方法

GetTextSize

指定されたフォーマットパラメーターとサイズ制限を使用すると、Vector2 の次元をピクセル単位で計算します。

注意、 fontSize パラメータは Enum.FontSize 枚ではなく、 Enum.FontSize 枚に対応します。代わりに、 Enum.FontSize 枚に対応する整数サイズが使用される必要があります。これは、 1>Size111> の font の値と同

この関数は、TextLabel.TextBounds プロパティのTextLabelTextButton オブジェクトを使用する代わりに便利な代替です。2>Class.TextLabel.TextBlocks2> プロパティを使用して、サイズテキストを計算するため

GetTextSize を使用すると、特定の TextLabel または TextButton のテキストストリングに必要なサイズが、オブジェクトが作成される前に計設定するされることができます。

開発者は、結果にパッドのピクセルを追加することをお勧めして、テキストが切り取られることを確認します。

このメソッドは、Enum.Font にリストされているフォントにのみ制限されます。フォントのコレクションにアクセスするには、TextService:GetTextBoundsAsync() を使用できます。

パラメータ

string: string

テキストサイズを計算するストリング。

fontSize: number

使用されているフォントサイズを表す整数。

font: Enum.Font

使用されているフォント。

frameSize: Vector2

使用するテキストオブジェクトの TextLabel.AbsoluteSize 。テキストがラップされる方法を計算するために必要です。


戻り値

指定された形式の文字列によるスペースのサイズ。

コードサンプル

This example showcases a possible usage of the GetTextSize function.

It computes the possible Vector2 dimensions (in pixels) of the string "Hello World" when the font size is 12, the font size SourceSans and the frame size is (1,1).

The expected return of this function is the Vector2 value 9, 13.

TextService: Getting the Text Size

local TextService = game:GetService("TextService")
local function getTextBounds()
local message = "Hello World"
local size = Vector2.new(1, 1)
local bounds = TextService:GetTextSize(message, 12, "SourceSans", size)
return bounds + Vector2.new(1, 1)
end
print(getTextBounds())

FilterAndTranslateStringAsync

イールド

レガシーチャットではチャット翻訳はサポートされていません。このメソッドはサポートされていなくなっており、使用しないでください。すべての呼び出しは空のオブジェクトを返します。チャットメッセージの翻訳は TextChatService を介してのみ可能です。

パラメータ

stringToFilter: string
fromUserId: number
targetLocales: Array
既定値: "PrivateChat"

戻り値

FilterStringAsync

イールド

FilterStringAsync 機能は、TextService を使用して、ユーザーから受信されたストリングをフィルタリングし、TextFilterResult を返します。これは、正しくフィルタリングされたテキストをユーザーに配信するために使用できます。

使用

このメソッドは、ユーザーがメッセージを送信するたびに を呼び出す必要があります。この関数の結果をキャッシュしてはならず、結果を別のメッセージに再使用することはできません。このメソッドは、メッセージが送信されるたびに を呼び出す必要があります。キャッシュされた結果

しかし、これらの結果オブジェクトを保持して、サーバーに参加するユーザーが後で同じメッセージを表示できるようにすることが推奨されます。たとえば、これは、最小限のフィルタリングを使用してユーザーが参加する後に常に最小限のフィルタリングを使用するサーバーチャットログを安全かつ効率的に実装するため

オプションの Enum.TextFilterContext パラメーターは、クエリのフィルタリング結果に影響しません。この値は、Roblox(ロブロックス)oblox のテキストフィルタリングを改善するために使用されます。

プライベートテキストは、特定のプレイヤーだけが見ることができる、すべてのプレイヤーが見ることができるわけではないものです。たとえば、チャットがプライベートサーバー一のプレイヤーによって見られ公開か、選択されたグループのプレイヤーによって見られるか、チャットはプライベートとして見られます。チームやサーバー

注:

  • このメソッドは常に、テキストフィルターサービスの呼び出しを返します
  • このメソッドは、サービスエラーが解決できない場合に発生します。この関数がエラーをスローする場合は、リクエストを再試行しないでください。このメソッドは、内部的にリトライアロジックを実装しています。このメソッドが失敗すると、テキストをユーザーに表示しません。
  • このメソッドは、現在、fromUserId が現在のサーバーにオンラインでない場合にスローします。我々は、将来、オフラインのユーザーまたは別のサーバーにいるユーーザーをサポートするためにこのメソッドを計画しています。

パラメータ

stringToFilter: string

フィルターをかけるテキスト。

fromUserId: number

テキストをフィルターするプレイヤーのユーザーID。

フィルターがかかったメッセージが使用するコンテキスト。

既定値: "PrivateChat"

戻り値

コードサンプル

This code sample includes a simple demonstration of how TextService:FilterStringAsync() should be implemented in a system where players are allowed to name their pets.

Note, this code sample includes the server-side implementation only (to be placed in a Script). Examples of how to implement the client-side of this system are included under the RemoteEvent and RemoteFunction examples.

Pet Name Filter Example

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextService = game:GetService("TextService")
local Players = game:GetService("Players")
local Remotes = Instance.new("Folder")
Remotes.Name = "PetNamingRemotes"
Remotes.Parent = ReplicatedStorage
local UserNamedPet = Instance.new("RemoteEvent")
UserNamedPet.Name = "UserNamedPet"
UserNamedPet.Parent = Remotes
local SendPetName = Instance.new("RemoteEvent")
SendPetName.Name = "SendPetName"
SendPetName.Parent = Remotes
local RequestAllPetNames = Instance.new("RemoteFunction")
RequestAllPetNames.Name = "RequestAllPetNames"
RequestAllPetNames.Parent = Remotes
local filterResults = {}
local function broadcastPetName(userId)
local filterResult = filterResults[userId]
if filterResult then
for _, player in pairs(Players:GetPlayers()) do
if player then
-- spawn a new thread so as to not yield the update
task.spawn(function()
-- grab the filtered string for this user
local toUserId = player.UserId
local filteredString = filterResult:GetNonChatStringForUserAsync(toUserId)
filteredString = filteredString or ""
SendPetName:FireClient(player, userId, filteredString)
end)
end
end
end
end
UserNamedPet.OnServerEvent:Connect(function(player, petName)
local fromUserId = player.UserId
-- pcall to catch errors
local success, result = pcall(function()
return TextService:FilterStringAsync(petName, fromUserId)
end)
if success then
filterResults[fromUserId] = result
broadcastPetName(fromUserId)
else
print("Could not filter pet name")
end
end)
RequestAllPetNames.OnServerInvoke = function(player)
local toUserId = player.UserId
local petNames = {}
-- go through filter results and filter the pet name to be sent
for fromUserId, filterResult in pairs(filterResults) do
local filteredString = filterResult:GetNonChatStringForUserAsync(toUserId)
filteredString = filteredString or ""
-- need to convert userId to string so it can't be sent via a remote function
petNames[tostring(fromUserId)] = filteredString
end
return petNames
end
Players.PlayerRemoving:Connect(function(oldPlayer)
local userId = oldPlayer.UserId
filterResults[userId] = nil
end)

GetFamilyInfoAsync

イールド

フォントファミリの名前と顔のコラムを含むテーブルを返します。

返されたテーブルは、次のように構造されています:


type FaceInfo = {
Name: string, -- 例: "Regular"、"Book"、"Italic"、"Thin Italic"
Weight: Enum.FontWeight,
Style: Enum.FontStyle, -- イタリア語またはノーマル
}
type FamilyInfo = {
Name: string, -- 例: "Source Sans Pro"、 "Grenze Gotisch"
Faces: {FaceInfo} -- 最小 1 個が常にありますが、最大 18 個まであります。
}

フォント家族が以前の呼び出しで GetFamilyInfoAsyncContentProvider:PreloadAsync() 、または TextLabel.FontFace プロパティを設定したテキストオブジェクトである場合、メソッドは 1> 無効なパラメーターを返しません。

エラー

このメソッドはネットワークエラーにより失敗する可能性があります。エラー処理のために常に pcall でラップしてください。

これらのシナリオでエラーが発生します:

  • パスされた家族は空の文字列です。
  • 家族のダウンロードに失敗しました。
  • アセット ID は無効であるか、または存在しないアセットを指しています。

パラメータ

assetId: ContentId

アセットのフォントファミリのアイデントを参照します。


戻り値

フォントファミリーに関する情報。

コードサンプル

This example showcases a possible usage of the TextService:GetFamilyInfoAsync() method.

It prints out information about a font family to the output window.

TextService: Getting font family information

local TextService = game:GetService("TextService")
local familyToCheck = "rbxasset://fonts/families/Arial.json"
-- This is a yield function which may take up to a few seconds to download the font.
local info = TextService:GetFamilyInfoAsync(familyToCheck)
print("Name of the font:", info.Name)
print("Faces:")
for _, face in info.Faces do
print("--------")
print("Name:", face.Name)
print("Weight:", face.Weight.Name)
print("Style:", face.Style.Name)
end

GetTextBoundsAsync

イールド

このメソッドは TextService:GetTextSize() と似ていますが、Font オブジェクトを使用して、Enum.Font よりも多くのフォントにアクセスできます。

テキストのサイズ、フォン文字列、ストリングなどのプロパティを含むプロパティセットを測定するために使用されます。

これは、フォントの中に読み込まれる必要があるため、生成関数です。フォントがすでに読み込まれている場合は、生成されません。ContentProvider:PreloadAsync() を使用して、フォントを読み込むことを確認できます。

エラー

このメソッドはネットワークエラーにより失敗する可能性があります。エラー処理のために常に pcall でラップしてください。

これらのシナリオでエラーが発生します:

  • Class.GetTextBoundsProps.Font には空の家族があります。
  • paramètres 引数は空でした。
  • フォントファミリーまたはフォントの顔をダウンロードできませんでした。

パラメータ

Class.GetTextBoundsProps オブジェクトへの参照。


戻り値

テキストのサイズを Vector2 とします。

コードサンプル

This example shows how you can use TextService:GetTextBoundsAsync().

It measures the size of some text in a specific font, then prints the result.

TextService: Measuring text size

local TextService = game:GetService("TextService")
local params = Instance.new("GetTextBoundsParams")
params.Text = "hello world!"
params.Font = Font.new("rbxasset://fonts/families/GrenzeGotisch.json", Enum.FontWeight.Thin)
params.Size = 20
params.Width = 200
local size = TextService:GetTextBoundsAsync(params)
print("The size of the text is:", size)

GetTextSizeOffsetAsync

イールド

パラメータ

fontSize: number
font: Enum.Font

戻り値

イベント