2012-04-17 15 views
5

लोगों की अंतिम पंक्ति चौड़ाई निर्धारित करें! मेरे पास एकाधिक लाइनों वाला एक लेबल है, लाइन ब्रेकमोड UILineBreakModeWordWrap पर सेट है। मैं अंतिम पंक्ति की चौड़ाई कैसे निर्धारित कर सकता हूं? धन्यवादआईओएस। UILabel

+0

मैं यह करने के लिए किसी भी तरह का पता नहीं है। लेकिन आप यह क्यों चाहते हैं? शायद अगर हम जानते हैं कि आप यह क्यों चाहते हैं तो हम आपकी समस्या का समाधान करने के लिए एक और तरीके से आ सकते हैं। – Rob

+2

मैं कुछ जटिल दिनचर्या की कल्पना कर सकता हूं जहां आप बार-बार एनएसएसटींग के 'आकार के साथ उपयोग करते हैं विथफॉन्ट: अवरुद्ध करने के लिए: लाइनब्रैक मोड:', एक समय में एक शब्द जोड़कर, यह पता लगाएं कि कौन सा शब्द आपको अगली पंक्ति में धक्का देता है, और फिर जब तक आप अंतिम तक नहीं पहुंच जाते, तब तक इस प्रक्रिया को दोहराएं लाइन, और उसके बाद एक अंतिम 'आकार WithFont: बाधित ToSize: lineBreakMode:' उस अंतिम पंक्ति की चौड़ाई को समझने के लिए। – Rob

+0

मैंने उस दृष्टिकोण का उपयोग किया लेकिन यह बहुत जटिल लग रहा है इसलिए मुझे आश्चर्य हुआ कि समस्या के लिए कुछ अच्छा समाधान है या नहीं। कोई बात नहीं धन्यवाद! – leon4ic

उत्तर

5

यहां मैंने यह कैसे किया है। सबसे पहले अपने लेबल की लाइनें एनएसएआरएआरई में डालें, और फिर अंतिम पंक्ति की चौड़ाई की जांच करें। viewDidLoad में:

NSArray* lines = [self getSeparatedLinesFromLbl:srcLabel]; 
NSString *lastLine=[lines lastObject]; 
float lastLineWidth=[lastLine sizeWithFont:srcLabel.font constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping].width; 

और getSeparatedLinesFromLbl:

-(NSArray*)getSeparatedLinesFromLbl:(UILabel*)lbl 
{ 
if (lbl.lineBreakMode != NSLineBreakByWordWrapping) 
{ 
    return nil; 
} 

NSMutableArray* lines = [NSMutableArray arrayWithCapacity:10]; 

NSCharacterSet* wordSeparators = [NSCharacterSet whitespaceAndNewlineCharacterSet]; 

NSString* currentLine = lbl.text; 
int textLength = [lbl.text length]; 

NSRange rCurrentLine = NSMakeRange(0, textLength); 
NSRange rWhitespace = NSMakeRange(0,0); 
NSRange rRemainingText = NSMakeRange(0, textLength); 
BOOL done = NO; 
while (!done) 
{ 
    // determine the next whitespace word separator position 
    rWhitespace.location = rWhitespace.location + rWhitespace.length; 
    rWhitespace.length = textLength - rWhitespace.location; 
    rWhitespace = [lbl.text rangeOfCharacterFromSet: wordSeparators options: NSCaseInsensitiveSearch range: rWhitespace]; 
    if (rWhitespace.location == NSNotFound) 
    { 
     rWhitespace.location = textLength; 
     done = YES; 
    } 

    NSRange rTest = NSMakeRange(rRemainingText.location, rWhitespace.location-rRemainingText.location); 

    NSString* textTest = [lbl.text substringWithRange: rTest]; 

    CGSize sizeTest = [textTest sizeWithFont: lbl.font forWidth: 1024.0 lineBreakMode: NSLineBreakByWordWrapping]; 
    if (sizeTest.width > lbl.bounds.size.width) 
    { 
     [lines addObject: [currentLine stringByTrimmingCharactersInSet:wordSeparators]]; 
     rRemainingText.location = rCurrentLine.location + rCurrentLine.length; 
     rRemainingText.length = textLength-rRemainingText.location; 
     continue; 
    } 

    rCurrentLine = rTest; 
    currentLine = textTest; 
} 

[lines addObject: [currentLine stringByTrimmingCharactersInSet:wordSeparators]]; 

return lines; 
} 
+0

क्या आप ios8 के लिए अपडेट कर सकते हैं – suthar

1

स्विफ्ट 3 (आईओएस 10,3)

extension UILabel { 
    func getSeparatedLines() -> [Any] { 
     if self.lineBreakMode != NSLineBreakMode.byWordWrapping { 
      self.lineBreakMode = .byWordWrapping 
     } 
     var lines = [Any]() /* capacity: 10 */ 
     let wordSeparators = CharacterSet.whitespacesAndNewlines 
     var currentLine: String? = self.text 
     let textLength: Int = (self.text?.characters.count ?? 0) 
     var rCurrentLine = NSRange(location: 0, length: textLength) 
     var rWhitespace = NSRange(location: 0, length: 0) 
     var rRemainingText = NSRange(location: 0, length: textLength) 
     var done: Bool = false 
     while !done { 
      // determine the next whitespace word separator position 
      rWhitespace.location = rWhitespace.location + rWhitespace.length 
      rWhitespace.length = textLength - rWhitespace.location 
      rWhitespace = (self.text! as NSString).rangeOfCharacter(from: wordSeparators, options: .caseInsensitive, range: rWhitespace) 
      if rWhitespace.location == NSNotFound { 
       rWhitespace.location = textLength 
       done = true 
      } 
      let rTest = NSRange(location: rRemainingText.location, length: rWhitespace.location - rRemainingText.location) 
      let textTest: String = (self.text! as NSString).substring(with: rTest) 
      let fontAttributes: [String: Any]? = [NSFontAttributeName: font] 
      let maxWidth = (textTest as NSString).size(attributes: fontAttributes).width 
      if maxWidth > self.bounds.size.width { 
       lines.append(currentLine?.trimmingCharacters(in: wordSeparators) ?? "") 
       rRemainingText.location = rCurrentLine.location + rCurrentLine.length 
       rRemainingText.length = textLength - rRemainingText.location 
       continue 
      } 
      rCurrentLine = rTest 
      currentLine = textTest 
     } 
     lines.append(currentLine?.trimmingCharacters(in: wordSeparators) ?? "") 
     return lines 
    } 

    var lastLineWidth: CGFloat { 
     let lines: [Any] = self.getSeparatedLines() 
     if !lines.isEmpty { 
      let lastLine: String = (lines.last as? String)! 
      let fontAttributes: [String: Any]? = [NSFontAttributeName: font] 
      return (lastLine as NSString).size(attributes:  fontAttributes).width 
     } 
     return 0 
    } 
} 

प्रयोग

print(yourLabel.lastLineWidth) 

स्विफ्ट 3 (आईओएस 10,3)