2013-02-14 8 views
6

मेरे पास एक मल्टीलाइन TextView है जो कुछ वैकल्पिक यूआरएल प्रदर्शित कर सकता है। अब मैं एक समस्या है: मेरे लंबे URL के किसी ://URLView में URL को लपेटने से कैसे रोकें?

sometext sometext http:// <-- AUTO LINE WRAP 
google.com/ 

मैं पूरी URL के लिए या कम से कम http(s):// उपसर्ग के लिए लपेटकर निष्क्रिय कर सकते हैं कैसे की स्थिति में लिपटे प्रदर्शित? हालांकि मुझे सक्षम होने के लिए अभी भी टेक्स्ट रैपिंग की आवश्यकता है।

मेरे पाठ कि

sometext sometext <-- AUTO LINE WRAP 
http://google.com/ 
+0

अपने कोड पाठ दृश्य से संबंधित हमें दिखा। –

+0

@ सौरबर्मा सिर्फ एक साधारण 'स्ट्रिंगबिल्डर' है जो कई वैकल्पिक सबस्ट्रिंग्स के साथ एक स्ट्रिंग बना रहा है, जो रिक्त स्थान और अल्पविराम से अलग है। तारों में से एक यूआरएल हो सकता है। – Andrew

उत्तर

0

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

आपकी आवश्यकता है कि हमारे TextView वर्ग को इस तरह से है कि यह कुछ पाठ के साथ कभी समाप्त नहीं होना चाहिए (यहाँ http में बहु दिखाना चाहिए है: // और http :), मैं बहुत लोकप्रिय TextView वर्ग के कोड को संशोधित किया है अधिक अतः इस आवश्यकता को पूरा करने के लिए: स्रोत: Auto Scale TextView Text to Fit within Bounds

परिवर्तन:

private boolean mCustomLineWrap = true; 

/** 
    * Resize the text size with specified width and height 
    * @param width 
    * @param height 
    */ 
    public void resizeText(int width, int height) { 
     CharSequence text = getText(); 
     // Do not resize if the view does not have dimensions or there is no text 
     if(text == null || text.length() == 0 || height <= 0 || width <= 0 || mTextSize == 0) { 
      return; 
     } 

     // Get the text view's paint object 
     TextPaint textPaint = getPaint(); 

     // Store the current text size 
     float oldTextSize = textPaint.getTextSize(); 
     // If there is a max text size set, use the lesser of that and the default text size 
     float targetTextSize = mMaxTextSize > 0 ? Math.min(mTextSize, mMaxTextSize) : mTextSize; 

     // Get the required text height 
     int textHeight = getTextHeight(text, textPaint, width, targetTextSize); 

     // Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes 
     while(textHeight > height && targetTextSize > mMinTextSize) { 
      targetTextSize = Math.max(targetTextSize - 2, mMinTextSize); 
      textHeight = getTextHeight(text, textPaint, width, targetTextSize); 
     } 



     if(mCustomLineWrap) { 

      // Draw using a static layout 
      StaticLayout layout = new StaticLayout(text, textPaint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false); 
      // Check that we have a least one line of rendered text 
      if(layout.getLineCount() > 0) { 
       String lineText[] = new String[layout.getLineCount()]; 
       // Since the line at the specific vertical position would be cut off, 
       // we must trim up to the previous line 
       String wrapStr = "http:", wrapStrWithSlash = "http://"; 
       boolean preAppendWrapStr = false, preAppendWrapStrWithSlash = false ; 
       for(int lastLine = 0; lastLine < layout.getLineCount(); lastLine++) 
       { 
        int start = layout.getLineStart(lastLine); 
        int end = layout.getLineEnd(lastLine); 
        lineText[lastLine] = ((String) getText()).substring(start,end); 
        if(preAppendWrapStr) 
        { 
         lineText[lastLine] = "\n" + wrapStr + lineText[lastLine]; 
         preAppendWrapStr = false; 
        } 
        else if(preAppendWrapStrWithSlash) 
        { 
         lineText[lastLine] = "\n" + wrapStrWithSlash + lineText[lastLine]; 
         preAppendWrapStrWithSlash = false; 
        } 

        if(lineText[lastLine].endsWith(wrapStr)) 
        { 
         preAppendWrapStr = true; 
         lineText[lastLine] = lineText[lastLine].substring(0,lineText[lastLine].lastIndexOf(wrapStr)); 
        } 
        if(lineText[lastLine].endsWith(wrapStrWithSlash)) 
        { 
         preAppendWrapStrWithSlash = true; 
         lineText[lastLine] = lineText[lastLine].substring(0,lineText[lastLine].lastIndexOf(wrapStrWithSlash)); 
        } 

       } 
       String compString = ""; 
       for(String lineStr : lineText) 
       { 
        compString += lineStr; 
       } 
        setText(compString); 
      } 

     } 

     // Some devices try to auto adjust line spacing, so force default line spacing 
     // and invalidate the layout as a side effect 
     textPaint.setTextSize(targetTextSize); 
     setLineSpacing(mSpacingAdd, mSpacingMult); 

     // Notify the listener if registered 
     if(mTextResizeListener != null) { 
      mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize); 
     } 

     // Reset force resize flag 
     mNeedsResize = false; 
    } 
+0

धन्यवाद, आपके समाधान काम करते हैं लेकिन क्या आपको कोई विचार है कि टेक्स्ट स्पैन को कैसे रखा जाए? अब टेक्स्ट व्यू के अंदर सभी टेक्स्ट शैलियों को 'setText() 'के कारण साफ़ कर दिया गया है – Andrew

संबंधित मुद्दे