设想
我想在WP7上使用Glyphs
创建合理的文本行,即触摸周围矩形的左右边界。
我的解决方案
var glyphs = new Glyphs();
glyphs.FontUri = new Uri("/MyAssembly;component/MyPath/MyFont.ttf", UriKind.Relative);
glyphs.FontRenderingEmSize = 20;
glyphs.Fill = new SolidColorBrush(Colors.Red);
// measue width of space
glyphs.UnicodeString = " ";
glyphs.Measure(availableSize);
double spaceWidth = glyphs.DesiredSize.Width;
glyphs.InvalidateMeasure();
// setup justified text
string text = "Lorem Ipsum is dummy text of the printing and typesetting industry.";
int spaceCount = 10; // number of spaces in above text
glyphs.UnicodeString = text;
glyphs.Measure(availableSize); // now DesiredSize.Width = width of left aligned text
// I suspect my error to be in this formula:
double spaceAdvance = ((availableSize.Width - glyphs.DesiredSize.Width)
/ spaceCount + spaceWidth) / glyphs.FontRenderingEmSize * 100;
string spaceAdvanceString = String.Format(",{0};", spaceAdvance);
var indices = new StringBuilder();
foreach (char c in text)
{
if (c == ' ') indices.Append(spaceAdvanceString);
else indices.Append(';');
}
glyphs.Indices = indices.ToString();
问题与疑问
字形的右侧未完全触及
availableSize.Width
-Border,但偏离了一些像素,当堆叠了几行文本时,这看起来很奇怪。我的计算有什么问题?
最佳答案
替代方案:您可以使用支持“justify”的RichTextBox控件。
关于c# - Silverlight:字形宽度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5264537/