Lớp công cụ
TextService
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Tóm Tắt
Phương Pháp
FilterAndTranslateStringAsync(stringToFilter: string,fromUserId: number,targetLocales: {any},textContext: Enum.TextFilterContext):TextFilterTranslatedResult |
FilterStringAsync(stringToFilter: string,fromUserId: number,textContext: Enum.TextFilterContext):TextFilterResult |
GetFamilyInfoAsync(assetId: ContentId):Dictionary |
GetTextBoundsAsync(params: GetTextBoundsParams):Vector2 |
GetTextSizeOffsetAsync(fontSize: number,font: Font):number |
Tài Liệu Tham Khảo API
Phương Pháp
FilterAndTranslateStringAsync
FilterStringAsync
TextService:FilterStringAsync(
Tham Số
| Giá Trị Mặc Định: "PrivateChat" |
Lợi Nhuận
Mẫu mã
Ví dụ bộ lọc tên thú cưng
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
-- tạo một luồng mới để không làm chậm quá trình cập nhật
task.spawn(function()
-- lấy chuỗi đã lọc cho người dùng này
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
-- sử dụng pcall để bắt lỗi
local success, result = pcall(function()
return TextService:FilterStringAsync(petName, fromUserId)
end)
if success then
filterResults[fromUserId] = result
broadcastPetName(fromUserId)
else
print("Không thể lọc tên thú cưng")
end
end)
RequestAllPetNames.OnServerInvoke = function(player)
local toUserId = player.UserId
local petNames = {}
-- đi qua kết quả lọc và lọc tên thú cưng cần gửi
for fromUserId, filterResult in pairs(filterResults) do
local filteredString = filterResult:GetNonChatStringForUserAsync(toUserId)
filteredString = filteredString or ""
-- cần chuyển đổi userId sang chuỗi để không thể gửi thông qua hàm từ xa
petNames[tostring(fromUserId)] = filteredString
end
return petNames
end
Players.PlayerRemoving:Connect(function(oldPlayer)
local userId = oldPlayer.UserId
filterResults[userId] = nil
end)GetFamilyInfoAsync
Tham Số
assetId:ContentId |
Lợi Nhuận
Mẫu mã
Lấy thông tin họ phông
local TextService = game:GetService("TextService")
local familyToCheck = "rbxasset://fonts/families/Arimo.json"
local success, info = pcall(function()
return TextService:GetFamilyInfoAsync(familyToCheck)
end)
if success then
print("Tên phông chữ:", info.Name)
print("Các kiểu mặt:")
for _, face in info.Faces do
print("--------")
print("Tên:", face.Name)
print("Trọng số:", face.Weight.Name)
print("Kiểu:", face.Style.Name)
end
endGetTextBoundsAsync
Tham Số
Lợi Nhuận
Mẫu mã
Đo kích thước văn bản
local TextService = game:GetService("TextService")
-- Khai báo tham số
local params = Instance.new("GetTextBoundsParams")
params.Text = "Xin chào thế giới!"
params.Font = Font.new("rbxasset://fonts/families/GrenzeGotisch.json", Enum.FontWeight.Thin)
params.Size = 20
params.Width = 200
local success, bounds = pcall(function()
return TextService:GetTextBoundsAsync(params)
end)
if success then
print(bounds)
endGetTextSize
Lợi Nhuận
Mẫu mã
Lấy Kích Thước Văn Bản
local TextService = game:GetService("TextService")
local function getTextBounds()
local message = "Xin chào thế giới!"
local size = Vector2.new(1, 1)
local bounds = TextService:GetTextSize(message, 12, "SourceSans", size)
return bounds
end
print(getTextBounds())GetTextSizeOffsetAsync
Lợi Nhuận
Mẫu mã
Lấy Kích Thước Văn Bản Bù
local TextService = game:GetService("TextService")
local label = script.Parent.TextLabel
-- Lấy giá trị bù điều chỉnh cho cài đặt kích thước văn bản ưa thích của người chơi
local success, offset = pcall(function()
return TextService:GetTextSizeOffsetAsync(label.TextSize, label.FontFace)
end)
if success then
-- Điều chỉnh chiều cao của nhãn bằng cách thêm giá trị bù
label.Size = UDim2.fromOffset(label.Size.X.Offset, label.Size.Y.Offset + offset)
end