2013-07-15 10 views
7

बहुत सारे प्रश्न हैं (ईजी: 1, 2, 3, 4, 5) पूछें कि आप एक स्ट्रिंग को वांछित मात्रा में कैसे छंटनी कर सकते हैं। लेकिन मैं एक कंटेनर में फिट करने के लिए पाठ का एक टुकड़ा छोटा करना चाहता हूं। (आईई: पिक्सल, में वर्णों की संख्या) में इसकी चौड़ाई से स्ट्रिंग को फसल करें।एक कंटेनर में फिट करने के लिए एक स्ट्रिंग को कैसे छंटनी करें?

यह आसान अगर आप WPF का उपयोग है, लेकिन WinForms में इतना नहीं ...

तो: आप इसे कैसे एक कंटेनर में फिट करने के लिए एक स्ट्रिंग को काटना कर सकते हैं?

उत्तर

13

कोडिंग के एक दिन बाद मुझे एक समाधान मिला और मैं इसे समुदाय के साथ साझा करना चाहता था।

सबसे पहले: स्ट्रिंग या Winforms टेक्स्टबॉक्स के लिए कोई देशी कटाव फ़ंक्शन नहीं है। यदि आप एक लेबल का उपयोग करते हैं, तो आप ऑटोइलीप्सिस प्रॉपर्टी का उपयोग कर सकते हैं।

FYI करें: एक अंडाकार विराम चिह्न है कि तीन बिंदुओं से मिलकर है। आईई: ...

कारण है कि मैं इस बनाया है यही कारण है कि:

public static class Extensions 
{ 
    /// <summary> 
    /// Truncates the TextBox.Text property so it will fit in the TextBox. 
    /// </summary> 
    static public void Truncate(this TextBox textBox) 
    { 
     //Determine direction of truncation 
     bool direction = false; 
     if (textBox.TextAlign == HorizontalAlignment.Right) direction = true; 

     //Get text 
     string truncatedText = textBox.Text; 

     //Truncate text 
     truncatedText = truncatedText.Truncate(textBox.Font, textBox.Width, direction); 

     //If text truncated 
     if (truncatedText != textBox.Text) 
     { 
      //Set textBox text 
      textBox.Text = truncatedText; 

      //After setting the text, the cursor position changes. Here we set the location of the cursor manually. 
      //First we determine the position, the default value applies to direction = left. 

      //This position is when the cursor needs to be behind the last char. (Example:"…My Text|"); 
      int position = 0; 

      //If the truncation direction is to the right the position should be before the ellipsis 
      if (!direction) 
      { 
       //This position is when the cursor needs to be before the last char (which would be the ellipsis). (Example:"My Text|…"); 
       position = 1; 
      } 

      //Set the cursor position 
      textBox.Select(textBox.Text.Length - position, 0); 
     } 
    } 

    /// <summary> 
    /// Truncates the string to be smaller than the desired width. 
    /// </summary> 
    /// <param name="font">The font used to determine the size of the string.</param> 
    /// <param name="width">The maximum size the string should be after truncating.</param> 
    /// <param name="direction">The direction of the truncation. True for left (…ext), False for right(Tex…).</param> 
    static public string Truncate(this string text, Font font, int width, bool direction) 
    { 
     string truncatedText, returnText; 
     int charIndex = 0; 
     bool truncated = false; 
     //When the user is typing and the truncation happens in a TextChanged event, already typed text could get lost. 
     //Example: Imagine that the string "Hello Worl" would truncate if we add 'd'. Depending on the font the output 
     //could be: "Hello Wor…" (notice the 'l' is missing). This is an undesired effect. 
     //To prevent this from happening the ellipsis is included in the initial sizecheck. 
     //At this point, the direction is not important so we place ellipsis behind the text. 
     truncatedText = text + "…"; 

     //Get the size of the string in pixels. 
     SizeF size = MeasureString(truncatedText, font); 

     //Do while the string is bigger than the desired width. 
     while (size.Width > width) 
     { 
      //Go to next char 
      charIndex++; 

      //If the character index is larger than or equal to the length of the text, the truncation is unachievable. 
      if (charIndex >= text.Length) 
      { 
       //Truncation is unachievable! 

       //Throw exception so the user knows what's going on. 
       throw new IndexOutOfRangeException("The desired width of the string is too small to truncate to."); 
      } 
      else 
      { 
       //Truncation is still applicable! 

       //Raise the flag, indicating that text is truncated. 
       truncated = true; 

       //Check which way to text should be truncated to, then remove one char and add an ellipsis. 
       if (direction) 
       { 
        //Truncate to the left. Add ellipsis and remove from the left. 
        truncatedText = "…" + text.Substring(charIndex); 
       } 
       else 
       { 
        //Truncate to the right. Remove from the right and add the ellipsis. 
        truncatedText = text.Substring(0, text.Length - charIndex) + "…"; 
       } 

       //Measure the string again. 
       size = MeasureString(truncatedText, font); 
      } 
     } 

     //If the text got truncated, change the return value to the truncated text. 
     if (truncated) returnText = truncatedText; 
     else returnText = text; 

     //Return the desired text. 
     return returnText; 
    } 

    /// <summary> 
    /// Measures the size of this string object. 
    /// </summary> 
    /// <param name="text">The string that will be measured.</param> 
    /// <param name="font">The font that will be used to measure to size of the string.</param> 
    /// <returns>A SizeF object containing the height and size of the string.</returns> 
    static private SizeF MeasureString(String text, Font font) 
    { 
     //To measure the string we use the Graphics.MeasureString function, which is a method that can be called from a PaintEventArgs instance. 
     //To call the constructor of the PaintEventArgs class, we must pass a Graphics object. We'll use a PictureBox object to achieve this. 
     PictureBox pb = new PictureBox(); 

     //Create the PaintEventArgs with the correct parameters. 
     PaintEventArgs pea = new PaintEventArgs(pb.CreateGraphics(), new System.Drawing.Rectangle()); 
     pea.Graphics.PageUnit = GraphicsUnit.Pixel; 
     pea.Graphics.PageScale = 1; 

     //Call the MeasureString method. This methods calculates what the height and width of a string would be, given the specified font. 
     SizeF size = pea.Graphics.MeasureString(text, font); 

     //Return the SizeF object. 
     return size; 
    } 
} 

उपयोग: यह एक वर्ग आप कॉपी और नाम स्थान है कि अपने WinForms रूप में शामिल है में पेस्ट कर सकते हैं। सुनिश्चित करें कि आप "सिस्टम का उपयोग कर शामिल हैं। ड्रॉइंग;"

इस श्रेणी में दो एक्सटेंशन विधियां हैं, जिन्हें ट्रंकेट कहा जाता है। मूल रूप से आप अब ऐसा कर सकते हैं:

public void textBox1_TextChanged(object sender, EventArgs e) 
{ 
    textBox1.Truncate(); 
} 

अब आप textBox1 में कुछ भी लिख सकते हैं और यदि आवश्यक हो, तो वह आपकी स्ट्रिंग पाठ बॉक्स में फिट करने के लिए काटना होगा और यह एक अंडाकार जोड़ देगा।

अवलोकन: इस वर्ग वर्तमान में 3 तरीकों में शामिल हैं:

  1. काटना (टेक्स्ट बॉक्स की अधिकतम विस्तार)
  2. काटना (स्ट्रिंग के लिए विस्तार)
  3. MeasureString

ट्रंकेट (टेक्स्टबॉक्स के लिए विस्तार)

यह विधि स्वचालित रूप से TextBox.Text संपत्ति को छोटा कर देगी। छंटनी की दिशा TextAlign संपत्ति द्वारा रोकथाम है। (ईजी: "बाएं संरेखण के लिए छंटनी ...", "... सही संरेखण के लिए ncation"।) कृपया ध्यान दें: इस विधि को हिब्रू या अरबी जैसे अन्य लेखन प्रणालियों के साथ काम करने के लिए कुछ बदलाव करने की आवश्यकता हो सकती है।


काटना (स्ट्रिंग के लिए विस्तार)

आदेश इस विधि का उपयोग करने के लिए आपके पास दो पैरामीटर भेजने चाहिए: एक फ़ॉन्ट और एक वांछित चौड़ाई। फ़ॉन्ट का उपयोग स्ट्रिंग की चौड़ाई की गणना करने के लिए किया जाता है और वांछित चौड़ाई का उपयोग छंटनी के बाद अधिकतम चौड़ाई के रूप में किया जाता है।


इस विधि कोड स्निपेट में निजी है MeasureString। तो यदि आप इसका उपयोग करना चाहते हैं, तो आपको इसे पहले सार्वजनिक रूप से बदलना होगा। इस विधि का उपयोग पिक्सल में स्ट्रिंग की ऊंचाई और चौड़ाई को मापने के लिए किया जाता है। इसके लिए दो पैरामीटर की आवश्यकता होती है: पाठ को मापने के लिए और पाठ का फ़ॉन्ट।

मुझे उम्मीद है कि मैंने इसके साथ किसी की मदद की है। शायद ऐसा करने का एक और तरीका है, मुझे this हंस पासेंट द्वारा उत्तर दिया गया, जो टूलटिपस्टैटस लेबल को छोटा करता है, जो काफी प्रभावशाली है। मेरे .NET कौशल हंस पासेंट के पास कहीं भी नहीं हैं इसलिए मैंने उस कोड को टेक्स्टबॉक्स जैसे कुछ के साथ काम करने में कामयाब नहीं किया है ... लेकिन अगर आपने सफल किया है, या कोई अन्य समाधान है तो मुझे इसे देखना अच्छा लगेगा! :)

+1

+1 आपके समाधान पर कुछ अविश्वसनीय दस्तावेज़ीकरण के लिए। लेकिन, यह दृष्टिकोण सचमुच 'टेक्स्टबॉक्स 'के पाठ को बदल देता है। यह निश्चित रूप से 'टैग' या कुछ अन्य गुणों के माध्यम से प्रबंधित किया जा सकता है, लेकिन हंस का जवाब बस 'पेंट' प्रक्रिया को ओवरराइड करता है। यह अकेले 'टेक्स्ट' छोड़ देता है लेकिन उपयोगकर्ता को * भ्रम * के साथ प्रदान करता है कि पाठ को छोटा कर दिया गया था। –

+0

@ माइकलपेरनॉउड जो वास्तव में उल्लेखनीय है। और यही कारण है कि मैंने हंस पासेंट्स का जवाब दिया। यह सिर्फ अधिक सुरुचिपूर्ण है :) – Jordy

+1

हालांकि, एक शोध के बाद, एक नोट है कि आप वास्तव में किसी टेक्स्ट बॉक्स पर कुछ भी आकर्षित नहीं कर सकते हैं। तो मैं आपको जो चाहिए उसे कहूंगा, आपने सबसे अच्छा किया है। बढ़िया काम! –

0

मैंने जॉर्डी के कोड का परीक्षण किया और इसके परिणाम के साथ परिणाम की तुलना की, इसमें कोई फर्क नहीं पड़ता, वे दोनों ठीक से ट्रिम/छंटनी करते हैं लेकिन कुछ मामलों में अच्छी तरह से नहीं, यह MeasureString() द्वारा मापा गया आकार हो सकता है सटीक नहीं मुझे पता है कि यह कोड सिर्फ एक सरलीकृत संस्करण है, अगर मैं इसके बारे में परवाह करता है और इसका उपयोग करता है, तो मैं इसे यहां पोस्ट करता हूं क्योंकि यह छोटा है और मैंने परीक्षण किया है: इसमें कोई फर्क नहीं पड़ता कि यह कोड जॉर्डी के कोड की तुलना में स्ट्रिंग को ट्रिम/टंक कैसे कर सकता है, बेशक उसका कोड किसी प्रकार का पूर्ण संस्करण है जिसमें 3 विधियां समर्थित हैं।

public static class TextBoxExtension 
{ 
    public static void Trim(this TextBox text){    
     string txt = text.Text; 
     if (txt.Length == 0 || text.Width == 0) return; 
     int i = txt.Length;    
     while (TextRenderer.MeasureText(txt + "...", text.Font).Width > text.Width)    
     { 
      txt = text.Text.Substring(0, --i); 
      if (i == 0) break; 
     } 
     text.Text = txt + "..."; 
    } 
    //You can implement more methods such as receiving a string with font,... and returning the truncated/trimmed version. 
} 
संबंधित मुद्दे