2008-08-14 17 views
18

में टेक्स्ट स्वरूपण करना क्या टेक्स्ट को एकाधिक लेबल में तोड़ने के बजाय WinForm लेबल में कुछ टेक्स्ट स्वरूपित करना संभव है? लेबल के पाठ में HTML टैग को अवहेलना करें; यह केवल मेरे बिंदु को पाने के लिए प्रयोग किया जाता है।WinForm लेबल

उदाहरण के लिए:

Dim myLabel As New Label 
myLabel.Text = "This is <b>bold</b> text. This is <i>italicized</i> text." 

कौन सा रूप में लेबल में पाठ का उत्पादन होगा:

यह वह जगह है बोल्ड पाठ। यह इटालिकिस टेक्स्ट है।

+0

ब्रायन, अगर आप # 2 या डैनी के जवाब की # 3, आप कोड या करने के लिए एक लिंक साझा बुरा इस्तेमाल किया है कि तुम क्या इस्तेमाल किया? धन्यवाद। – FastAl

+0

@FastAl मुझे विश्वास है कि मैं अभी # 1 का उपयोग कर समाप्त हुआ लेकिन मैंने # 3 का उपयोग किया होगा। मेरे पास अब कोडबेस तक पहुंच नहीं है इसलिए मैं एक उदाहरण प्रदान नहीं कर सकता। माफ़ कीजिये। –

उत्तर

16

WinForms लेबल के साथ यह संभव नहीं है। लेबल में बिल्कुल एक फ़ॉन्ट होना चाहिए, बिल्कुल एक आकार और एक चेहरा के साथ। आप विकल्पों में से एक जोड़ी है:

  1. उपयोग अलग लेबल
  2. एक नया नियंत्रण व्युत्पन्न वर्ग के माध्यम से GDI + और उपयोग अपने स्वयं के ड्राइंग करता बनाएं कि लेबल के बजाय; यह शायद आपका सबसे अच्छा विकल्प है, क्योंकि यह आपको अपने पाठ को प्रारूपित करने के लिए नियंत्रण को निर्देशित करने के तरीके पर पूर्ण नियंत्रण देता है
  3. किसी तृतीय-पक्ष लेबल नियंत्रण का उपयोग करें जो आपको HTML स्निपेट्स (एक गुच्छा - चेक कोडप्रोजेक्ट) डालने देगा; यह # 2 का कोई और कार्यान्वयन होगा।
1

मुझे यह पता लगाने में भी दिलचस्पी होगी कि यह संभव है या नहीं।

जब हमें कोई समाधान नहीं मिला, तो हमने घटक ऑनस 'सुपरलेबेल' नियंत्रण का सहारा लिया जो लेबल में HTML मार्कअप की अनुमति देता है।

14

वास्तव में नहीं, लेकिन आप सीमाओं के बिना केवल पढ़ने के लिए RichTextBox के साथ इसे नकली बना सकते हैं। RichTextBox रिच टेक्स्ट फॉर्मेट (आरटीएफ) का समर्थन करता है।

5
  1. वर्डपैड में एक RTF फ़ाइल के रूप में पाठ बनाएँ
  2. कोई सीमाओं और साथ रिच टेक्स्ट नियंत्रण बनाएं संपादन योग्य = false
  3. Form1_Load में एक संसाधन के रूप में इस परियोजना के लिए RTF फ़ाइल जोड़े
  4. है

    myRtfControl.Rtf = Resource1.MyRtfControlText

+0

वर्डपैड उपयोग को अच्छी बात बताएं! छोटी जटिलता: यह बहुत सारे कचरे को उत्पन्न करता है जो परिणामी आरटीएफ की समझ को थोड़ा कठिन बनाता है (लेकिन सुरक्षित रूप से हटाया जा सकता है) ... आरटीएफ "spec" पढ़ने से बेहतर है। – sharpener

+0

@sharpener आप rtf कि एमएस वर्ड का उत्पादन ... बस वर्डपैड में है कि खोलने और दबाने "बचाने" देखना चाहिए कम कर देता है कि बेहद, कभी कभी भी मूल फ़ाइल आकार का सिर्फ 10% करने के लिए नीचे। और नहीं, मैं केवल _wish_ मैं मजाक कर रहा था या वहां अतिरंजित कर रहा था X_x – Nyerguds

1

फिर 200 9 से कोड प्रोजेक्ट पर "A Professional HTML Renderer You Will Use" नामक एक उत्कृष्ट लेख है जो मूल पोस्टर के समान कुछ करता है।

मैं इसे कई परियोजनाओं के भीतर सफलतापूर्वक उपयोग करता हूं।

0

यह समझना एक पुराना सवाल है, मेरा जवाब उन लोगों के लिए अधिक है, जो अभी भी ऐसे समाधान ढूंढ रहे हैं और इस प्रश्न पर ठोकर खा रहे हैं।

अलावा क्या पहले से ही उल्लेख किया गया था से, DevExpress के LabelControl एक लेबल इस व्यवहार का समर्थन करता है - demo here। हां, यह एक भुगतान पुस्तकालय का हिस्सा है।

यदि आप मुफ्त समाधान की तलाश में हैं, तो मेरा मानना ​​है कि HTML Renderer अगली सबसे अच्छी बात है।

+0

जब हमने DevExpress 'जेनेरिक नियंत्रणों का उपयोग किया, तो उन्हें थीमिंग के साथ भारी समस्याएं होती थीं। जैसे, उन्हें कुछ क्वियर थीम का उपयोग करना आसान है, लेकिन उन्हें वर्तमान ओएस की तरह दिखाना मुश्किल है। – hypersw

2

बहुत सरल उपाय:

  1. फार्म पर 2 लेबल जोड़ें, LabelA और LabelB
  2. LabelA के लिए गुण में जाओ और छोड़ दिया करने के लिए यह गोदी।
  3. लेबलबी के लिए गुणों पर जाएं और इसे बाईं ओर भी डॉक करें।
  4. लेबलए के लिए बोल्ड को फ़ॉन्ट सेट करें।

अब लेबलबी लेबलए के पाठ की लंबाई के आधार पर स्थानांतरित होगा।

यह सब कुछ है।

9

एक और वैकल्पिक हल, पार्टी के लिए देर से: आप किसी तृतीय पक्ष नियंत्रण का उपयोग नहीं करना चाहते हैं, और अगर आप सिर्फ अपनी लेबल में पाठ के कुछ पर ध्यान आकर्षित करने के लिए देख रहे हैं और तुम ठीक कर रहे हैं अंडरलाइन के साथ, आप LinkLabel का उपयोग कर सकते हैं।

ध्यान दें कि कई लोग इसे 'usability crime' मानते हैं, लेकिन यदि आप अंतिम उपयोगकर्ता उपभोग के लिए कुछ डिज़ाइन नहीं कर रहे हैं तो यह कुछ ऐसा हो सकता है जिसे आप अपनी विवेक पर रखने के लिए तैयार हैं।

चाल आपके टेक्स्ट के उन हिस्सों में अक्षम लिंक जोड़ना है जिन्हें आप रेखांकित करना चाहते हैं, और फिर वैश्विक लेबल को शेष लेबल से मिलान करने के लिए लिंक रंग सेट करें। आप लगभग सब अलग Links.Add() टुकड़े से डिजाइन समय में आवश्यक गुण सेट कर सकते हैं, लेकिन यहाँ वे कोड में हैं:

linkLabel1.Text = "You are accessing a government system, and all activity " + 
        "will be logged. If you do not wish to continue, log out now."; 
linkLabel1.AutoSize = false; 
linkLabel1.Size = new Size(365, 50); 
linkLabel1.TextAlign = ContentAlignment.MiddleCenter; 
linkLabel1.Links.Clear(); 
linkLabel1.Links.Add(20, 17).Enabled = false; // "government system" 
linkLabel1.Links.Add(105, 11).Enabled = false; // "log out now" 
linkLabel1.LinkColor = linkLabel1.ForeColor; 
linkLabel1.DisabledLinkColor = linkLabel1.ForeColor; 

परिणाम:

enter image description here

+2

ओह, अद्भुत। मुझे कभी नहीं पता था कि लिंक लेबल में लिंक के रूप में विशिष्ट क्षेत्र हो सकते हैं। यह वास्तव में उपयोग किए जाने वाले वास्तविक प्रश्न के मुकाबले उपयोग के मामले के करीब है; मेरा लेबल वैसे भी क्लिक करने योग्य था। अभी सिर्फ एक क्लिक करने योग्य क्षेत्र के साथ बहुत अच्छा है। – Nyerguds

0

एक FlowLayoutPanel आपकी समस्या के लिए अच्छी तरह से काम । यदि आप प्रवाह पैनल में लेबल जोड़ते हैं और प्रत्येक लेबल के फ़ॉन्ट और मार्जिन गुणों को प्रारूपित करते हैं, तो आपके पास अलग-अलग फ़ॉन्ट शैलियों हो सकती हैं। काम करने के लिए बहुत तेज़ और आसान समाधान।

6

कस्टम RichEditBox का उपयोग करके - मेरे लिए समाधान समाधान। सही गुणों के साथ इसे बोल्ड सपोर्ट के साथ सरल लेबल के रूप में देखा जाएगा।

1) पहले, विकलांग कैरट के साथ अपने कस्टम RichTextLabel वर्ग जोड़ें:

public class RichTextLabel : RichTextBox 
{ 
    public RichTextLabel() 
    { 
     base.ReadOnly = true; 
     base.BorderStyle = BorderStyle.None; 
     base.TabStop = false; 
     base.SetStyle(ControlStyles.Selectable, false); 
     base.SetStyle(ControlStyles.UserMouse, true); 
     base.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 

     base.MouseEnter += delegate(object sender, EventArgs e) 
     { 
      this.Cursor = Cursors.Default; 
     }; 
    } 

    protected override void WndProc(ref Message m) 
    { 
     if (m.Msg == 0x204) return; // WM_RBUTTONDOWN 
     if (m.Msg == 0x205) return; // WM_RBUTTONUP 
     base.WndProc(ref m); 
    } 
} 

2) स्प्लिट आप IsSelected ध्वज के साथ शब्द, कि निर्धारित करते हैं कि शब्द बोल्ड या नहीं होना चाहिए करने के लिए सजा:

 private void AutocompleteItemControl_Load(object sender, EventArgs e) 
    { 
     RichTextLabel rtl = new RichTextLabel(); 
     rtl.Font = new Font("MS Reference Sans Serif", 15.57F); 
     StringBuilder sb = new StringBuilder(); 
     sb.Append(@"{\rtf1\ansi "); 
     foreach (var wordPart in wordParts) 
     { 
      if (wordPart.IsSelected) 
      { 
       sb.Append(@"\b "); 
      } 
      sb.Append(ConvertString2RTF(wordPart.WordPart)); 
      if (wordPart.IsSelected) 
      { 
       sb.Append(@"\b0 "); 
      } 
     } 
     sb.Append(@"}"); 

     rtl.Rtf = sb.ToString(); 
     rtl.Width = this.Width; 
     this.Controls.Add(rtl); 
    } 

3) समारोह में परिवर्तित करने के लिए आप मान्य rtf (यूनिकोड समर्थन के साथ के लिए पाठ जोड़ने):

private string ConvertString2RTF(string input) 
    { 
     //first take care of special RTF chars 
     StringBuilder backslashed = new StringBuilder(input); 
     backslashed.Replace(@"\", @"\\"); 
     backslashed.Replace(@"{", @"\{"); 
     backslashed.Replace(@"}", @"\}"); 

     //then convert the string char by char 
     StringBuilder sb = new StringBuilder(); 
     foreach (char character in backslashed.ToString()) 
     { 
      if (character <= 0x7f) 
       sb.Append(character); 
      else 
       sb.Append("\\u" + Convert.ToUInt32(character) + "?"); 
     } 
     return sb.ToString(); 
    } 

Sample

निर्माण मेरे लिए एक आकर्षण की तरह! समाधान से संकलित:

How to convert a string to RTF in C#?

Format text in Rich Text Box

How to hide the caret in a RichTextBox?

+0

यदि आप जितना संभव हो उतना हल्का वजन रखना चाहते हैं और तृतीय पक्ष निर्भरताओं से बचना चाहते हैं तो यह सबसे अच्छा समाधान उपलब्ध है। मैंने कई विकल्पों का परीक्षण किया और यह विधि सर्वोत्तम काम करती है और हैंडल भी करती है .ऑटोसाइज और .WordWrap ठीक उसी तरह सामान्य लेबल की तरह। मैंने आरटीएफ कन्वर्टर फ़ंक्शन के लिए एक साधारण HTML में भी बनाया है जो मूल टैग (बोल्डिंग, इटालिक्स इत्यादि) के साथ सीधे प्रतिस्थापन को संभालता है, इसलिए मुझे आरटीएफ प्रारूप को याद रखने की आवश्यकता नहीं है। मैं सिर्फ HTML स्वरूपित स्ट्रिंग लोड करता हूं। –

0

हाँ। आप एचटीएमएल रेंडर का उपयोग कर लागू कर सकते हैं। आप देखने के लिए, लिंक पर क्लिक करें: https://htmlrenderer.codeplex.com/ मुझे आशा है कि यह उपयोगी है।

2

AutoRichLabel

            AutoRichLabel with formatted RTF content

मैं एक UserControl कि एक TransparentRichTextBox केवल पढ़ने के लिए है कि शामिल हैं का निर्माण करके इस समस्या को हल किया गया था। TransparentRichTextBox एक RichTextBox कि पारदर्शी होने के लिए अनुमति देता है:

TransparentRichTextBox.cs:

public class TransparentRichTextBox : RichTextBox 
{ 
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)] 
    static extern IntPtr LoadLibrary(string lpFileName); 

    protected override CreateParams CreateParams 
    { 
     get 
     { 
      CreateParams prams = base.CreateParams; 
      if (TransparentRichTextBox.LoadLibrary("msftedit.dll") != IntPtr.Zero) 
      { 
       prams.ExStyle |= 0x020; // transparent 
       prams.ClassName = "RICHEDIT50W"; 
      } 
      return prams; 
     } 
    } 
} 

अंतिम UserControlTransparentRichTextBox का आवरण के रूप में कार्य करता है। दुर्भाग्यवश, मुझे इसे अपने रास्ते पर AutoSize तक सीमित करना पड़ा, क्योंकि RichTextBox के AutoSize टूट गए।

AutoRichLabel.designer.cs:

partial class AutoRichLabel 
{ 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Component Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.rtb = new HST.SCADA.OPCServer.Config.Controls.TransparentRichTextBox(); 
     this.SuspendLayout(); 
     // 
     // rtb 
     // 
     this.rtb.BorderStyle = System.Windows.Forms.BorderStyle.None; 
     this.rtb.Dock = System.Windows.Forms.DockStyle.Fill; 
     this.rtb.Location = new System.Drawing.Point(0, 0); 
     this.rtb.Margin = new System.Windows.Forms.Padding(0); 
     this.rtb.Name = "rtb"; 
     this.rtb.ReadOnly = true; 
     this.rtb.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None; 
     this.rtb.Size = new System.Drawing.Size(46, 30); 
     this.rtb.TabIndex = 0; 
     this.rtb.Text = ""; 
     this.rtb.WordWrap = false; 
     this.rtb.ContentsResized += new System.Windows.Forms.ContentsResizedEventHandler(this.rtb_ContentsResized); 
     // 
     // AutoRichLabel 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; 
     this.BackColor = System.Drawing.Color.Transparent; 
     this.Controls.Add(this.rtb); 
     this.Name = "AutoRichLabel"; 
     this.Size = new System.Drawing.Size(46, 30); 
     this.ResumeLayout(false); 

    } 

    #endregion 

    private TransparentRichTextBox rtb; 
} 

AutoRichLabel.cs:

/// <summary> 
/// <para>An auto sized label with the ability to display text with formattings by using the Rich Text Format.</para> 
/// <para>­</para> 
/// <para>Short RTF syntax examples: </para> 
/// <para>­</para> 
/// <para>Paragraph: </para> 
/// <para>{\pard This is a paragraph!\par}</para> 
/// <para>­</para> 
/// <para>Bold/Italic/Underline: </para> 
/// <para>\b bold text\b0</para> 
/// <para>\i italic text\i0</para> 
/// <para>\ul underline text\ul0</para> 
/// <para>­</para> 
/// <para>Alternate color using color table: </para> 
/// <para>{\colortbl ;\red0\green77\blue187;}{\pard The word \cf1 fish\cf0 is blue.\par</para> 
/// <para>­</para> 
/// <para>Additional information: </para> 
/// <para>Always wrap every text in a paragraph. </para> 
/// <para>Different tags can be stacked (i.e. \pard\b\i Bold and Italic\i0\b0\par)</para> 
/// <para>The space behind a tag is ignored. So if you need a space behind it, insert two spaces (i.e. \pard The word \bBOLD\0 is bold.\par)</para> 
/// <para>Full specification: http://www.biblioscape.com/rtf15_spec.htm </para> 
/// </summary> 
public partial class AutoRichLabel : UserControl 
{ 
    /// <summary> 
    /// The rich text content. 
    /// <para>­</para> 
    /// <para>Short RTF syntax examples: </para> 
    /// <para>­</para> 
    /// <para>Paragraph: </para> 
    /// <para>{\pard This is a paragraph!\par}</para> 
    /// <para>­</para> 
    /// <para>Bold/Italic/Underline: </para> 
    /// <para>\b bold text\b0</para> 
    /// <para>\i italic text\i0</para> 
    /// <para>\ul underline text\ul0</para> 
    /// <para>­</para> 
    /// <para>Alternate color using color table: </para> 
    /// <para>{\colortbl ;\red0\green77\blue187;}{\pard The word \cf1 fish\cf0 is blue.\par</para> 
    /// <para>­</para> 
    /// <para>Additional information: </para> 
    /// <para>Always wrap every text in a paragraph. </para> 
    /// <para>Different tags can be stacked (i.e. \pard\b\i Bold and Italic\i0\b0\par)</para> 
    /// <para>The space behind a tag is ignored. So if you need a space behind it, insert two spaces (i.e. \pard The word \bBOLD\0 is bold.\par)</para> 
    /// <para>Full specification: http://www.biblioscape.com/rtf15_spec.htm </para> 
    /// </summary> 
    [Browsable(true)] 
    public string RtfContent 
    { 
     get 
     { 
      return this.rtb.Rtf; 
     } 
     set 
     { 
      this.rtb.WordWrap = false; // to prevent any display bugs, word wrap must be off while changing the rich text content. 
      this.rtb.Rtf = value.StartsWith(@"{\rtf1") ? value : @"{\rtf1" + value + "}"; // Setting the rich text content will trigger the ContentsResized event. 
      this.Fit(); // Override width and height. 
      this.rtb.WordWrap = this.WordWrap; // Set the word wrap back. 
     } 
    } 

    /// <summary> 
    /// Dynamic width of the control. 
    /// </summary> 
    [Browsable(false)] 
    public new int Width 
    { 
     get 
     { 
      return base.Width; 
     } 
    } 

    /// <summary> 
    /// Dynamic height of the control. 
    /// </summary> 
    [Browsable(false)] 
    public new int Height 
    { 
     get 
     { 
      return base.Height; 
     } 
    } 

    /// <summary> 
    /// The measured width based on the content. 
    /// </summary> 
    public int DesiredWidth { get; private set; } 

    /// <summary> 
    /// The measured height based on the content. 
    /// </summary> 
    public int DesiredHeight { get; private set; } 

    /// <summary> 
    /// Determines the text will be word wrapped. This is true, when the maximum size has been set. 
    /// </summary> 
    public bool WordWrap { get; private set; } 

    /// <summary> 
    /// Constructor. 
    /// </summary> 
    public AutoRichLabel() 
    { 
     InitializeComponent(); 
    } 

    /// <summary> 
    /// Overrides the width and height with the measured width and height 
    /// </summary> 
    public void Fit() 
    { 
     base.Width = this.DesiredWidth; 
     base.Height = this.DesiredHeight; 
    } 

    /// <summary> 
    /// Will be called when the rich text content of the control changes. 
    /// </summary> 
    private void rtb_ContentsResized(object sender, ContentsResizedEventArgs e) 
    { 
     this.AutoSize = false; // Disable auto size, else it will break everything 
     this.WordWrap = this.MaximumSize.Width > 0; // Enable word wrap when the maximum width has been set. 
     this.DesiredWidth = this.rtb.WordWrap ? this.MaximumSize.Width : e.NewRectangle.Width; // Measure width. 
     this.DesiredHeight = this.MaximumSize.Height > 0 && this.MaximumSize.Height < e.NewRectangle.Height ? this.MaximumSize.Height : e.NewRectangle.Height; // Measure height. 
     this.Fit(); // Override width and height. 
    } 
} 

रिच टेक्स्ट प्रारूप की वाक्य रचना काफी सरल है:

पैरा:

{\pard This is a paragraph!\par} 

बोल्ड/इटैलिक/रेखांकन पाठ:

\b bold text\b0 
\i italic text\i0 
\ul underline text\ul0 

वैकल्पिक रंग का उपयोग कर रंग तालिका:

{\colortbl ;\red0\green77\blue187;} 
{\pard The word \cf1 fish\cf0 is blue.\par 

लेकिन कृपया ध्यान दें: हमेशा एक पैराग्राफ में हर पाठ लपेट। साथ ही, अलग-अलग टैगों को ढेर किया जा सकता है (यानी \pard\b\i Bold and Italic\i0\b0\par) और टैग के पीछे स्पेस कैरेक्टर को अनदेखा किया जाता है। तो अगर आपको इसके पीछे एक जगह चाहिए, तो दो रिक्त स्थान डालें (यानी \pard The word \bBOLD\0 is bold.\par)। \ या { या } से बचने के लिए, कृपया एक अग्रणी \ का उपयोग करें। अधिक जानकारी के लिए full specification of the rich text format online है।

इस सरल सिंटैक्स का उपयोग करके आप कुछ छवियां बना सकते हैं जैसे कि आप पहली छवि में देख सकते हैं।रिच टेक्स्ट सामग्री है कि पहली छवि में मेरी AutoRichLabel की RtfContent संपत्ति से जुड़ा था था:

{\colortbl ;\red0\green77\blue187;} 
{\pard\b BOLD\b0 \i ITALIC\i0 \ul UNDERLINE\ul0 \\\{\}\par} 
{\pard\cf1\b BOLD\b0 \i ITALIC\i0 \ul UNDERLINE\ul0\cf0 \\\{\}\par} 

AutoRichLabel with formatted RTF content

आप शब्द रैप सक्षम करना चाहते हैं, के लिए अधिकतम चौड़ाई सेट करें एक वांछित आकार। हालांकि, यह चौड़ाई को अधिकतम चौड़ाई तक ठीक कर देगा, भले ही पाठ छोटा हो।

मज़े करो!