概要
方法
計算 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.FontSize 枚對應的整數大小應該使用。 這不等於 Enum.FontSize 枚的值。 例如,對於 1> Size111> 字體的 font,整數 4> 1
此功能是 TextLabel.TextBounds 和 TextLabel 對象的有用替代品,因為使用 TextButton 屬性來計算尺寸文本通常需要一個 2>Class.TextLabel2> 對象才能創建。
使用 GetTextSize,特定文字串在特定 TextLabel 或 TextButton 中所需的尺寸可以在任何對象創建或設置文字屬設定之前計算。
建議開發者在結果中增加一個墊位,以確保沒有文字被切關閉。
此方法只適用於列在 Enum.Font 中列出的字體。若要獲得更多字體,您可以使用 TextService:GetTextBoundsAsync() 代替。
參數
返回
需要的空間大小,以像素計,與指定格式的字串。
範例程式碼
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
聊天翻譯不支援傳統聊天。此方法已停止支援並且不應該使用。所有呼叫都會返回空對物件。翻譯聊天訊息只是可用 via TextChatService 。
參數
返回
FilterStringAsync
FilterStringAsync 函數過濾受到的用戶端字串,並使用 TextService 返回一個 TextFilterResult,可以用來按照正確過濾的文字來分配相應的文字。
使用
這個方法應該每次用戶提交一個訊息時呼叫一次。不要儲存此功能的結果,並且在發送訊息時重新使用它們。如果用戶在多次提交同一個文字,此方法必須每次發送訊息時再次呼
但是,建議您保留這些結果對象,以便可以顯示同一個訊息給加入伺服器的用戶。例如:這可以用來安全並有效地實現一個伺服器聊天記錄,這使用最小限制的過濾器對用戶加入伺服器後使用,或用於顯示文字,例如寵物名稱,以便在寵物生成後第一次啟用過濾器過濾對用戶。
可選的 Enum.TextFilterContext 參數不會影響查詢的過濾結果。此值將用於改善 Roblox 的文字過濾。
私人文字是只有特定玩家看到的文字,而不是每個玩家。 例如,如果聊天被單一玩家或選擇一群玩家看到,聊天就會被視為私人。 對於團隊或可能對廣大群組眾可見的聊天,如伺服器,留下可選的文字欄是公開的。 如果您不確定您的文字是否資格,請留下可選的文字欄。
注意:
- 此方法總是會返回以作成文字過濾服務呼叫
- 此方法可能會發生,如果無法解決的服務錯誤。 如果此方法發生錯誤,請不要重試要邀請;此方法內部實現了自己的重試論。 如果此方法失敗,請不要顯示文字給任何用戶。
- 此方法目前會 throw if fromUserId 在當前服務器上不在線上。我們預計會支援在線或離線的用戶,以及在不同伺服器上。
參數
要過濾的文字。
玩家濾文的用戶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, -- 範例: "正常", "書冊", "Italic", "Thin Italic"Weight: Enum.FontWeight,Style: Enum.FontStyle, -- 正常或義大利文}type FamilyInfo = {Name: string, -- 範例: "源 Sans Pro", "Grenze Gotisch"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
此方法與 TextService:GetTextSize() 相似,但使用 Font 對象而不是 Enum.Font,這可以存取更多字體。
用於測量一些文字的大小,並設定一些字串、大小和字體的屬性。
這是輸出函數,因為有些字體可能需要載入才能測量它們。如果字體已載入,則不會輸出。ContentProvider:PreloadAsync() 可以用來確認字體是否載入。
錯誤
此方法因網路錯誤而失敗。您應該總是包它在 pcall 以處理錯誤。
在這些情況下會發生錯誤:
- Class.GetTextBoundsProps.Font 沒有空白的家庭。
- paramètres 參數為空。
- 字體家族或字體面無法載入。
參數
一個引用 Class.GetTextBoundsProps 對物件的引用。
返回
範例程式碼
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)