18

有没有一种简单的方法可以从 UILabel 中的给定行获取(或简单地显示)文本?

我的 UILabel 正确地显示了我的文本并将其布置得很漂亮,但有时我需要能够只显示某些行,但显然我需要知道 UILabel 是如何定位所有内容来做到这一点的。

我知道这可以通过子字符串轻松完成,但我需要知道该行的起点和终点。

或者,如果 UILabel 的框架有某种偏移,我可以滚动 UILabel 并隐藏我不想看到的其余内容。

我无法发现任何表明如何轻松做到这一点的东西。有人有什么好主意吗?

谢谢

伊帕奥

4

10 回答 10

32

我有更好的方法找到它。

你可以在CoreText.framework的帮助下得到这个。

1.添加CoreText.framework 。
2.进口#import <CoreText/CoreText.h>
然后使用以下方法:

- (NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label {
    NSString *text = [label text];
    UIFont   *font = [label font];
    CGRect    rect = [label frame];
    
    CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];
    
    
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr);
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
    
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    
    NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
    NSMutableArray *linesArray = [[NSMutableArray alloc]init];
    
    for (id line in lines)
    {
        CTLineRef lineRef = (__bridge CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);
        
        NSString *lineString = [text substringWithRange:range];
        [linesArray addObject:lineString];
    }

    return (NSArray *)linesArray;
}

调用此方法:-

NSArray *linesArray = [self getLinesArrayOfStringInLabel:yourLabel];

现在您可以使用linesArray.

斯威夫特 4 版

func getLinesArrayOfString(in label: UILabel) -> [String] {
        
        /// An empty string's array
        var linesArray = [String]()
        
        guard let text = label.text, let font = label.font else {return linesArray}
        
        let rect = label.frame
        
        let myFont = CTFontCreateWithFontDescriptor(font.fontDescriptor, 0, nil)
        let attStr = NSMutableAttributedString(string: text)
        attStr.addAttribute(kCTFontAttributeName as NSAttributedString.Key, value: myFont, range: NSRange(location: 0, length: attStr.length))
        
        let frameSetter: CTFramesetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
        let path: CGMutablePath = CGMutablePath()
        path.addRect(CGRect(x: 0, y: 0, width: rect.size.width, height: 100000), transform: .identity)
        
        let frame: CTFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
        guard let lines = CTFrameGetLines(frame) as? [Any] else {return linesArray}
        
        for line in lines {
            let lineRef = line as! CTLine
            let lineRange: CFRange = CTLineGetStringRange(lineRef)
            let range = NSRange(location: lineRange.location, length: lineRange.length)
            let lineString: String = (text as NSString).substring(with: range)
            linesArray.append(lineString)
        }
        return linesArray
 }

利用:

let lines: [String] = getLinesArrayOfString(in: label)
于 2013-01-19T10:42:23.050 回答
9

斯威夫特 3

func getLinesArrayFromLabel(label:UILabel) -> [String] {
  let text:NSString = label.text! as NSString // TODO: Make safe?
  let font:UIFont = label.font
  let rect:CGRect = label.frame
    
  let myFont:CTFont = CTFontCreateWithName(font.fontName as CFString, font.pointSize, nil)
  let attStr:NSMutableAttributedString = NSMutableAttributedString(string: text as String)
  attStr.addAttribute(String(kCTFontAttributeName), value:myFont, range: NSMakeRange(0, attStr.length))
  let frameSetter:CTFramesetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
  let path:CGMutablePath = CGMutablePath()
  path.addRect(CGRect(x:0, y:0, width:rect.size.width, height:100000))
    
  let frame:CTFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
  let lines = CTFrameGetLines(frame) as NSArray
  var linesArray = [String]()
    
  for line in lines {
    let lineRange = CTLineGetStringRange(line as! CTLine)
    let range:NSRange = NSMakeRange(lineRange.location, lineRange.length)
    let lineString = text.substring(with: range)
    linesArray.append(lineString as String)
  }
  return linesArray
}

Swift 2 (Xcode 7) 版本(经过测试,并从 Swift 1 答案重新编辑)

func getLinesArrayOfStringInLabel(label:UILabel) -> [String] {  
  let text:NSString = label.text! // TODO: Make safe?
  let font:UIFont = label.font
  let rect:CGRect = label.frame
    
  let myFont:CTFontRef = CTFontCreateWithName(font.fontName, font.pointSize, nil)
  let attStr:NSMutableAttributedString = NSMutableAttributedString(string: text as String)
  attStr.addAttribute(String(kCTFontAttributeName), value:myFont, range: NSMakeRange(0, attStr.length))
  let frameSetter:CTFramesetterRef = CTFramesetterCreateWithAttributedString(attStr as CFAttributedStringRef)
  let path:CGMutablePathRef = CGPathCreateMutable()
  CGPathAddRect(path, nil, CGRectMake(0, 0, rect.size.width, 100000))
  let frame:CTFrameRef = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
  let lines = CTFrameGetLines(frame) as NSArray
  var linesArray = [String]()
    
  for line in lines {
    let lineRange = CTLineGetStringRange(line as! CTLine)
    let range:NSRange = NSMakeRange(lineRange.location, lineRange.length)
    let lineString = text.substringWithRange(range)
    linesArray.append(lineString as String)
  }
  return linesArray
}
于 2015-06-30T02:04:21.360 回答
5

以适当的释放回答!!!!

-(NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label
{
    NSString *text = [label text];
    UIFont   *font = [label font];
    CGRect    rect = [label frame];

    CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:( id)myFont range:NSMakeRange(0, attStr.length)];

    CFRelease(myFont);

    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));

    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);

    NSArray *lines = ( NSArray *)CTFrameGetLines(frame);

    NSMutableArray *linesArray = [[NSMutableArray alloc]init];

    for (id line in lines)
    {
        CTLineRef lineRef = ( CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);

        NSString *lineString = [text substringWithRange:range];

        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0]));
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0]));

        //NSLog(@"''''''''''''''''''%@",lineString);
        [linesArray addObject:lineString];

    }
    [attStr release];

    CGPathRelease(path);
    CFRelease( frame );
    CFRelease(frameSetter);


    return (NSArray *)linesArray;
}
于 2013-02-19T08:28:01.517 回答
5

关于 iOS 11+ 的非常重要的变化

从 iOS 11 开始,Apple 有意更改了其自动换行功能的行为,UILabel该功能会检测String多行中各行的内容UILabel。按照设计,UILabel现在的自动换行避免了孤立文本(换行中的单个单词),如下所述:iOS 11 中的自动换行

因此,如果避免孤立文本的新自动换行在特定行中生效,则CTFrameGetLines(frame)返回CTLine标签中所有行的数组的方式将不再正常工作。相反,它导致String新的自动换行设计的部分内容属于下一行,而不是最终在焦点行中。

可以在我的@TheTiger's answer 的更改版本中找到针对此问题的经过测试的修复程序,该版本使用计算UILabelusing的实际内容大小sizeThatFits(size:),然后使用该大小创建用Swift 4编写的 rect / 路径:

extension UILabel {

    /// creates an array containing one entry for each line of text the label has
    var lines: [String]? {

        guard let text = text, let font = font else { return nil }

        let attStr = NSMutableAttributedString(string: text)
        attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: attStr.length))

        let frameSetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
        let path = CGMutablePath()

        // size needs to be adjusted, because frame might change because of intelligent word wrapping of iOS
        let size = sizeThatFits(CGSize(width: self.frame.width, height: .greatestFiniteMagnitude))
        path.addRect(CGRect(x: 0, y: 0, width: size.width, height: size.height), transform: .identity)

        let frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, attStr.length), path, nil)
        guard let lines = CTFrameGetLines(frame) as? [Any] else { return nil }

        var linesArray: [String] = []

        for line in lines {
            let lineRef = line as! CTLine
            let lineRange = CTLineGetStringRange(lineRef)
            let range = NSRange(location: lineRange.location, length: lineRange.length)
            let lineString = (text as NSString).substring(with: range)
            linesArray.append(lineString)
        }
        return linesArray
    }
}

UILabel扩展将标签的内容作为String数组返回,每行一个条目与呈现给用户的外观完全相同。

于 2018-12-14T16:06:06.733 回答
4

我不认为有这样做的本地方法(如“takethenline”方法)。
我可以找出一个棘手的解决方案,但我不确定是最好的解决方案。
您可以将标签拆分为一组单词。
然后你可以循环数组并检查文本高度,直到这个词是这样的:

NSString *texttocheck;
float old_height = 0;
int linenumber = 0; 

for (x=0; x<[wordarray lenght]; x++) {
    texttocheck = [NSString stringWithFormat:@"%@ %@", texttocheck, [wordarray objectAtIndex:x]];

    float height = [text sizeWithFont:textLabel.font
                    constrainedToSize:CGSizeMake(textLabel.bounds.size.width,99999) 
                        lineBreakMode:UILineBreakModeWordWrap].height;

    if (old_height < height) {
        linenumber++;
    }
}

如果高度发生变化,则表示单词前有换行符。
我现在无法检查语法是否正确编写,因此您必须自己检查。

于 2010-12-12T13:27:22.610 回答
2

这是用于获取标签中所有行的 Swift 3 版本。(@fredpi 有类似的答案,但仅适用于第一行)

extension UILabel {

    func getArrayOfLinesInLabel() -> [String] {

       let text = NSString(string: self.text ?? "-- -- -- --")
       let font = self.font ?? // Your default font here
       let rect = self.frame

       let myFont = CTFontCreateWithName(font.fontName as CFString?, font.pointSize, nil)
       let attStr = NSMutableAttributedString(string: text as String)
       attStr.addAttribute(String(kCTFontAttributeName), value:myFont, range: NSRange(location: 0, length: attStr.length))
       let frameSetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
       let path = CGPath(rect: CGRect(x: 0, y: 0, width: rect.size.width, height: rect.size.height), transform: nil)
       let frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
       guard let lines = CTFrameGetLines(frame) as? [CTLine] else {
           return []
       }

       var linesArray = [String]()

       for line in lines {
           let lineRange = CTLineGetStringRange(line)
           let range = NSRange(location: lineRange.location, length: lineRange.length)
           let lineString = text.substring(with: range)
           linesArray.append(lineString as String)
       }

       return linesArray
   }
}
于 2017-04-07T18:23:18.457 回答
1

斯威夫特 3 - Xcode 8.1

我已将先前答案中的代码放在一起,以创建与 Swift 3、Xcode 8.1 兼容的代码,extensionUILabel返回标签的第一行。

import CoreText

extension UILabel {

   /// Returns the String displayed in the first line of the UILabel or "" if text or font is missing
   var firstLineString: String {

    guard let text = self.text else { return "" }
    guard let font = self.font else { return "" }
    let rect = self.frame

    let attStr = NSMutableAttributedString(string: text)
    attStr.addAttribute(String(kCTFontAttributeName), value: CTFontCreateWithName(font.fontName as CFString, font.pointSize, nil), range: NSMakeRange(0, attStr.length))

    let frameSetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
    let path = CGMutablePath()
    path.addRect(CGRect(x: 0, y: 0, width: rect.size.width + 7, height: 100))
    let frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)

    guard let line = (CTFrameGetLines(frame) as! [CTLine]).first else { return "" }
    let lineString = text[text.startIndex...text.index(text.startIndex, offsetBy: CTLineGetStringRange(line).length-2)]

    return lineString
  }
}

要使用它,只需像这样firstLineString对您的UILabel实例进行简单调用:

let firstLine = myLabel.firstLineString
于 2016-11-06T12:22:00.010 回答
0

如果您的所有字符都以相同的大小显示,即它们被封闭在一个共同大小的盒子中,您可以利用它。(例如,日语字符似乎就是这种情况。)

否则,您可以查询显示字体中每个字符的大小并计算该行的大小。

那么唯一的担心是你的计算可能与苹果在幕后所做的不一致——在这种情况下,我建议你去覆盖文本框架绘图的麻烦。为此,请在文档中查找 Core Text。

(我可能做错了,但我没有发现文档中给出的 Apple 方法非常准确,所以我自己做了其他事情。)

于 2011-03-28T07:21:58.620 回答
0

接受的答案非常好。

我重构了两个地方:

  1. 10000更改为CGFloat.greatestFiniteMagnitude

  2. extension将其添加到UILabel

  3. 我还想提一下,如果您通过设置框架来创建标签,它可以正常工作。如果您使用自动布局,那么不要忘记调用

    youLabel.layoutIfNeeded()

以获得正确的帧大小。

这是代码:

extension UILabel {
    var stringLines: [String] {
        guard let text = text, let font = font else { return [] }
        let ctFont = CTFontCreateWithName(font.fontName as CFString, font.pointSize, nil)
        let attStr = NSMutableAttributedString(string: text)
        attStr.addAttribute(kCTFontAttributeName as NSAttributedString.Key, value: ctFont, range: NSRange(location: 0, length: attStr.length))
        let frameSetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
        let path = CGMutablePath()
        path.addRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: CGFloat.greatestFiniteMagnitude), transform: .identity)
        let frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
        guard let lines = CTFrameGetLines(frame) as? [Any] else { return [] }
        return lines.map { line in
            let lineRef = line as! CTLine
            let lineRange: CFRange = CTLineGetStringRange(lineRef)
            let range = NSRange(location: lineRange.location, length: lineRange.length)
            return (text as NSString).substring(with: range)
        }
    }
}
于 2019-03-14T07:22:42.530 回答
0

对不起,我的声誉太低,无法发表评论。这是Philipp Jahoda对https://stackoverflow.com/a/53783203/2439941的评论。

您的代码片段完美无缺,直到我们在 UILabel 上启用了动态类型。当我们在 iOS 设置应用程序中将文本大小设置为最大值时,它开始丢失返回数组的最后一行中的字符。或者甚至用大量文本完全错过最后一行。

我们设法通过使用不同的方式来解决这个问题frame

let frameSetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
let path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.frame.width, height: .greatestFiniteMagnitude))
let frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, attStr.length), path.cgPath, nil)
guard let lines = CTFrameGetLines(frame) as? [Any] else { return nil }

现在它适用于任何动态类型大小。

那么完整的功能是:

extension UILabel {

    /// creates an array containing one entry for each line of text the label has
    var lines: [String]? {

        guard let text = text, let font = font else { return nil }

        let attStr = NSMutableAttributedString(string: text)
        attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: attStr.length))

        let frameSetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
        let path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.frame.width, height: .greatestFiniteMagnitude))
        let frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, attStr.length), path.cgPath, nil)
        guard let lines = CTFrameGetLines(frame) as? [Any] else { return nil }

        var linesArray: [String] = []

        for line in lines {
            let lineRef = line as! CTLine
            let lineRange = CTLineGetStringRange(lineRef)
            let range = NSRange(location: lineRange.location, length: lineRange.length)
            let lineString = (text as NSString).substring(with: range)
            linesArray.append(lineString)
        }
        return linesArray
    }
}
于 2019-05-31T07:07:34.247 回答