tags:

views:

464

answers:

6

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

+3  A: 

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.

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();
Russ Cam
sorry for not being specific but I need it dynamic to change during runtime
Ahmad Farid
A: 

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:label runat="server" id="nonRed">some text 
        <asp:label runat="server" id="redText" style="color:Red">Red Text</asp:label>
   </asp:label>
Kevin
Nested labels? Does that work? I think that's not ideal, even if it works. Imagine the case where they have like you have, but "some text" followed by "red text" followed by "some text", both "some text"s in the outer label. What happens when they replace the text for that outer label? How do they realign "red text" into the right place. I dunno. Just gives me the willies.
Beska
sorry for not being specific but I need it dynamic to change during runtime
Ahmad Farid
works fine, it was just an example of how to do it.
Kevin
+1  A: 

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.

Matt Ball
yeah c# with ASP.NET in a user control in webpart in sharepoint
Ahmad Farid
+1  A: 

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!

Denis Sadowski
Didn't notice the .net tag. Should be asp:label rather then straight html label.
Denis Sadowski
sorry for not being specific but I need it dynamic to change during runtime
Ahmad Farid
+1  A: 

On the server-side, you could just embed some Html in your Label's text (VB):

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.

Check this out for more details: StackOverflow: Literals versus Labels

For the record, depending on the situation I think a Label may actually be okay here.

Brian MacKay
This would work a bit better in a `Literal` instead of a `Label` and also make more sense semantically
Rob Allen
A: 

You could use a Substitution control if caching is a concern.

<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" );
}
Greg