2013-11-26 3 views
8

आईओएस के पिछले संस्करणों में करने के लिए, मेरे UITextView नीचेक्या iOS7 में scrollRangeToVisible के बजाय का उपयोग करें या TextKit

[displayText scrollRangeToVisible:NSMakeRange(0,[displayText.text length])]; 

या

CGFloat topCorrect = displayText.contentSize.height -[displayText bounds].size.height; 
topCorrect = (topCorrect<0.0?0.0:topCorrect); 
displayText.contentOffset = (CGPoint){.x=0, .y=topCorrect}; 

लेकिन पूर्व का उपयोग कर अब अजीब होगा करने के लिए स्क्रॉल करेगा टेक्स्ट की लंबी अवधि के शीर्ष पर शुरू होने और प्रत्येक बार जब मैं टेक्स्ट को दृश्य में जोड़ता हूं तो नीचे स्क्रॉल को एनिमेट करने का प्रभाव। जब मैं पाठ जोड़ता हूं तो पाठ के निचले हिस्से में नीचे जाने का कोई तरीका है?

उत्तर

2

मेरा मानना ​​है कि इस UITextView पर iOS 7. टॉगल scrollEnabled में एक बग है इसे ठीक करने लगता है:

[displayText scrollRangeToVisible:NSMakeRange(0,[displayText.text length])]; 
displayText.scrollEnabled = NO; 
displayText.scrollEnabled = YES; 
+1

आईओएस 7.1 पर मेरे लिए काम नहीं करता। –

+1

@ स्टियन-होइलैंड सही साबित होता है कि यह कामकाज अब काम नहीं करता है। –

+0

@ user3907849 एक कामकाजी समाधान है। 'ScrollRangeToVisible:' कॉल करने से पहले 'scrollEnabled = NO' सेट करना महत्वपूर्ण बात है। पूरी स्ट्रिंग या केवल अंतिम वर्ण का उपयोग करने से दोनों नीचे स्क्रॉल करने के लिए काम करेंगे। –

0

मुझे लगता है कि अपने मानकों को NSMakeRange में उलट कर रहे हैं। स्थान पहला है, तो आप कितने चुनना चाहते हैं (लंबाई)।

NSMakeRange(0,[displayText.text length]) 

... 0 वें (पहले?) चरित्र से शुरू होने वाला चयन और स्ट्रिंग की पूरी लंबाई पर जाकर चयन करेगा। नीचे स्क्रॉल करने के लिए आप शायद अंत में एक ही अक्षर का चयन करना चाहते हैं।

यह आईसीएस एसडीके 7.1 में एक्ससीडीओ 5.1.1 के साथ मेरे लिए काम कर रहा है।

[textView scrollRangeToVisible:NSMakeRange(textView.text.length - 1,0)]; 
textView.scrollEnabled = NO; 
textView.scrollEnabled = YES; 

मैं प्रोग्राम के रूप में इस पाठ के रूप में मैं जोड़ने करते हैं, और पाठ विचारों टर्मिनल या कमांड लाइन उत्पादन की तरह तल पर रहता है।

3
textView.scrollEnabled = NO; 
[textView scrollRangeToVisible:NSMakeRange(textView.text.length - 1,0)]; 
textView.scrollEnabled = YES; 

यह वास्तव में आईओएस 7.1.2 में मेरे लिए काम करता है।

0

UITextView के लिए सीमा निर्धारित करने का सबसे अच्छा तरीका है। यह स्क्रॉलिंग को ट्रिगर नहीं करता है और जो दिख रहा है उसे पुनर्स्थापित करने का तत्काल प्रभाव पड़ता है। आप कैरट के स्थान खोजने और उसके बाद का स्थान बदलने से ऐसा कर सकते हैं: भविष्य यात्रियों के लिए

- (void)userInsertingNewText { 
    UITextView *textView; 
    // find out where the caret is located 
    CGRect caret = [textView caretRectForPosition:textView.selectedTextRange.start]; 
    // there are insets that offset the text, so make sure we use that to determine the actual text height 
    UIEdgeInsets textInsets = textView.textContainerInset; 
    CGFloat textViewHeight = textView.frame.size.height - textInsets.top - textInsets.bottom; 
    // only set the offset if the caret is out of view 
    if (textViewHeight < caret.origin.y) { 
     [self repositionScrollView:textView newOffset:CGPointMake(0, caret.origin.y - textViewHeight)]; 
    } 
} 

/** 
This method allows for changing of the content offset for a UIScrollView without triggering the scrollViewDidScroll: delegate method. 
*/ 
- (void)repositionScrollView:(UIScrollView *)scrollView newOffset:(CGPoint)offset { 
    CGRect scrollBounds = scrollView.bounds; 
    scrollBounds.origin = offset; 
    scrollView.bounds = scrollBounds; 
} 
2

, @ mikeho पद के बंद का निर्माण, मैं कुछ है कि मेरे लिए चमत्कार काम मिल गया है, लेकिन थोड़ा आसान है।

1) सुनिश्चित करें कि आपके UITextView के contentInset रों ठीक से & सेट कर रहे हैं अपने TextView पहले से ही firstResponder() ऐसा करने से पहले हो जाता है।
2) मेरी सन्निवेश के बाद जाने के लिए तैयार हैं, और कर्सर सक्रिय है, मैं निम्नलिखित फ़ंक्शन को कॉल करें:

private func scrollToCursorPosition() { 
    let caret = textView.caretRectForPosition(textView.selectedTextRange!.start) 
    let keyboardTopBorder = textView.bounds.size.height - keyboardHeight! 

    // Remember, the y-scale starts in the upper-left hand corner at "0", then gets 
    // larger as you go down the screen from top-to-bottom. Therefore, the caret.origin.y 
    // being larger than keyboardTopBorder indicates that the caret sits below the 
    // keyboardTopBorder, and the textView needs to scroll to the position. 
    if caret.origin.y > keyboardTopBorder { 
     textView.scrollRectToVisible(caret, animated: true) 
    } 
} 
संबंधित मुद्दे