美文网首页
iOS中求出label中文字的行数和每一行的内容

iOS中求出label中文字的行数和每一行的内容

作者: 荔枝lizhi_iOS程序猿 | 来源:发表于2022-10-17 16:27 被阅读0次

    ios swift 获取UIlabel的文字行数,每一行的内容。 记录 ----> 荔枝

    下面代码不但能够求出一个label中文字行数,厉害的是能够求出每一行的内容是什么; 代码如下。

    OC

    - (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:(__bridge  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 = (__bridge  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];
        }
        
        CGPathRelease(path);
        CFRelease( frame );
        CFRelease(frameSetter);
        return (NSArray *)linesArray;
    }
    

    swift

     extension UILabel {
        /// 字符串分行后获取每行的内容
        /// 通过富文本分行,然后通过CTframe获取每一行的内容
        func linesOfString() -> [String] {
            var strings: [String] = []
            guard let text = text,
                  let font = font else { return [] }
            let attstr = NSMutableAttributedString(string: text, attributes: [NSAttributedString.Key.font: font])
            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineBreakMode = .byWordWrapping
            attstr.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, text.count))
            
            let frameSetter = CTFramesetterCreateWithAttributedString(attstr as CFAttributedString)
            
            let path = CGMutablePath()
            path.addRect(CGRect(x: 0, y: 0, width: frame.size.width, height: 100))
            
            let frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
            
            if let lines = CTFrameGetLines(frame) as? [CTLine] {
                lines.forEach({
                    let linerange = CTLineGetStringRange($0)
                    let range = NSMakeRange(linerange.location, linerange.length)
                    let string = (text as NSString).substring(with: range)
                    strings.append(string)
                })
            }
            return strings
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS中求出label中文字的行数和每一行的内容

          本文链接:https://www.haomeiwen.com/subject/mardzrtx.html