2013-11-26 11 views
17

मैं प्रत्येक पंक्ति पाठ के बीच लाइनस्पेस जोड़ने के लिए UITextview में NSMutableParagraphStyle का उपयोग कर रहा हूं।आईओएस - यूआईटीएक्स्टव्यू लाइनस्पेसिंग कर्सर की ऊंचाई समान नहीं है

जब मैं टेक्स्टव्यू में कुछ टाइप करता हूं, तो कर्सर ऊंचाई सामान्य होती है। लेकिन जब मैं कर्सर की स्थिति को दूसरी पंक्ति (अंतिम पंक्ति नहीं) पर पाठ में ले जाता हूं, तो कर्सर की ऊंचाई बड़ी हो रही है।

enter image description here

क्या मैं कर्सर ऊंचाई ग्रंथों के हर पंक्ति में सामान्य बनाने के लिए क्या करना चाहिए?

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
paragraphStyle.lineSpacing = 30.; 
textView.font = [UIFont fontWithName:@"Helvetica" size:16]; 
textView.attributedText = [[NSAttributedString alloc] initWithString:@"My Text" attributes:@{NSParagraphStyleAttributeName : paragraphStyle}]; 

उत्तर

23

अंत में मैं समाधान जो मेरी समस्या का समाधान मिलता है: इस कोड मैं वर्तमान में उपयोग कर रहा हूँ है।

हम UITextView को उप-वर्गीकरण करके कर्सर ऊंचाई बदल सकते हैं, फिर caretRectForPosition:position फ़ंक्शन को ओवरराइड कर सकते हैं।

- (CGRect)caretRectForPosition:(UITextPosition *)position { 
    CGRect originalRect = [super caretRectForPosition:position]; 
    originalRect.size.height = 18.0; 
    return originalRect; 
} 

प्रलेखन लिंक:: उदाहरण के लिए https://developer.apple.com/documentation/uikit/uitextinput/1614518-caretrectforposition

+2

यह एक दस्तावेज विधि है? (संपादित करें: हां, यह 'UITextInput' प्रोटोकॉल में है जो' UITextView' के अनुरूप है (https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITextInput_Protocol/index.html#//apple_ref/occ/intf/UITextInput) – oztune

+0

वास्तव में उपयोगी, अंगूठे ऊपर! – KkMIW

+0

इस पाठ का चयन करते समय हैंडल वास्तव में बड़े हैं, किसी भी विचार को ठीक करने का तरीका क्या है? –

7

और स्विफ्ट 2. एक्स या स्विफ्ट 3. एक्स के लिए:

import UIKit 

class MyTextView : UITextView { 
    override func caretRectForPosition(position: UITextPosition) -> CGRect { 
     var superRect = super.caretRectForPosition(position) 
     guard let isFont = self.font else { return superRect } 

     superRect.size.height = isFont.pointSize - isFont.descender 
      // "descender" is expressed as a negative value, 
      // so to add its height you must subtract its value 

     return superRect 
    } 
} 
संबंधित मुद्दे