I have a label containing some text and I want to highlight or change the color of some words in the text of the label and not all of the words. It has to be dynamic. Any suggestions?
我有一个包含一些文字的标签,我想要突出显示或更改标签文本中某些单词的颜色,而不是所有单词。它必须是动态的。有什么建议么?
It's for c# with ASP.NET in a user control in webpart in sharepoint
它适用于带有ASP.NET的c#,位于sharepoint中的webpart中的用户控件中
7 个解决方案
#1
10
On the server-side, you could just embed some Html in your Label's text (VB):
在服务器端,您可以在Label的文本(VB)中嵌入一些Html:
myLabel.Text="Some normal text <span style='color: red;'>some red text</span>"
That's the basic mechanism, but 'dynamic' could mean a lot of things here. If you post some more details about exactly what you're doing, I might be able to help more.
这是基本机制,但“动态”可能意味着很多事情。如果你发布一些关于你正在做什么的更多细节,我可能会提供更多帮助。
One more thought: as Rob Allen pointed out, the Literal control may be a slightly better choice in this situation since it's intended to emit raw Html, whereas the Label wraps the text in a span so that the whole thing can be formatted easily.
还有一个想法:正如Rob Allen指出的那样,Literal控件在这种情况下可能是一个稍微好一点的选择,因为它打算发出原始的Html,而Label将文本包装在一个范围内,这样整个事情就可以很容易地格式化了。
Check this out for more details: *: Literals versus Labels
查看更多详细信息:*:文字与标签
For the record, depending on the situation I think a Label may actually be okay here.
为了记录,根据情况,我认为标签实际上可能在这里没问题。
#2
7
For ASP.NET,
wrap the words you want highlighted in a <span>
. Then set the <span>
style background-color
to the colour of your choice, or use a CSS class to do so.
在中包含您想要突出显示的单词。然后将样式背景颜色设置为您选择的颜色,或使用CSS类来执行此操作。
For example,
<asp:Label runat="server">
<span style="background-color:Blue;">Hello</span> World
</asp:Label>
or
<asp:Label runat="server" Text="<span style='background-color:Blue;'>Hello</span> World" />
EDIT:
If setting this in code behind, then you can do something like the following
如果在代码中设置此项,那么您可以执行以下操作
StringBuilder builder = new StringBuilder();
builder.Append([start of text]);
builder.Append("<span style=\"background-color:Blue;\">");
builder.Append([text to highlight]);
builder.Append("</span>");
builder.Append([rest of text]);
Label.Text = builder.ToString();
If you needed to match text already in the label against some specific text then something like the following
如果您需要将标签中已有的文本与某些特定文本进行匹配,则需要执行以下操作
string theTextToMatch = "[Text to match]";
string theText = Label.Text;
int leftIndex = theText.IndexOf(theTextToMatch, StringComparison.OrdinalIgnoreCase);
int rightIndex = leftIndex + theTextToMatch.Trim().Length;
StringBuilder builder = new StringBuilder();
builder.Append(theText , 0, leftIndex);
builder.Append("<span style=\"background-color:Blue;\">");
builder.Append(theText, leftIndex, rightIndex - leftIndex);
builder.Append("</span>");
builder.Append(theText, rightIndex, theText.Length - rightIndex);
Label.Text = builder.ToString();
#3
2
I made a function to look up words in a text string and highlight them with color, the result is put into a label.
我创建了一个函数来查找文本字符串中的单词并用颜色突出显示它们,结果放入标签中。
Function Remarcar(ByVal palabra As String, ByVal texto As String) As String
Dim textoNuevo As String = String.Empty
If Not String.IsNullOrEmpty(palabra) Then
Dim split As String() = texto.Split(New Char() {" "c})
For Each str As String In split
If str.ToLower.Contains(palabra.ToLower) Then
Dim a As String = String.Empty
Dim b As Int32
For i = 0 To str.Length
If str.ToLower.Substring(i, palabra.Length) = palabra.ToLower Then
a = str.Substring(i, palabra.Length)
b = i
Exit For
End If
Next
textoNuevo &= str & " "
textoNuevo = textoNuevo.Replace(str.Substring(b, palabra.Length), "<span style=""background-color:Yellow;"">" & a & "</span>")
Else
textoNuevo &= str & " "
End If
Next
Else
textoNuevo = texto
End If
Return textoNuevo
End Function
Dim texto As String = "I made a function to look up words in a text string and highlight them with color, the result is put into a label."
Label1.Text = Remarcar("highlight", texto)
#4
1
You're going to need to be a lot more specific. What language is this in? Are you building an ASP.NET web site with C# code-behind? Is this label in a Windows Form? Please provide as much detail as you can, and update the tags on your post as well.
你需要更加具体。这是什么语言?您是否正在使用C#代码隐藏构建ASP.NET网站?此标签是否为Windows窗体?请提供尽可能详细的信息,并更新帖子上的标签。
#5
1
Starting with:
<label> She sells sea shells by the sea shore </label>
We want "sells sea" to be red, and "the sea shore" to be highlighted.
我们希望“卖海”是红色,并且要突出“海岸”。
<label> She <font color="red">sea shells</font> by <font style="BACKGROUND-COLOR: yellow">the sea shore</font></label>
All done!
#6
0
If it is asp.net (since you didn't specify) you are referring to you are going have to embed the words you want to highlight in another label.
如果它是asp.net(因为你没有指定),你指的是你必须在另一个标签中嵌入你想要突出显示的单词。
<asp:label runat="server" id="nonRed">some text
<asp:label runat="server" id="redText" style="color:Red">Red Text</asp:label>
</asp:label>
#7
0
You could use a Substitution control if caching is a concern.
如果需要缓存,则可以使用Substitution控件。
<asp:Label ID="Label1" runat="server" Text="">
<asp:Substitution ID="Substitution1" runat="server" MethodName="GetDynamicLabel"/>
</asp:Label>
protected static string GetDynamicLabel( HttpContext context )
{
return string.Format( "<span style='background-color:Blue;'>{0}</span> {1}", "Blue", "Not Blue" );
}
#1
10
On the server-side, you could just embed some Html in your Label's text (VB):
在服务器端,您可以在Label的文本(VB)中嵌入一些Html:
myLabel.Text="Some normal text <span style='color: red;'>some red text</span>"
That's the basic mechanism, but 'dynamic' could mean a lot of things here. If you post some more details about exactly what you're doing, I might be able to help more.
这是基本机制,但“动态”可能意味着很多事情。如果你发布一些关于你正在做什么的更多细节,我可能会提供更多帮助。
One more thought: as Rob Allen pointed out, the Literal control may be a slightly better choice in this situation since it's intended to emit raw Html, whereas the Label wraps the text in a span so that the whole thing can be formatted easily.
还有一个想法:正如Rob Allen指出的那样,Literal控件在这种情况下可能是一个稍微好一点的选择,因为它打算发出原始的Html,而Label将文本包装在一个范围内,这样整个事情就可以很容易地格式化了。
Check this out for more details: *: Literals versus Labels
查看更多详细信息:*:文字与标签
For the record, depending on the situation I think a Label may actually be okay here.
为了记录,根据情况,我认为标签实际上可能在这里没问题。
#2
7
For ASP.NET,
wrap the words you want highlighted in a <span>
. Then set the <span>
style background-color
to the colour of your choice, or use a CSS class to do so.
在中包含您想要突出显示的单词。然后将样式背景颜色设置为您选择的颜色,或使用CSS类来执行此操作。
For example,
<asp:Label runat="server">
<span style="background-color:Blue;">Hello</span> World
</asp:Label>
or
<asp:Label runat="server" Text="<span style='background-color:Blue;'>Hello</span> World" />
EDIT:
If setting this in code behind, then you can do something like the following
如果在代码中设置此项,那么您可以执行以下操作
StringBuilder builder = new StringBuilder();
builder.Append([start of text]);
builder.Append("<span style=\"background-color:Blue;\">");
builder.Append([text to highlight]);
builder.Append("</span>");
builder.Append([rest of text]);
Label.Text = builder.ToString();
If you needed to match text already in the label against some specific text then something like the following
如果您需要将标签中已有的文本与某些特定文本进行匹配,则需要执行以下操作
string theTextToMatch = "[Text to match]";
string theText = Label.Text;
int leftIndex = theText.IndexOf(theTextToMatch, StringComparison.OrdinalIgnoreCase);
int rightIndex = leftIndex + theTextToMatch.Trim().Length;
StringBuilder builder = new StringBuilder();
builder.Append(theText , 0, leftIndex);
builder.Append("<span style=\"background-color:Blue;\">");
builder.Append(theText, leftIndex, rightIndex - leftIndex);
builder.Append("</span>");
builder.Append(theText, rightIndex, theText.Length - rightIndex);
Label.Text = builder.ToString();
#3
2
I made a function to look up words in a text string and highlight them with color, the result is put into a label.
我创建了一个函数来查找文本字符串中的单词并用颜色突出显示它们,结果放入标签中。
Function Remarcar(ByVal palabra As String, ByVal texto As String) As String
Dim textoNuevo As String = String.Empty
If Not String.IsNullOrEmpty(palabra) Then
Dim split As String() = texto.Split(New Char() {" "c})
For Each str As String In split
If str.ToLower.Contains(palabra.ToLower) Then
Dim a As String = String.Empty
Dim b As Int32
For i = 0 To str.Length
If str.ToLower.Substring(i, palabra.Length) = palabra.ToLower Then
a = str.Substring(i, palabra.Length)
b = i
Exit For
End If
Next
textoNuevo &= str & " "
textoNuevo = textoNuevo.Replace(str.Substring(b, palabra.Length), "<span style=""background-color:Yellow;"">" & a & "</span>")
Else
textoNuevo &= str & " "
End If
Next
Else
textoNuevo = texto
End If
Return textoNuevo
End Function
Dim texto As String = "I made a function to look up words in a text string and highlight them with color, the result is put into a label."
Label1.Text = Remarcar("highlight", texto)
#4
1
You're going to need to be a lot more specific. What language is this in? Are you building an ASP.NET web site with C# code-behind? Is this label in a Windows Form? Please provide as much detail as you can, and update the tags on your post as well.
你需要更加具体。这是什么语言?您是否正在使用C#代码隐藏构建ASP.NET网站?此标签是否为Windows窗体?请提供尽可能详细的信息,并更新帖子上的标签。
#5
1
Starting with:
<label> She sells sea shells by the sea shore </label>
We want "sells sea" to be red, and "the sea shore" to be highlighted.
我们希望“卖海”是红色,并且要突出“海岸”。
<label> She <font color="red">sea shells</font> by <font style="BACKGROUND-COLOR: yellow">the sea shore</font></label>
All done!
#6
0
If it is asp.net (since you didn't specify) you are referring to you are going have to embed the words you want to highlight in another label.
如果它是asp.net(因为你没有指定),你指的是你必须在另一个标签中嵌入你想要突出显示的单词。
<asp:label runat="server" id="nonRed">some text
<asp:label runat="server" id="redText" style="color:Red">Red Text</asp:label>
</asp:label>
#7
0
You could use a Substitution control if caching is a concern.
如果需要缓存,则可以使用Substitution控件。
<asp:Label ID="Label1" runat="server" Text="">
<asp:Substitution ID="Substitution1" runat="server" MethodName="GetDynamicLabel"/>
</asp:Label>
protected static string GetDynamicLabel( HttpContext context )
{
return string.Format( "<span style='background-color:Blue;'>{0}</span> {1}", "Blue", "Not Blue" );
}