iOS中求出label中文字的行数和每一行的内容,带自定义行间距
来源:互联网 发布:淘宝开店考试 编辑:程序博客网 时间:2024/06/16 03:22
1.引入头文件#import
2.如果没设置字间距,行间距,断行模式,使用使用默认的,求出label中文字的行数和每一行的内容,使用下面方法:
- (NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label{ NSString *text = [label text]; UIFont *font = [label font]; CGRect rect = [label frame]; CTFontRef myFont = CTFontCreateWithName(( 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)]; CFRelease(myFont); CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( 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 = ( 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]; CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0])); CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0])); //NSLog(@"''''''''''''''''''%@",lineString); [linesArray addObject:lineString]; } CGPathRelease(path); CFRelease( frame ); CFRelease(frameSetter); return (NSArray *)linesArray;}
3.如果自定义行间距,字间距,段子模式等设置
3.1设置这些参数,封装到一个助手类里面了
+ (NSDictionary *)setTextLineSpaceWithString:(NSString *)string withLineBreakMode:(NSLineBreakMode)lineBreakMode withAlignment:(NSTextAlignment)alignment withFont:(UIFont *)font withLineSpace:(CGFloat)lineSpace withTextlengthSpace:(NSNumber *)textlengthSpace andParagraphSpaceing:(CGFloat)paragraphSpacing { // 1. 创建样式对象 NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; // 2. 每行容纳字符的宽度 style.lineBreakMode = lineBreakMode; // 3. 对齐方式 style.alignment = alignment; // 4. 设置行间距 style.lineSpacing = lineSpace; // 5. 连字符号链接 style.hyphenationFactor = 1.0f; // 6. 首行缩进 style.firstLineHeadIndent = 0.f; // 7. 段间距 style.paragraphSpacing = paragraphSpacing; // 8. 段前间距 style.paragraphSpacingBefore = 0.0f; // 9. 除首行之外其他行缩进 style.headIndent = 0.0f; // 10. 每行容纳字符的宽度 style.tailIndent = 0.0f; NSDictionary *dict = @{NSFontAttributeName : font, NSParagraphStyleAttributeName : style, NSKernAttributeName : textlengthSpace, }; return dict;}
3.2设置label
CGFloat contentLabelWidth = kScreenWidth - 36 * ScaleNumberWidth; CGFloat kContentFontSize = 14.f; CGFloat kContentLineSpace = 1.5f; CGFloat kContentTextLengthSpage = 0.8f; contentLabel.size = CGSizeMake(contentLabelWidth, 1); contentLabel.font = CRFontType5(kContentFontSize); contentLabel.numberOfLines = 0; NSDictionary *contentDict = [CommonClass setTextLineSpaceWithString:content withLineBreakMode:(NSLineBreakByCharWrapping) withAlignment:(NSTextAlignmentLeft) withFont:CRFontType5(kContentFontSize) withLineSpace:kContentLineSpace withTextlengthSpace:[NSNumber numberWithFloat:kContentTextLengthSpage] andParagraphSpaceing:0]; contentLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:content attributes:contentDict]; [contentLabel sizeToFit]; contentLabel.center = CGPointMake(kScreenWidth / 2, CGRectGetMaxY(priceLabel.frame) + 20 * ScaleNumberHeight + contentLabel.height / 2);
3.3在自定义label参数的情况下,计算label中文字的行数和每一行的内容,修改了NSMutableAttributedString参数
- (NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label{ NSString *text = [label text]; UIFont *font = [label font]; CGRect rect = [label frame]; CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL); CGFloat kContentFontSize = 14.f; CGFloat kContentLineSpace = 1.5f; CGFloat kContentTextLengthSpage = 0.8f; NSDictionary *contentDict = [CommonClass setTextLineSpaceWithString:text withLineBreakMode:(NSLineBreakByCharWrapping) withAlignment:(NSTextAlignmentLeft) withFont:CRFontType5(kContentFontSize) withLineSpace:kContentLineSpace withTextlengthSpace:[NSNumber numberWithFloat:kContentTextLengthSpage] andParagraphSpaceing:0]; NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text attributes:contentDict]; [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)]; CFRelease(myFont); CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( 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 = ( 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]; CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0])); CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0])); [linesArray addObject:lineString]; } CGPathRelease(path); CFRelease( frame ); CFRelease(frameSetter); return (NSArray *)linesArray;}
4.效果图如下,字间距和行间距改变了,展示效果和打印的一致
阅读全文
0 0
- iOS中求出label中文字的行数和每一行的内容,带自定义行间距
- 计算label自定义行间距字间距,计算自定义行间距字间距的高度
- 使用富文本在lable上展示不同颜色的文字,自定义label高度,自定义行间距
- 自定义行间距 多行输出 解决DrawText 无法自定义行间距的问题
- [转]iOS UILabel自定义行间距时获取高度
- easyui的自定义行工具栏
- VC 中改变ListCtrl每一行的文字颜色
- 关于CKEditor4.5.6的使用,自定义toolbar配置,上传图片案例(SpringMVC+MyBatis案例),自定义行高,去编辑器的中内容,将编辑器中内容设置到指定的位置等
- JSF中为dataTable组件指定自定义行样式
- 使用Spread Studio for .NET自定义行和列的数量
- 零件库的族添加自定义行
- 控制iOS中文字的字间距与行间距
- UILabel - 自定义行间距,字间距及段间距[并动态调节高度]
- 在主函数内任意输入一个5X6矩阵,编写一函数求出每一行的和放到一个一维数组中,输出此矩阵及其每一行的和。
- label中文字的自适应
- iOS 一个label中显示不同颜色的文字
- IOS中label的文字一个个动态出现
- Ext中TextField的 label和feild的间距的设定
- 区块链开发教程: 使用Javascript发送数据到区块链
- 开发工程师必会的Linux命令
- 人工智能为什么率先应用在这七个领域?
- 比特币挖矿——钱包
- HTML标签
- iOS中求出label中文字的行数和每一行的内容,带自定义行间距
- 检测四边形(多边形)
- 非常有用的Java程序片段
- 抽象类和接口
- vue.js入门笔记 记录
- 内核级驱动对抗Hook ZwSetInformationFile反删除技术
- AngularJS+查询+敏感字+排序+添加
- Andriodjie——OKhttp拦截器
- hdu3709 枚举