2011-09-06 16 views
6

मुझे UITextView की लाइनों की संख्या मिलनी है। पर UITextView पर कोई संपत्ति उपलब्ध नहीं है। मैं निम्नलिखित सूत्र का उपयोग करता हूं, लेकिन यह काम नहीं करता है। क्या किसी के बारे में कोई विचार है?लाइनों की UITextView संख्या को कैसे खोजें

int numLines = txtview.contentSize.height/txtview.font.lineHeight; 

उत्तर

19

आप आईओएस 3 का उपयोग कर रहे हैं, तो आप leading संपत्ति का उपयोग करने की जरूरत है:

int numLines = txtview.contentSize.height/txtview.font.lineHeight; 
:

int numLines = txtview.contentSize.height/txtview.font.leading; 

आप आईओएस 4 का उपयोग कर रहे हैं, तो आप lineHeight संपत्ति उपयोग करने की आवश्यकता

और, जैसा कि @ थॉमस ने बताया, यदि आपको सटीक परिणाम की आवश्यकता है तो गोल करने से सावधान रहें।

+1

: सूत्र एक फ्लोटिंग प्वाइंट-मूल्य जो (int करने के लिए डाली कम के साथ समाप्त होता है पर हो जाता है बाध्य)। शायद परिणाम को गोल करने से बेहतर परिणाम मिलते हैं: 'int numLines = round (...)' तो 0.9999 का नतीजा 1 होता है और 0 – thomas

+0

@ थॉमस: सत्य नहीं होता है। मैंने इसे उत्तर में जोड़ा। –

1

आप अपने UITextView की contentSize संपत्ति को देखने UITextView के फ़ॉन्ट का पंक्ति रिक्ति द्वारा पिक्सेल में पाठ की ऊंचाई, और भाग कर (पर कुल UIScrollView में पाठ लाइनों की संख्या प्राप्त करने के लिए कर सकते हैं और ऑफ स्क्रीन), दोनों लपेटा और लाइन टूटा पाठ सहित।

int numLines = txtview.contentSize.height/txtview.font.leading; 
1

स्विफ्ट 4 रास्ता UITextView में लाइनों की संख्या की गणना करने के UITextInputTokenizer का उपयोग कर:

public extension UITextView { 
    /// number of lines based on entered text 
    public var numberOfLines: Int { 
     guard compare(beginningOfDocument, to: endOfDocument).same == false else { 
      return 0 
     } 
     let direction: UITextDirection = UITextStorageDirection.forward.rawValue 
     var lineBeginning = beginningOfDocument 
     var lines = 0 
     while true { 
      lines += 1 
      guard let lineEnd = tokenizer.position(from: lineBeginning, toBoundary: .line, inDirection: direction) else { 
       fatalError() 
      } 
      guard compare(lineEnd, to: endOfDocument).same == false else { 
       break 
      } 
      guard let newLineBeginning = tokenizer.position(from: lineEnd, toBoundary: .character, inDirection: direction) else { 
       fatalError() 
      } 
      guard compare(newLineBeginning, to: endOfDocument).same == false else { 
       return lines + 1 
      } 
      lineBeginning = newLineBeginning 
     } 
     return lines 
    } 
} 

public extension ComparisonResult { 

    public var ascending: Bool { 
     switch self { 
     case .orderedAscending: 
      return true 
     default: 
      return false 
     } 
    } 

    public var descending: Bool { 
     switch self { 
     case .orderedDescending: 
      return true 
     default: 
      return false 
     } 
    } 

    public var same: Bool { 
     switch self { 
     case .orderedSame: 
      return true 
     default: 
      return false 
     } 
    } 
} 
अलावा
+0

'प्रकार का मूल्य' संकलित नहीं करता है तुलनात्मकता 'का कोई सदस्य' समान 'नहीं है। कुछ निजी विस्तार? – Pahnev

संबंधित मुद्दे