2012-05-18 201 views
0

我有這段代碼來顯示特定章節的一些文本,我正在使用核心文本。但我的需要是,當第一章的文本結束時,它想要顯示下一章文本在newline.my代碼中用於顯示文本附加字符串

NSMutableString *combined = [NSMutableString string]; 

    for(NSUInteger idx = 0; idx < [delegatee.allSelectedVerseEnglish count]; idx++) { 
     [combined appendFormat:@" %d %@", 
     idx + 1, 
     [delegatee.allSelectedVerseEnglish objectAtIndex:idx]]; 

    } 

    self.multiPageView.text =combined; 

multipageview的是,我通過核心text.so我需要的是我在「組合拳」第一章的文字。所以我想追加與下一章結合,代碼呈現文本視圖爲顯示下一章是

delegatee.selectedChapter = [NSString stringWithFormat:@"%d",[delegatee.selectedChapter intValue] + 1]; 
    [delegatee reloadVerses]; 
    [self resetReadViewToVerse:1]; 

合併包含這樣的文字... 1 haii這是我的第一句話2這是我的第二句話3這是我的第三句話,蝕刻etc ..所以如果第三句話是最後一段文字,那麼在/ n換行我需要下一章文字.. 我把這個按鈕中的代碼導航到下一章,但在結束每章之後我會自動需要它。

是否有可能? 在此先感謝。

+0

我的理解是否正確:'allSelectedVerseEnglish'返回所選章節的所有經文,並且沒有方法通過章節號來獲取經文? –

+0

@ A-Live是的,它返回所選章節的所有詩句,沒有方法可以按照章節號碼 – stackiphone

+0

@ A-Live獲得詩句,但我可以選擇書籍,章節和詩句 – stackiphone

回答

0

首先,您可能需要準備一種方法,通過章節號來獲取經文(以獲取下一章的經文,以便顯示它)。

for循環,self.multiPageView.text =combined;您應該計算在過去的「頁」佔據的行數之間,這可以讓你計算行從下一章填充的基礎上,最大的「頁」數數線。

現在,當你有下一章節和要添加的行數時,你可以很容易地使用while循環附加下一章節行來填充「頁面」。還有就是考慮到兩個重要條件:

  • 有可能是沒有下一章 - 你跳過下一章文本追加,禁止下一章按鈕等
  • 有在最後沒有空行「頁」需要填補,但有一個新篇章 - 你跳過下一章文本追加,但接下來的章節按鈕啓用

生成的代碼如下所示:

NSMutableString *combined = @""; 
NSArray * selectedChapterVersesArray = [delegatee.allVersesForLanguage:kEngLang atChapter:delegatee.selectedChapter]; 

if (selectedChapterVersesArray != nil && [selectedChapterVersesArray count] > 0) 

// prepare the selected chapter text 
for(NSUInteger idx = 0; idx < [selectedChapterVersesArray count]; idx++) { 
    [combined appendFormat:@" %d %@ \n", 
    idx + 1, 
    [selectedChapterVersesArray objectAtIndex:idx]]; 

} 

// calculate the lines found to be added from the next chapter - to be implemented ! 
NSUInteger linesToAddFromNextChapter = [self emptyLinesForMultipageView:self.multipageview withText:combined]; 

if (delegatee.numberOfChapters > delegatee.selectedChapter) 
{ 
// there's a next chapter 
NSArray * nextChapterVersesArray = [delegatee.allVersesForLanguage:kEngLang atChapter:delegatee.selectedChapter+1]; 

// append the next chapter text 
NSUInteger linesAdded = 0; 
BOOL nextChapterHasLinesToAdd = [nextChapterVersesArray count] > 0; 

while (linesAdded < linesToAddFromNextChapter && nextChapterHasLinesToAdd) { 
    // append one line, inc linesAdded, recalculate nextChapterHasLinesToAdd 
} 
} 
} 

self.multiPageView.text =combined; 
delegatee.selectedChapter = [NSString stringWithFormat:@"%d",[delegatee.selectedChapter intValue] + 1]; 
[delegatee reloadVerses]; 
[self resetReadViewToVerse:1]; 

請注意,您在格式字面值中丟失了換行符\n,代碼僅僅是一個草稿,並且有要實施的方法。

+0

讓我chi,非常感謝 – stackiphone

+0

我不知道如何cacuate每章的線條,我很驚訝there.but你的代碼是真正正確的。但我不認爲我能想出它out.its我的錯誤。 – stackiphone

+0

我不知道你的'multiPageView'實現是什麼,如果這是一個'UIScrollView'子類(就像'UITextView'),那麼不用添加新行,你可以在while循環的下一章添加完整的經文。停止的條件將是a)添加所有經文b)'multiPageView.contentSize。height> kMultiPageViewHeight',那麼你可以改變「page」'contentInset'來截取b)或'contentSize'的多餘行,並且預定義的寬度和高度爲a)。希望能幫助到你。 –