使用给定字符串查找具有恒定宽度和高度的UITextView中的字符数?

Jac*_*ery 8 nsstring uitextview ios nsrange swift

在我的应用程序中,如果文本输入很大,我需要将Read更多地设置到文本视图中.所以我的方法是找到适合文本视图的字符串范围并附加查看更多信息.有没有办法实现它在Swift中.要求是模拟阅读更多选项,以便像Facebook一样在详细视图中显示完整的文本.

kga*_*dis 6

你正在寻找的功能是CTFramesetterSuggestFrameSizeWithConstraints.从本质上讲,它允许您找出适合某个帧的字符数.您可以使用该数字剪切当前文本并插入按钮.

我为UILabel的子类编写了这个函数的实现:

- (NSInteger)numberOfCharactersThatFitLabel {

    // Create an 'CTFramesetterRef' from an attributed string
    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)self.font.fontName, self.font.pointSize, NULL);
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:(__bridge id)fontRef forKey:(id)kCTFontAttributeName];
    CFRelease(fontRef);
    NSAttributedString *attributedString  = [[NSAttributedString alloc] initWithString:self.text attributes:attributes];
    CTFramesetterRef frameSetterRef = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);

    // Get a suggested character count that would fit the attributed string
    CFRange characterFitRange;
    CTFramesetterSuggestFrameSizeWithConstraints(frameSetterRef, CFRangeMake(0,0), NULL, CGSizeMake(self.bounds.size.width, self.numberOfLines*self.font.lineHeight), &characterFitRange);
    CFRelease(frameSetterRef);
    return (NSInteger)characterFitRange.length;
}
Run Code Online (Sandbox Code Playgroud)

这是一个bog帖子,用于完全实现将随机文本剪切到指定行数并向其添加"查看更多"文本.


hat*_*ike 5

kgaidis 出色答案的粗略快速翻译。

extension UILabel {

    func numberOfCharactersThatFitLabel() -> Int {
        let fontRef = CTFontCreateWithName(self.font.fontName as CFStringRef, self.font.pointSize, nil)
        let attributes = NSDictionary(dictionary: [kCTFontAttributeName : fontRef])
        let attributeString = NSAttributedString(string: text!, attributes: attributes as? [String : AnyObject])
        let frameSetterRef = CTFramesetterCreateWithAttributedString(attributeString as CFAttributedStringRef)

        var characterFitRange:CFRange

        CTFramesetterSuggestFrameSizeWithConstraints(frameSetterRef, CFRangeMake(0, 0), nil, CGSizeMake(bounds.size.width, CGFloat(numberOfLines)*font.lineHeight), &characterFitRange)

        return characterFitRange.length
    }
}
Run Code Online (Sandbox Code Playgroud)


noo*_*oob 5

这是 Swift 4UITextView扩展

extension UITextView {

    func numberOfCharactersThatFitTextView() -> Int {
        let fontRef = CTFontCreateWithName(font!.fontName as CFString, font!.pointSize, nil)
        let attributes = [kCTFontAttributeName : fontRef]
        let attributedString = NSAttributedString(string: text!, attributes: attributes as [NSAttributedStringKey : Any])
        let frameSetterRef = CTFramesetterCreateWithAttributedString(attributedString as CFAttributedString)

        var characterFitRange: CFRange = CFRange()

        CTFramesetterSuggestFrameSizeWithConstraints(frameSetterRef, CFRangeMake(0, 0), nil, CGSize(width: bounds.size.width, height: bounds.size.height), &characterFitRange)
        return Int(characterFitRange.length)

    }
}
Run Code Online (Sandbox Code Playgroud)

如果文本超出了您的预期,您可以尝试调整CTFramesetterSuggestFrameSizeWithConstraints函数参数中的高度值。