使用给定字符串查找具有恒定宽度和高度的UITextView中适合的字符数? [英] Find the number of characters that fits in a UITextView with constant width and height with a given string?

查看:109
本文介绍了使用给定字符串查找具有恒定宽度和高度的UITextView中适合的字符数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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



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



'pre> - (NSInteger的)numberOfCharactersThatFitLabel {

//从归因字符串
CTFontRef fontRef创建 'CTFramesetterRef' = 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);

//获取适合属性字符串的建议字符数
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;
}

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


In my application I need to set Read more into the text view if text input is large.so my approach is to find the range of string that would fit in the text view and append See More to it.Is there any way to achieve it in Swift.The requirement is to emulate the read more option to show the complete text in detailed view on tap like facebook.

解决方案

The function you are looking for is CTFramesetterSuggestFrameSizeWithConstraints. Essentially, it allows you to find out the number of characters that fit in a certain frame. You can use that number to cut off your current text and insert a button.

I wrote an implementation of this function for a subclass of a 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;
}

Here's a bog post for the full implementation of cutting off random text to a specified number of lines and appending "view more" text to it.

这篇关于使用给定字符串查找具有恒定宽度和高度的UITextView中适合的字符数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆