TextService
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
テキストサービスは、ゲーム内でテキストの表示を処理する内部サービスです。
このクラスには 2つのメンバー関数があります:
TextService:GetTextSize() 関数は、特定のフォーマットで指定されたテキストストリングに必要なスペースを計算する能力を開発者に提供し、Vector2 ピクセルサイズを返します。
TextService:FilterStringAsync() 機能は、ユーザーが指定したテキスト (チャットメッセージや他の入力など) をユーザーの安全のために適切にフィルタリングするために必要です。Roblox のデフォルトを使用していない開発者、またはユーザーがテキストをその他の形式で入力できるようにする必要がある場合は、この関数を使用する必要があります。
概要
方法
指定されたフォーマットパラメータとサイズ制約を使用すると、テキストで取り込まれる Vector2 次元 (ピクセル) を計算します。
- FilterAndTranslateStringAsync(stringToFilter : string,fromUserId : number,targetLocales : Array,textContext : Enum.TextFilterContext):TextFilterTranslatedResult
チャット翻訳はレガシーチャットではサポートされていません。この方法はもはやサポートされておらず、使用してはなりません。
- FilterStringAsync(stringToFilter : string,fromUserId : number,textContext : Enum.TextFilterContext):TextFilterResult
ユーザーから受信された文字列をフィルターし、正しくフィルター処理されたテキストを適切に配布するために使用できる TextFilterResult を返します。
フォント家族の名前と顔を含むテーブルを返します。
指定されたパラメータのテキストの幅と高さを計算します。
プロパティ
方法
GetTextSize
指定されたフォーマットパラメータとサイズ制約を使用すると、テキストで取り込まれる Vector2 次元 (ピクセル) を計算します。
フォントサイズパラメータは、Enum.FontSize Enum を受け入れません。代わりに、Enum.FontSize Enum に対応する整数サイズが使用されるべきです。これは Enum.FontSize Enum の値と同じではありません。たとえば、Size11 フォントの場合、整数 11 が使用される必要があります。
この機能は、 および および オブジェクトの プロパティの有用な代替です。TextLabel.TextBounds プロパティを使用して、寸法テキストを計算するには、作成する TextLabel オブジェクトが必要になるため、実用的ではありません。
GetTextSize では、特定の TextLabel または TextButton の特定のテキストストリングに必要な寸法は、オブジェクトが作成されたり、テキストプロパティが設定される前に設定する算できます。
開発者は、結果にパッドピクセルを追加することをお勧めし、テキストが切り落とされないようにします。
このメソッドは、Enum.Font にリストされているフォントに限定されます。より多くのフォントにアクセスするには、代わりに TextService:GetTextBoundsAsync() を使用できます。
パラメータ
テキストサイズを計算するための文字列。
使用されたフォントサイズを表す整数。
使用されたフォント。
使用するテキストオブジェクトの 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.
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 を介してのみ利用可能です。
パラメータ
戻り値
FilterStringAsync
FilterStringAsync 関数は、ユーザーから受信された文字列を TextService を使用してフィルタリングし、正しくフィルタリングされたテキストを適切に配布するために使用できる TextFilterResult を返します。
使用法
このメソッドは、ユーザーがメッセージを送信するたびに 1 回呼び出されるべきです。この機能の結果をキャッシュしないで、別のメッセージで再使用しないでください。ユーザーが同じテキストを複数回送信すると、メッセージが送信されるたびにこのメソッドが再度呼び出されなければなりません。結果がキャッシュされて再使用されると、スパム検出と多くのコンテキスト意識のフィルタリングが壊れ、ユーザーの安全を危険に晒す可能性があります。キャッシュ結果を正しく使用しないゲームは、モデレーションに直面する可能性があります。
ただし、後でサーバーに参加するユーザーに同じメッセージを表示するために、これらの結果オブジェクトを保持することを推奨します。たとえば: これは、後で参加するユーザーに常に最も制限的なフィルタリングを使用してサーバーチャットログを安全かつ効率的に実装するため、またはペットの名前のようなテキストをゲームに参加したユーザーに効率的に表示するために使用できます。
オプションの Enum.TextFilterContext パラメータは、クエリのフィルタリング結果に影響しません。この値は、Roblox のテキストフィルタリングを改善するために使用されます。
プライベートテキストは、すべてのプレイヤーではなく、特定のプレイヤーだけが見るものです。たとえば、チャットがプライベートサーバー一のプレイヤーによって見られたり、選択されたプレイヤーグループによって見られた場合、チャットはプライベートと見なされます。チーム向けのチャットや、サーバーなど、より広いグループに見える可能性のあるチャットは、公開とみなされます。あなたのテキストがどのように資格を得るか不明な場合は、オプションのフィールドを空白のままにします。
注意:
- このメソッドは常にテキストフィルタリングサービス呼び出しを生成します
- このメソッドは、解決できないサービスエラーがある場合に throw する可能性があります。この機能がエラーをスローする場合は、リクエストを再試行しないでください;このメソッドは内部で独自の再試行ロジックを実装しています。このメソッドが失敗した場合、テキストをどのユーザーにも表示しないでください。
- このメソッドは現在、ユーザーID が現在のサーバーにオンラインでない場合に throw します。将来、オフラインまたは異なるサーバー上のユーザーをサポートする予定です。
パラメータ
フィルターするテキスト。
テキストをフィルタするプレイヤーのユーザーID。
フィルタリングされたメッセージが使用されるコンテキスト。
戻り値
コードサンプル
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.
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, -- 例: "レギュラー", "ブック", "イタリック", "薄イタリック"Weight: Enum.FontWeight,Style: Enum.FontStyle, -- ノーマルかイタリックか}type FamilyInfo = {Name: string, -- 例: "ソースサンスプロ", "グレンツゴティック"Faces: {FaceInfo} -- 常に少なくとも 1 がありますが、最大 18 があり得ます。}
フォント家族が以前の呼び出しで GetFamilyInfoAsync 、ContentProvider:PreloadAsync() 、または TextLabel.FontFace プロパティセットを持つテキストオブジェクトですでにロードされている場合、メソッドは引き渡さずに返します。
エラー
このメソッドはネットワークエラーにより失敗する可能性があります。エラー処理のために常に pcall で包む必要があります。
これらのシナリオでエラーをスローします:
- パスされた家族は空の文字列です。
- 家族をダウンロードできませんでした。
- アセットIDが無効であるか、存在しないアセットを指しています。
パラメータ
検索するフォント家族のアセット ID。
戻り値
フォント家族の情報。
コードサンプル
This example showcases a possible usage of the TextService:GetFamilyInfoAsync() method.
It prints out information about a font family to the output window.
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
このメソッドは と似ていますが、より多くのフォントにアクセスできる オブジェクトを代わりに使用します。
テキストの大きさを測定するために、文字列、サイズ、およびフォントのようなプロパティのセットが与えられます。
これは、フォントを測定するためにロードする必要がある場合があるため、収益機能です。フォントがすでにロードされている場合、それは生成しません。ContentProvider:PreloadAsync() フォントがロードされているかどうかを確認するために使用できます。
エラー
このメソッドはネットワークエラーにより失敗する可能性があります。エラー処理のために常に pcall で包む必要があります。
これらのシナリオでエラーをスローします:
- The GetTextBoundsParams.Font には空の家族があります。
- params 引数は nil でした。
- フォント家族またはフォント面がダウンロードできませんでした。
パラメータ
GetTextBoundsParams オブジェクトへの参照。
戻り値
コードサンプル
This example shows how you can use TextService:GetTextBoundsAsync().
It measures the size of some text in a specific font, then prints the result.
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)