如何使文本/标签平滑?

How to make text/labels smooth?(如何使文本/标签平滑?)

本文介绍了如何使文本/标签平滑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何使标签或文本更平滑吗?目前,它们看起来很参差不齐.由于我想让标签动态化,我不能只从 Photoshop 中插入文本.

Does anyone know how to make labels, or text, smoother? At the moment they look quite jagged. As I want to make the label dynamic, I can't just insert the text from Photoshop.

推荐答案

如果你想对文本进行抗锯齿处理,就必须动态生成代表文本的图像.这是一个关于 msdn 的示例:http://msdn.microsoft.com/en-我们/图书馆/a619zh6z.aspx

You'll have to dynamically generate images representing your text if you want to anti-alias it. Here is an example on msdn: http://msdn.microsoft.com/en-us/library/a619zh6z.aspx

根据下面的评论进行编辑.

该链接描述了使用控件的 OnPaint 事件来使用不同的 TextRenderingHint.如果您想要一些更可重用的东西,您可以做的是创建一个扩展 Label 类的自定义标签类,并在您的表单中使用它:

The link describes using the OnPaint event of your control to use a different TextRenderingHint. If you're wanting something a little more re-useable what you can do is create a Custom Label class that extends the Label class, and use this in your forms:

public partial class CustomLabel : Label
{
    private TextRenderingHint _hint = TextRenderingHint.SystemDefault;   
    public TextRenderingHint TextRenderingHint
    {
        get { return this._hint; }
        set { this._hint = value; }
    }        

    protected override void OnPaint(PaintEventArgs pe)
    {            
        pe.Graphics.TextRenderingHint = TextRenderingHint;
        base.OnPaint(pe);
    }
}

添加一个名为 CustomLabel 的新自定义控件(或您想调用的任何名称)并使用上面的代码.重建您的项目,然后您应该会看到 CustomLabel 控件出现在顶部的MyProject Components"类别下的工具箱中.在此自定义标签的属性窗格中,您将看到新的 TextRenderingHint 属性.将此设置为AntiAlias".为您的表单添加另一个标签并比较它们的外观.

Add a new Custom Control called CustomLabel (or whatever you want to call it) and use the code above. Rebuild your project and you should then see the CustomLabel control appear in your toolbox at the top, under the "MyProject Components" category. In the properties pane for this custom label you'll see the new TextRenderingHint property. Set this to "AntiAlias." Add another label to your form and compare how they look.

如果您想将其默认为 AntiAlias,只需更改私有变量的默认值即可.

If you want to default it to AntiAlias simply change the default value of the private variable.

这篇关于如何使文本/标签平滑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使文本/标签平滑?

基础教程推荐