gpt4 book ai didi

c# - 为 RichTextBox 决定 FontStyle(粗体、斜体、下划线)

转载 作者:行者123 更新时间:2023-11-30 22:18:23 29 4
gpt4 key购买 nike

究竟如何更改 RichTextBox 中的字体?

环顾四周给了我似乎不再有效的旧答案。我认为这就像执行 richtextbox1.Font = Font.Bold; 或类似操作一样简单。原来不是,所以我环顾四周。显然,您必须更改 FontStyle 这是一个 readonly (??) 属性,但您必须创建一个新的 FontStyle 对象。

但即使这样也行不通o.o

你是怎么做到的?编辑:

似乎不起作用:\

            rssTextBox.Document.Blocks.Clear();
rssTextBox.FontWeight = FontWeights.Bold;
rssTextBox.AppendText("Title: ");
rssTextBox.FontWeight = FontWeights.Normal;
rssTextBox.AppendText(rs.Title + "\n");
rssTextBox.FontWeight = FontWeights.Bold;
rssTextBox.AppendText("Publication Date: ");
rssTextBox.FontWeight = FontWeights.Normal;
rssTextBox.AppendText(rs.PublicationDate + "\n");
rssTextBox.FontWeight = FontWeights.Bold;
rssTextBox.AppendText("Description: ");
rssTextBox.FontWeight = FontWeights.Normal;
rssTextBox.AppendText(rs.Description + "\n\n");

最佳答案

Bold 是一个 FontWeight。您可以直接应用它。

作为MSDN Doc声明“获取或设置指定字体的粗细或粗细。”

你可以在xaml中设置

<RichTextBox FontWeight="Bold" x:Name="richText" />

或在代码隐藏中:

richText.FontWeight = FontWeights.Bold;

如果您尝试切换 FontFamily,那将是这样的:

richText.FontFamily = new FontFamily("Arial");

字体样式:

richText.FontStyle = FontStyles.Italic;

更新:(用于内联更新RichTextBox)

这只是一个快速模型。以此为例。请根据您的要求构建它。

richText.Document.Blocks.Clear();
Paragraph textParagraph = new Paragraph();
AddInLineBoldText("Title: ", ref textParagraph);
AddNormalTextWithBreak(rs.Title, ref textParagraph);
AddInLineBoldText("Publication Date: ", ref textParagraph);
AddNormalTextWithBreak(rs.PublicationDate, ref textParagraph);
AddInLineBoldText("Description: ", ref textParagraph);
AddNormalTextWithBreak(rs.Description, ref textParagraph);
AddNormalTextWithBreak("", ref textParagraph);
richText.Document.Blocks.Add(textParagraph);

private static void AddInLineBoldText(string text, ref Paragraph paragraph) {
Bold myBold = new Bold();
myBold.Inlines.Add(text);
paragraph.Inlines.Add(myBold);
}

private static void AddNormalTextWithBreak(string text, ref Paragraph paragraph) {
Run myRun = new Run {Text = text + Environment.NewLine};
paragraph.Inlines.Add(myRun);
}

关于c# - 为 RichTextBox 决定 FontStyle(粗体、斜体、下划线),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16125870/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com