iOS每日一记之-------------------获取label的行数。。。。。恩
2016-04-25 15:46
615 查看
-(NSArray *)getSeparatedLinesFromLabel {
NSString *text = [self text];
UIFont *font = [self font];
CGRect rect = [self 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 获取到NSarray.count 就是label的行数相关文章推荐
- 关于iOS定位问题
- iOS开发用boundingRectWithSize:options:attributes:context:写的一个分类
- iOS 常用知识总结
- [iOS]FDFullscreenPopGesture 全局返回手势 与 pan手势冲突
- Attributes-iOS初学
- iOS- 利用AFNetworking3.0+(最新AFN) - 实现文件断点下载
- iOS开发需要哪些图片?
- iOS 不建议使用PCH文件-----使用PCH文件的坏处
- iOS中使用RSA加密
- 好文!关于iOS下的正则表达式实战案例
- 好文!关于iOS下的正则表达式实战案例
- iOS - Cell高度不固定的情况处理
- iOS layer层的转场动画
- iOS中内存管理
- iOS对象数组排序
- iOS 图像的滤镜效果实现
- iOS nslog
- dSYM 崩溃信息分析工具
- iOS打包步骤
- 《Motion Design for iOS》(一)