2013-06-18 125 views
4

我很清楚這個問題已被問到,但我找不到有效的答案。 利用現有解決方案的組合,我想出了這個代碼:限制UITextView內的行數

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string 
{ 
    int numLines = notesTextView.contentSize.height/notesTextView.font.lineHeight; 
     if (numLines <= 8) 
     { 
      return true; 
     } 
     return false; 
} 

因爲行數先於其他文本計數,所以我們仍然採取線超出我們想要的這不起作用然後被困在上面,因爲沒有進一步的編輯是可能的。

我也嘗試過檢測「\ n」條目的解決方案,但這不起作用,因爲我們可以在不按回車的情況下自然達到新行。

+0

我不明白您的評論,是我的答案有幫助,或者沒有爲你工作? –

+0

它沒有爲我工作。一旦返回被按下,或者當我刪除那段代碼會將光標移動到第10行的開頭,然後再不允許任何進一步的輸入時,它會關閉textview(我希望這會發生在第8行的末尾線)。 – Deco

+0

剛編輯我的答案。檢查新方法,看看它是否適合你。 –

回答

6

我也遇到過這個問題。以前的解決方案都不適合我。這裏是我的解決方案,希望:(!的iOS 7+只)

- (void)textViewDidChange:(UITextView *)textView 
{ 
    NSLayoutManager *layoutManager = [textView layoutManager]; 
    NSUInteger numberOfLines, index, numberOfGlyphs = [layoutManager numberOfGlyphs]; 
    NSRange lineRange; 
    for (numberOfLines = 0, index = 0; index < numberOfGlyphs; numberOfLines++) 
    { 
      (void) [layoutManager lineFragmentRectForGlyphAtIndex:index 
               effectiveRange:&lineRange]; 
     index = NSMaxRange(lineRange); 
    } 

    if (numberOfLines > 3) 
    { 
     // roll back 
     _labelField.text = _text; 
    } 
    else 
    { 
     // change accepted 
     _text = _labelField.text; 
    } 
} 

它使用一個NSString伊娃_text文本已經改變之後,才能夠回滾。這不會導致閃爍。

numberOfLines參考:https://developer.apple.com/library/mac/documentation/cocoa/conceptual/TextLayout/Tasks/CountLines.html#//apple_ref/doc/uid/20001810-CJBGBIBB

5

如何:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string 
{ 
    NSString *temp = [textView.text stringByReplacingCharactersInRange:range withString:string] 
    CGSize size = [temp sizeWithFont:textView.font constrainedToSize:CGSizeMake(textView.frame.size.width,999) lineBreakMode:UILineBreakModeWordWrap]; 
    int numLines = size.height/textView.font.lineHeight; 
    if (numLines <= 8) 
    { 
     return true; 
    } 
    return false; 
} 

解析新文本,然後使用TextView中的信息來檢查新文本的大小。

+0

這似乎不起作用。一個孤立的「\ n」字符似乎並沒有改變「大小」的高度,所以我們遇到了與我的解決方案相同的問題。 – Deco

+0

有趣。我認爲它會默認換行模式,但顯然不是。檢查編輯,將'lineBreakMode'選項添加到size方法。 – Putz1103

+0

它也不適用於自然行更改,它會停止接受通過第9行中途的新輸入。無法弄清楚爲什麼會發生tbh。 – Deco

0

這裏是我過去如何做到的,希望它能爲您提供一些幫助。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 

// limit the number of lines in textview 
NSString* newText = [mytextView.text stringByReplacingCharactersInRange:range withString:text]; 

// pretend there's more vertical space to get that extra line to check on 
CGSize tallerSize = CGSizeMake(mytextView.frame.size.width-15, mytextView.frame.size.height*2); 

CGSize newSize = [newText sizeWithFont:mytextView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap]; 

if (newSize.height > mytextView.frame.size.height) 
{ 
    NSLog(@"two lines are full"); 
    return NO; 
} 


// dismiss keyboard and send comment 
if([text isEqualToString:@"\n"]) { 
    [mytextView resignFirstResponder]; 

    return NO; 
} 

return YES; 
} 

祝你好運。

編輯:

好吧請嘗試下面的方法,看看它是否適合你。只要將行數改爲任何你想要的數字。

(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { 
    if([text isEqualToString:@"n"]) { 
     rows++; 
    if(rows >= maxNumberOfLines){ 
     //Exit textview 
     return NO; 
     } 
    } 
    return YES; 

讓我知道這是否可行。

+0

刪除鍵盤解除,它禁止我按下回車,目的是開始新的一行,這使我到第十行,然後拒絕爲該行採取任何輸入。非常感謝。 – Deco