TextService

แสดงที่เลิกใช้งานแล้ว

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ไม่สามารถสร้าง
บริการ
ไม่ซ้ำ

TextService เป็นบริการที่มีความรับผิดชอบภายในเกี่ยวกับการจัดการการแสดงข้อความในเกม

คลาสนี้มีสองหน้าที่สมาชิก:

การใช้งาน TextService:GetTextSize() ช่วยให้ผู้พัฒนาสามารถคํานวณพื้นที่ที่ต้องการสําหรับข้อความข้อความที่กําหนดด้วยการจัดรูปแบบที่กำหนดไว้ การให้ความใหญ่ของ Vector2 ขนาด

ฟังก์ชัน TextService:FilterStringAsync() จำเป็นต้องใช้เพื่อการกรองข้อความผู้ใช้ที่กำหนดเอง (เช่นข้อความในการแชทหรือข้อมูลอื่น ๆ ที่ผู้ใช้กรอง) ในความสนใจของความปลอดภัยข

สรุป

วิธีการ

คุณสมบัติ

วิธีการ

GetTextSize

คํานวณมิติ Vector2 ที่จะถูกนํามาใช้กับข้อความเมื่อใช้ตัวแบ่งรูปแบบและข้อจํากัดขนาดที่กําหนด

หมายเหตุ, พารามิเตอร์ fontSize จะไม่ยอมรับ Enum.FontSize 枚 แทนที่จะใช้ขนาดตัวอักษรที่ตรงกันกับ Enum.FontSize เนื้อหุ้มต้องใช้ สิ่งนี

คุณสมบัตินี้เป็นตัวเลือกที่มีประโยชน์สำหรับสมบัติ TextLabel.TextBounds ของ TextLabel และ TextButton โดยใช้สมบัติ 2>Class.Text

ด้วย GetTextSize ขนาดที่ต้องการโดยคำสตริงข้อความเฉพาะใน TextLabel หรือ TextButton สามารถคำนวณได้ก่อนที่จะมีการสร้างหรือการตั้งค่าคุณสมบัติของข้อความ

ปิด

วิธีนี้จำกัดเฉพาะตัวอักษรที่ระบุใน Enum.Font เท่านั้น เพื่อรับการเข้าถึงตัวอักษรเพิ่มเติม คุณสามารถใช้ TextService:GetTextBoundsAsync() แทน

พารามิเตอร์

string: string

ตัวอักษรสำหรับขนาดข้อความ

fontSize: number

ตัวเลขที่แทนตัวอักษรขนาด

font: Enum.Font

ตัวอักษรที่ใช้

frameSize: Vector2

Class.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 ตัวนี้

ข้อความส่วนตัวคือสิ่งที่มองเห็นได้เฉพาะผู้เล่นบางคนเท่านั้น ไม่ใช่ทุกคน เช่น หากคุณแชทด้วยผู้เล่นคนเดียว หรือกลุ่มคนเลือก เช่น ผู้เล่นเซิร์ฟเวอร์

หมายเหตุ:

  • วิธีนี้จะทำให้เราสามารถโทรหาบริการการกรองข้อความข้อความ
  • วิธีนี้อาจปรากฏขึ้นหากมีข้อผิดพลาดในการให้บริการที่ไม่สามารถแก้ไขได้ หากวิธีนี้ปรากฏขึ้นโปรดอย่าลองทำคำขออีกครั้ง วิธีนี้จะใช้วิธีการทำคำขอที่เป็นของตัวเองในขั้นตอนการทำงานภาย
  • วิธีนี้จะปัจจุบันวิธีนี้ถูกทิ้งถ้า จาก UserId ไม่ได้ออนไลน์ในเซิร์ฟเวอร์ปัจจุบัน เราวางแผนที่จะสนับสนุนผู้ใช้ที่ออฟไลน์หรือในเซิร์ฟเวอร์อื่นในอนาคต

พารามิเตอร์

stringToFilter: string

ข้อความที่จะกรอง

fromUserId: number

รหัสผู้ใช้ของผู้กรองข้อความ

บริบทที่ข้อความกรองจะใช้

ค่าเริ่มต้น: "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
}

หากตระกูลตัวอักษรถูกโหลดโดยการโทรก่อนหน้านี้ไปยัง GetFamilyInfoAsync หรือตัวอักษรข้อความด้วยสมบัติของ ContentProvider:PreloadAsync() หรือตัวอักษรข้อความด้วยสมบัติของ Class.TextLabel.

ข้อผิดพลาด

วิธีนี้อาจล้มเหลวเนื่องจากข้อผิดพลาดในเครือข่าย คุณควร always wrap it in a pcall สำหรับการจัดการข้อผิดพลาด

ส่งข้อผิดพลาดในสถานการณ์เหล่านี้:

  • ครอบครัวที่ผ่านมาเป็นสตริงว่างเปล่า
  • การดาวน์โหลดครอบครัวล้มเหลว
  • รหัสสินทรัพย์ไม่ถูกต้องหรือชี้ไปที่สินทรัพย์ที่ไม่มีอยู่

พารามิเตอร์

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() สามารถใช้เพื่อให้แน่ใจว่าตัวอักษรนั้นโหลดได้

ข้อผิดพลาด

วิธีนี้อาจล้มเหลวเนื่องจากข้อผิดพลาดในเครือข่าย คุณควร always wrap it in a pcall สำหรับการจัดการข้อผิดพลาด

ส่งข้อผิดพลาดในสถานการณ์เหล่านี้:

  • Class.GetTextBoundsProps.Font มีครอบครัวที่ว่างเปล่า
  • ค่าตัวละคร params เป็น nul
  • ตระกูลตัวอักษรหรือหน้าตาของตัวอักษรไม่สามารถดาวน์โหลดได้

พารามิเตอร์

อ้างอิงถึงวัตถุ GetTextBoundsParams


ส่งค่ากลับ

ขนาดข้อความเป็น 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

ส่งค่ากลับ

อีเวนต์