0

我知道爲了將樣式設置爲spannable,我們可以使用setSpan(Object classOfStyle,int start,int end,int flags)。 我想將alignspan設置爲當前段落。當前段落由當前光標位置檢測到。這可能嗎?我可以得到段落的開始位置和結束位置嗎?如何在當前光標段落上設置AlignSpan樣式?

編輯:

段落是一組句子。其中一段將以「輸入字符」結束。

+0

定義段落。 – 2014-09-02 03:17:37

+0

對不起,它可以嗎? – ARM 2014-09-02 04:10:36

+0

如果你不知道段落是什麼,那麼你的要求就不清楚了,你的問題確實沒有答案。在對SO提出問題之前,先試着找出你想知道的內容。 – 2014-09-02 12:43:57

回答

1

我寫了一個小型助手類(針對速度進行了優化),它將完全按照您的需要進行操作。您通過將被分析以查找所有段落一個Spannable對象:

public class Layout { 
    private static final Pattern LINEBREAK_PATTERN = Pattern.compile("\\r\\n|\\r|\\n"); 

    private int mNrOfLines = 0; 
    private ArrayList<Selection> mSelection = new ArrayList<Selection>(); 

    public Layout(Spannable spannable) { 
     String s = spannable.toString(); 

     // remove the trailing line feeds 
     int len = s.length(); 
     char c = len > 0 ? s.charAt(len - 1) : '-'; 
     while (len > 0 && (c == '\n' || c == '\r')) { 
      len--; 
      c = s.charAt(len - 1); 
     } 

     // now find the line breaks and the according lines/paragraphs 
     mNrOfLines = 1; 
     Matcher m = LINEBREAK_PATTERN.matcher(s.substring(0, len)); 
     int groupStart = 0; 
     while (m.find()) { 
      mSelection.add(new Selection(groupStart, m.end())); 
      groupStart = m.end(); 
      mNrOfLines++; 
     } 
     if (groupStart < len) { 
      mSelection.add(new Selection(groupStart, len)); 
     } 
    } 

    public List<Selection> getParagraphs() { 
     return mSelection; 
    } 

    public int getLineForOffset(int offset) { 
     int lineNr = 0; 
     while(lineNr < mNrOfLines && offset >= mSelection.get(lineNr).end()) { 
      lineNr++; 
     } 
     return Math.min(Math.max(0, lineNr), mSelection.size() - 1); 
    } 

    public int getLineStart(int line) { 
     return mNrOfLines == 0 || line < 0 ? 0 : 
       line<mNrOfLines ? mSelection.get(line).start() : 
       mSelection.get(mNrOfLines-1).end()-1; 
    } 

    public int getLineEnd(int line) { 
     return mNrOfLines == 0 || line < 0 ? 0 : 
       line<mNrOfLines ? mSelection.get(line).end() : 
       mSelection.get(mNrOfLines-1).end()-1; 
    } 
} 

這三種方法getLineForOffset,getLineStart和getLineEnd可以用來尋找段落當前SelectionSelection可以是當前光標位置(基本上是一個Selection,開始==結束)或選定的文本。以下代碼將返回當前所選段落的選定段落:

Selection getParagraphs(EditText editor) {  
    Layout layout = new Layout(editor.getEditableText()); 

    int selStart = editor.getSelectionStart(); 
    int selEnd = editor.getSelectionEnd(); 

    int firstLine = layout.getLineForOffset(selStart); 
    int end = selStart == selEnd ? selEnd : selEnd - 1; 
    int lastLine = layout.getLineForOffset(end); 

    return new Selection(layout.getLineStart(firstLine), layout.getLineEnd(lastLine)); 
} 

如果例如文本是:

線1
升[INE 2
線] 3
線4

用[]中選擇,getParagraphs(之間的所有內容)將返回所述第二和第三(它基本上擴展了選定的文本,包括所有(甚至部分)選定的段落。

+0

很好..謝謝.. – ARM 2014-09-03 03:54:52

+0

謝謝..我想再問一次,但對不起線程主題。我看到你的電子郵件應用程序。如何才能創建字體格式化,可能僅用於標準化粗體,斜體和下劃線。我在Wordpress Android源代碼中看到,但是在格式化字體時存在很多bug。 – ARM 2014-09-03 04:02:39

+0

這不是我可以回答的問題。我的富文本編輯器有很多工作,上面的代碼只是處理段落格式的一小部分。你可以看看這個非常簡單的富文本編輯器:https://github.com/commonsguy/cwac-richedit – 2014-09-03 13:33:04

0

我寫了一些方法來獲取段落的第一個位置和段落的最後一個位置 我不知道,哪一個更好。如果它錯了,請糾正我。


    public static int getFirstPositionOfParagraph(String paragraph, int cursorPosition) { 
     StringBuffer buffer = new StringBuffer(paragraph); 
     List pos = new ArrayList(); 
     for (String breaker : LINE_BREAK){ 
      pos.add(buffer.lastIndexOf(breaker, cursorPosition)); 
     } 
     int firstPosition = Collections.max(pos); 
     return firstPosition
 

     public static int getLastPositionOfParagraph(String paragraph, int cursorPosition) { 
     StringBuffer buffer = new StringBuffer(paragraph); 
     List pos = new ArrayList(); 
     for (String breaker : LINE_BREAK){ 
      int breakerPos = buffer.indexOf(breaker, cursorPosition); 
      if (breakerPos > 0) 
      pos.add(breakerPos); 
     } 
     return pos.size() > 0 ? Collections.min(pos) + 1 : paragraph.length(); 
     } 

相關問題