欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > ios 设置行距和获取文本行数

ios 设置行距和获取文本行数

2024/10/25 0:29:46 来源:https://blog.csdn.net/qq_32540053/article/details/140490315  浏览:    关键词:ios 设置行距和获取文本行数

设置文本行距

    UILabel *label = [[UILabel alloc] init];label.font = [UIFont systemFontOfSize:12];label.numberOfLines = 0;label.lineBreakMode = NSLineBreakByWordWrapping;label.textColor = [UIColor colorWithHexString:@"B3B3B3"];label.text = textDes;//设置行距NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];style.lineSpacing  = 3;NSMutableDictionary *attributes  = [NSMutableDictionary dictionary];[attributes setObject:style forKey:NSParagraphStyleAttributeName];label.attributedText = [[NSAttributedString alloc] initWithString:label.text attributes:attributes];

获取文本行数


//获取文本行数
- (int)getNumberOfLinesWithText:(NSMutableAttributedString *)text andLabelWidth:(CGFloat)width {CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)text);CGMutablePathRef Path = CGPathCreateMutable();CGPathAddRect(Path, NULL ,CGRectMake(0 , 0 , width, INT_MAX));CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), Path, NULL);// 得到字串在frame中被自动分成了多少个行。CFArrayRef rows = CTFrameGetLines(frame);// 实际行数CFIndex numberOfLines = CFArrayGetCount(rows);CFRelease(frame);CGPathRelease(Path);CFRelease(framesetter);return numberOfLines;
}

使用

   //设置行距NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];style.lineSpacing  = 3;NSMutableDictionary *attributes  = [NSMutableDictionary dictionary];[attributes setObject:style forKey:NSParagraphStyleAttributeName];//获取行数int nums = [self getNumberOfLinesWithText:[[NSMutableAttributedString alloc] initWithString:self.permissionDes attributes:attributes] andLabelWidth:SCREENWIDTH- 40];

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com