Exemplo n.º 1
0
        /// <summary>
        /// 设置调整内容控件的大小
        /// </summary>
        /// <param name="rich"></param>
        /// <param name="str">文本内容</param>
        private void SetFormat(ref RichTextBox rich, string str)
        {
            //❤🍀🍎☎🔥👽🎒
            label1.Font = rich.Font;
            //将表情占位符替换为与表情基本等宽的字符

            Regex rx = new Regex(@"<[0-9]{1,2}>");

            while (rx.IsMatch(str))
            {
                Match  m    = rx.Match(str);
                string text = m.Value;
                int    num  = int.Parse(text.Trim('<').Trim('>'));
                str = str.Remove(m.Index, m.Length);
                str = str.Insert(m.Index, ti);
                if (num <= 70 && num > 0)
                {
                    this.images.Add(this.We ? Method.ReadExpressionImage("b" + num) : Method.ReadExpressionImage("w" + num));
                }
            }

            label1.Text = str;
            rich.Text   = str;
            rich.Size   = new Size(label1.Width, rich.Lines.Length > 1 ? label1.Height + 15 : label1.Height);

            if (this.inPub)
            {
                Regex re = new Regex(@"\@.{1,6} ");
                for (int i = 0; i < rich.Lines.Length; i++)
                {
                    //获取<i行的文本字符总数
                    int bits = 0;
                    for (int j = 0; j < i; j++)
                    {
                        bits += rich.Lines[j].Length;
                    }

                    foreach (Match ms in re.Matches(rich.Lines[i]))
                    {
                        rich.Select(i > 0 ? ms.Index + bits + i : ms.Index, ms.Length);
                        rich.SelectionFont  = new Font("微软雅黑", 10, FontStyle.Bold, GraphicsUnit.Point);
                        rich.SelectionColor = this.We ? Color.Yellow : Color.DeepSkyBlue;
                    }
                }
            }
            Debug.WriteLine("行数:" + rich.Lines.Length);
        }