cách của mình là dựa vào CharacterRegion trong *.spriteFont
1. thử nghiệm mặc định
- mục đích : in cái dòng "Xin chào cộng đồng cviet" ra màn hình
- tạo 1 file spriteFont từ project, mặc định nội dung của file font là
Mã:
<?xml version="1.0" encoding="utf-8"?><XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics"> <Asset Type="Graphics:FontDescription"> <FontName>Segoe UI Mono</FontName> <Size>14</Size> <Spacing>0</Spacing> <UseKerning>true</UseKerning> <Style>Regular</Style> <CharacterRegions> <CharacterRegion> <Start> </Start> <End>~</End> </CharacterRegion> </CharacterRegions> </Asset></XnaContent>
trong Game1.cs tôi code như sau
Mã:
using System;using System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Input.Touch;using Microsoft.Xna.Framework.Media; namespace WindowsPhoneGame1{ public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont font = null; String s = "Xin chào cộng đồng cviet"; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; TargetElapsedTime = TimeSpan.FromTicks(333333); } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); font = Content.Load<SpriteFont>("font/font"); } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.DrawString(font, s, new Vector2(), Color.White); spriteBatch.End(); base.Draw(gameTime); } }}
chắc chắn sẽ lỗi be bét, Exception sẽ nhẩy đến đoạn spriteBatch.DrawString(font, s, new Vector2(), Color.White);
cụ thể sẽ nhảy đến cái từ "à" trong chữ "chào"
do
2. Hướng suy nghĩ, tìm giải pháp
phân tích :
- nó chỉ lỗi ở các kí tự nguyên âm tiếng việt : à ,á ,ă,...v v
- mở cái file *.spriteFont trên ra, để ý đoạn dưới có đoạn
Mã:
<CharacterRegion> <Start> </Start> <End>~</End> </CharacterRegion>
- để ý tiếp cái đống &#, 32,126 là cái quái gì nhỉ ? , start từ 32 đến 126 --> các bạn mở bảng mã ASCII ra xem nó là gì nhé. ---> các kí tự tiếng anh nó nằm trong đống này, nên draw tiếng anh thoải mái
- &# đằng trước nghĩa là html code của kí tự đó
- còn tiếng việt ? tôi lên google search : html code for vietnamese đc trang này http://art-hanoi.com/vncode/
- tôi lần lượt thêm vào các thẻ <CharacterRegion> với các html code tiếng việt như trang đó
- tóm lại sau khi thêm các <CharacterRegion> tôi có file font như sau
Mã:
<?xml version="1.0" encoding="utf-8"?><XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics"> <Asset Type="Graphics:FontDescription"> <FontName>Tahoma</FontName> <Size>20</Size> <Spacing>0</Spacing> <UseKerning>true</UseKerning> <Style>Regular</Style> <CharacterRegions> <CharacterRegion> <Start> </Start> <End>~</End> </CharacterRegion> <CharacterRegion> <Start>À</Start> <End>Ã</End> </CharacterRegion> <CharacterRegion> <Start>Ạ</Start> <End>ỹ</End> </CharacterRegion> <CharacterRegion> <Start>È</Start> <End>Í</End> </CharacterRegion> <CharacterRegion> <Start>Ò</Start> <End>Õ</End> </CharacterRegion> <CharacterRegion> <Start>Ù</Start> <End>Ú</End> </CharacterRegion> <CharacterRegion> <Start>à</Start> <End>ã</End> </CharacterRegion> <CharacterRegion> <Start>Ý</Start> <End>Ý</End> </CharacterRegion> <CharacterRegion> <Start>è</Start> <End>ê</End> </CharacterRegion> <CharacterRegion> <Start>ì</Start> <End>í</End> </CharacterRegion> <CharacterRegion> <Start>ò</Start> <End>õ</End> </CharacterRegion> <CharacterRegion> <Start>ù</Start> <End>ù</End> </CharacterRegion> <CharacterRegion> <Start>ú</Start> <End>ú</End> </CharacterRegion> <CharacterRegion> <Start>ý</Start> <End>ý</End> </CharacterRegion> <CharacterRegion> <Start>Ă</Start> <End>ă</End> </CharacterRegion> <CharacterRegion> <Start>Đ</Start> <End>đ</End> </CharacterRegion> <CharacterRegion> <Start>Ĩ</Start> <End>ĩ</End> </CharacterRegion> <CharacterRegion> <Start>Ũ</Start> <End>ũ</End> </CharacterRegion> <CharacterRegion> <Start>Ơ</Start> <End>ơ</End> </CharacterRegion> <CharacterRegion> <Start>Ư</Start> <End>ư</End> </CharacterRegion> </CharacterRegions> </Asset></XnaContent>
giờ thì đoạn code Game1.cs ở trên chạy mượt, đã draw tiếng việt thoải mái. bạn có thể chọn font ở thẻ <FontName>Tahoma</FontName> của file font
kết quả :
Vệ sinh cá nhân là một phần quan yếu của sức khỏe nam giới. Dùng dung dịch vệ sinh mỗi ngày mang lại nhiều ích, giúp loại bỏ vi khuẩn và vi sinh vật gây hại, giúp khu vực cơ quan sinh dục luôn sạch...
Gel vệ sinh cu, bạn trai có nên...