【问题标题】:Write to the left and right side of textview写入textview的左右两边
【发布时间】:2018-03-31 06:32:45
【问题描述】:

我希望能够同时写入文本字段的左侧和右侧,具体取决于消息是从用户还是服务器发送的。

到目前为止,我有这个:

if (Equals(first, Name))// checks if im the one sending message 
{
    BeginInvokeOnMainThread(delegate
    {                            
        OutgoingText.Text += StateObject.Response + "\n";
        OutgoingText.TextAlignment = UITextAlignment.Left;
    });
}
else
{
    BeginInvokeOnMainThread(delegate
    {
        OutgoingText.Text += StateObject.Response + "\n";
        OutgoingText.TextAlignment = UITextAlignment.Right;
    });
}

不幸的是,Alignment.RightAlignment.Left 将文本字段的全部内容移了过来。我想这个问题有很多解决方案,但我才刚刚开始,所以越简单越好。

【问题讨论】:

  • “文本字段的左侧和右侧”是什么意思? OutgoingText 对象的类型是什么?也许您只需要两个字段,一个左侧和一个右侧,其中一个或两个都可以为空?

标签: c# styling text-alignment


【解决方案1】:

改编自这里:Color different parts of a RichTextBox string

您可以扩展 Richtextbox 类并创建一个接受对齐参数的方法。它可能看起来像这样:

public static class RichTextBoxExtensions
{
    public static void AppendText(this RichTextBox box, string text, HorizontalAlignment align)
    {
        box.SelectionStart = box.TextLength;
        box.SelectionLength = 0;

        box.SelectionAlignment = align;
        box.AppendText(text);
    }
}

你可以这样使用:

richTextBox1.AppendText("lasdflaksjdflakjsdf\n", HorizontalAlignment.Left);
richTextBox1.AppendText("lasdflaksjdflakjsdf\n", HorizontalAlignment.Right);

【讨论】:

    猜你喜欢
    • 2020-09-22
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    相关资源
    最近更新 更多