将UITextView内容作为行数组

时间:2013-12-17 04:19:43

标签: ios uitextview sizewithfont

有什么方法可以将UITextView的内容检索为包装的数组?

我可以用愚蠢的方式做到这一点,并用sizeWithFont逐字测量,看看它可以包裹的地方,但我希望有更聪明的方法吗?

干杯

4 个答案:

答案 0 :(得分:1)

我猜你可以使用attributeString。使用此功能,您可以为一系列字符或单词设置颜色。获取textview中的文本长度并执行attributeString工作

答案 1 :(得分:1)

你可以使用textviews布局管理器来获取那些。但是可以从ios 7获得。 你可以使用布局管理器的方法 - ( enumerateLineFragmentsForGlyphRange:usingBlock:)

以下代码打印结果如图所示 - >

_textView.text =@"abcdsakabsbdkjflk sadjlkasjdlk asjkdasdklj asdpaandjs bajkhdb hasdskjbnas kdbnkja sbnkasj dbkjasd kjk aj";

NSLog(@"%d",_textView.text.length); // length here is 104.

[_textView.layoutManager enumerateLineFragmentsForGlyphRange:NSMakeRange(0, 104) usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *textContainer, NSRange glyphRange, BOOL *stop) {

NSLog(@"rect %@ - usedRect %@ - glymph Rangle %d %d -",NSStringFromCGRect(rect),NSStringFromCGRect(usedRect),glyphRange.location,glyphRange.length);
    }]

打印结果 - >

2013-12-17 12:48:40.250 testEmpty[675:a0b] rect {{0, 0}, {200, 13.8}} - usedRect {{0, 0}, {176.08398, 13.8}} - glymph Rangle 0 31 -
2013-12-17 12:48:40.251 testEmpty[675:a0b] rect {{0, 13.8}, {200, 13.8}} - usedRect {{0, 13.8}, {182.11328, 13.8}} - glymph Rangle 31 31 -
2013-12-17 12:48:40.251 testEmpty[675:a0b] rect {{0, 27.6}, {200, 13.8}} - usedRect {{0, 27.6}, {168.75977, 13.8}} - glymph Rangle 62 28 -
2013-12-17 12:48:40.252 testEmpty[675:a0b] rect {{0, 41.400002}, {200, 13.8}} - usedRect {{0, 41.400002}, {82.035156, 13.8}} - glymph Rangle 90 14 -

所以在每次阻止运行时,你都会得到glymphRange.length作为该行中使用的字符串的长度。

答案 2 :(得分:0)

我是这样做的,似乎足够快。

-(void) _printCharactersAndPositionsForLines:(UITextView*) textView{
    NSString * text = textView.text;
    if (text != nil){
        float currentLineY = -1;
        int currentLineIndex = -1;
        for (int charIndex = 0; charIndex < [text length]; charIndex++){
            UITextPosition *charTextPositionStart = [textView positionFromPosition:textView.beginningOfDocument offset:charIndex];
            UITextPosition *charTextPositionEnd = [textView positionFromPosition:charTextPositionStart offset:+1];
            UITextRange *range = [textView textRangeFromPosition:charTextPositionStart toPosition:charTextPositionEnd];
            CGRect rectOfChar = [textView firstRectForRange:range];
            if (rectOfChar.origin.y > currentLineY){
                currentLineY = rectOfChar.origin.y;
                currentLineIndex++;
                // new line
                NSLog(@"line [%i] starts with '%@' at character index %i",currentLineIndex,[text substringWithRange:NSMakeRange(charIndex, 1)],charIndex);
            }
        }
    }
}

答案 3 :(得分:0)

使用属性字符串,我实现了以下功能:

扩展名NSAttributedString {     函数行(大小:CGSize)-> [NSAttributedString] {         让textStorage = NSTextStorage(attributedString:self)         让layoutManager = NSLayoutManager()         textStorage.addLayoutManager(layoutManager)

    let textContainer = NSTextContainer(size: size)
    textContainer.lineFragmentPadding = 0.0
    layoutManager.addTextContainer(textContainer)

    var lines = [NSAttributedString]()
    layoutManager.enumerateLineFragments(forGlyphRange: layoutManager.glyphRange(forBoundingRect: CGRect(origin: .zero, size: size), in: textContainer)) { (_, _, _, range, _) in
        let charactersRange = layoutManager.characterRange(forGlyphRange: range, actualGlyphRange: nil)
        lines.append(self.attributedSubstring(from: charactersRange))
    }
    return lines
}

}

因此,例如,您可以这样运行它:

NSAttributedString(string: "J'aime le fromage. Donnez moi du fromage. Munster, Reblochon, ...\n Ou bien du saucisson", attributes: [
    .font: UIFont.systemFont(ofSize: 12, weight: .semibold)
]).lines(in: CGSize(width: 130, height: .max))

将输出

  

[“ J'aime le fromage。”,“ Donnez moi du”,“ fromage。Munster”,“ Reblochon ...”,“ Ou bien du saucisson”]

相关问题