如何使文本框中的字体上下左右都居中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使文本框中的字体上下左右都居中相关的知识,希望对你有一定的参考价值。

如何使文本框中的字体上下左右都居中

左右居中:text-align:center;上下居中:line-height设成文本框的高度。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.w
height:25px;
text-align:center;
line-height:25px;

</style>
</head>

<body>
<input type="text" name="textfield" id="textfield" value="1111" class="w"/>
</body>
</html>
参考技术A 左右居中:text-align:center;上下居中:line-height设成文本框的高度。
<!DOCTYPE
html
PUBLIC
"-//W3C//DTD
XHTML
1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=utf-8"
/>
<title>无标题文档</title>
<style>
.w
height:25px;
text-align:center;
line-height:25px;

</style>
</head>
<body>
<input
type="text"
name="textfield"
id="textfield"
value="1111"
class="w"/>
</body>
</html>
参考技术B 左右居中:text-align:center;上下居中:line-height设成文本框的高度。
<!DOCTYPE
html
PUBLIC
"-//W3C//DTD
XHTML
1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=utf-8"
/>
<title>无标题文档</title>
<style>
.w
height:25px;
text-align:center;
line-height:25px;

</style>
</head>
<body>
<input
type="text"
name="textfield"
id="textfield"
value="1111"
class="w"/>
</body>
</html>
参考技术C 只有水平居中这个功能,要想垂直居中还得自己调,如太靠上了就敲回车换行向下走

相对于文本框中的文本块垂直居中控件

【中文标题】相对于文本框中的文本块垂直居中控件【英文标题】:Centering control vertically in relation to chunk of text in textbox 【发布时间】:2015-02-23 22:10:24 【问题描述】:

我有一个文本框,其中包含一些 html 元素,每个元素都显示为单独的文本块。现在我想添加一些图片框,每个元素一个,垂直居中在其对应元素的左侧(如果是嵌套元素,例如示例中div内的p,div应该优先,p应该被忽略) .

到目前为止,我提出的代码显然缺少一些东西,因为 Y 坐标不正确。见下图,其中两个黑色斑点代表两个图片框:

这是代码:

string str = textBox1.Text;
string pattern = "<(?<tag>div|p|h[1-6])>";
Regex r = new Regex(pattern);
Match m = r.Match(str);
int i = 1;
while (m.Success) 
    string new_s = str.Substring(m.Index + 3);
    string new_p = "</" + m.Groups["tag"].Value + ">";
    Match m_end = Regex.Match(new_s, new_p);
    if (m_end.Success)  // Corresponding end tag exists
        Point start_p = textBox1.GetPositionFromCharIndex(m.Index);
        Point end_p = textBox1.GetPositionFromCharIndex(m_end.Index);
        double top = start_p.Y;
        double bottom = end_p.Y;
        int midpoint = (int)(top + bottom) / 2;
        PictureBox pictureBox = new PictureBox();
        pictureBox.Name = "pictureBox" + i.ToString();
        pictureBox.Location = new System.Drawing.Point(15, midpoint + 7);
        pictureBox.Image = Image.FromFile("c:\\blob.png");
        pictureBox.Size = new Size(15, 15);
        pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
        this.Controls.Add(pictureBox);
        m_end.Index.ToString());
    
    i++;
    m = r.Match(str, m_end.Index);

我也尝试将 textBox1.Font.Size 添加到等式中,但这没有帮助。而且行高很困难,因为从技术上讲,每个块都是一行。有关如何进行的任何建议?

编辑:由于 Point 基于左上角,因此使用 Font.Size 查找底部似乎是合理的。这似乎为第一个 blob 提供了正确的位置。

【问题讨论】:

【参考方案1】:

我想我找到了解决方案。基本上,由于每次搜索新标签时都会缩短输入字符串,因此如果我不添加从搜索字符串中删除的字符数,则结束标签位置会出错。或者在代码中,希望更清楚一点:

while (m.Success) 
    string new_s = str.Substring(m.Index);
    string new_p = "</" + m.Groups["tag"].Value + ">";
    Match m_end = Regex.Match(new_s, new_p);
    Point start_p = textBox1.GetPositionFromCharIndex(m.Index);
    Point end_p = textBox1.GetPositionFromCharIndex(m_end.Index + m.Index);
    int top = (int)start_p.Y;
    int bottom = (int)(end_p.Y + textBox1.Font.Size);
    if (m_end.Success) 
        int midpoint = (top + bottom) / 2;
        PictureBox pictureBox = new PictureBox();
        pictureBox.Name = "pictureBox" + i.ToString();
        pictureBox.Location = new System.Drawing.Point(15, midpoint + 7 + 2);
        pictureBox.Image = Image.FromFile("c:\\blob.png");
        pictureBox.Size = new Size(15, 15);
        pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
        this.Controls.Add(pictureBox);
    
    i++;
    m = r.Match(str, m_end.Index + m.Index);

【讨论】:

以上是关于如何使文本框中的字体上下左右都居中的主要内容,如果未能解决你的问题,请参考以下文章

如何让Word文本框中的文字垂直上下居中

word里怎么让文本框里字体上下居中呢?

如何让word文本框中的文字垂直上下居中

VB。如何把 TEXT 文本框中选中的字体 变成想要的颜色

怎么让文字上下居中

css html 如何让div里边的图片和文字同时上下居中?