简体   繁体   English

如何在 Gtk# 中做粗体、斜体、下划线按钮,又名如何在 gtk 中动态格式化文本

[英]How to do Bold, Italic, Underline buttons in Gtk# aka How to dynamically format text in gtk

Maybe this is a little to broad or vague of a question.也许这是一个有点宽泛或模糊的问题。

I can't find anything that makes sense to me online, I am working with Gtk2.0 and C#, and every reference on how to do this online, is either in a different language and just seems to be a list of names of functions, or only has examples of formatting text in a textview while generating the text from code (Ie, make a sentence "Hello World." then make the hello bold . It seems that no one is talking about how to do this, and it seems like a pretty fundamental bit of functionality.我在网上找不到任何对我有意义的东西,我正在使用 Gtk2.0 和 C#,关于如何在线执行此操作的每个参考都是使用不同的语言,似乎只是函数名称列表,或者只有在从代码生成文本时在 textview 中格式化文本的示例(即,创建一个句子“Hello World。”然后将 hello 设为粗体。似乎没有人在谈论如何做到这一点,而且似乎就像一个非常基本的功能。

It's perfectly easy to make some text bold, for example:将某些文本设为粗体非常容易,例如:

protected void Command_bold(object sender, EventArgs e)
{
    if (selectedTextView.Buffer.GetSelectionBounds(out A, out B))
    {
        selectedTextView.Buffer.ApplyTag("bold", A, B);
    }
}

But, when I trigger this function with a button, it only makes things bold (or italics... etc).但是,当我用一个按钮触发这个功能时,它只会使事情变得粗体(或斜体......等)。 Normal functionality of a bold button would make all of the selection bold if some or none of the selection is already bold, or make it all not bold if all of it is bold.粗体按钮的正常功能会在某些选择或没有选择已经是粗体的情况下使所有选择加粗,或者如果所有选择都为粗体则使其全部不加粗。

So, how do you detect in a Gtk TextView object, if your text is bold already or not?那么,您如何在 Gtk TextView 对象中检测您的文本是否已经加粗?

Ok, so I figured it out.好的,所以我想通了。 More or less.或多或少。 The logic below is flawed but this is a way to detect tags and control the logic of a bold button:下面的逻辑有缺陷,但这是一种检测标签和控制粗体按钮逻辑的方法:

protected void Action_Bold(object sender, EventArgs e)
{
    TextIter iA, A, B;
    bool isBold = false;
    if (selectedTextView.Buffer.GetSelectionBounds(out A, out B))
    {
        iA = A;
        while (iA.Compare(B) < 0)
        {
            foreach (TextTag tag in A.Tags)
            {
                if (tag.Name == "bold") isBold = true;
            }

            iA.ForwardChar();

        }

        if (isBold == true)
        {
            selectedTextView.Buffer.RemoveTag("bold", A, B);
        }
        else
        {
            selectedTextView.Buffer.ApplyTag("bold", A, B);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM