2012-04-17 35 views
5

夥計!我有一個多行標籤,lineBreakMode設置爲UILineBreakModeWordWrap。我如何確定最後一行的寬度?謝謝iOS。確定UILabel的最後一行寬度

+0

我不知道任何方式做到這一點。但爲什麼你想要這個?也許如果我們知道爲什麼你想要這個,我們可以想出另一種方法來解決你的問題。 – Rob 2012-04-17 14:25:12

+2

我可以想象一些複雜的例程,你反覆使用NSString的'sizeWithFont:constrainedToSize:lineBreakMode:',一次添加一個單詞,找出哪個單詞將你推到下一行,然後重複這個過程,直到你到達最後行,然後最後一個'sizeWithFont:constrainedToSize:lineBreakMode:'來確定最後一行的寬度。 – Rob 2012-04-17 14:30:43

+0

我用這種方法,但它看起來很複雜,所以我想知道是否有一些很好的解決方案。不管怎樣,謝謝! – leon4ic 2012-04-30 07:24:05

回答

5

這是我做到的。首先將標籤的行放在NSArray中,然後檢查最後一行的寬度。 在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 2015-05-06 13:21:52

1

夫特3(IOS 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(IOS 10.3)