当前位置: 首页 > news >正文

NSAttributedString设置折行方式NSLineBreakByTruncatingTail,计算高度出错,高度返回异常。

iOS13上,NSAttributedString设置折行方式NSLineBreakByTruncatingTail,计算高度出错,只返回一行的高度。

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];

style.hyphenationFactor = 1;  // 设置每行的最后单词是否截断,在0.0-1.0之间,默认为0.0,越接近1.0单词被截断的可能性越大, 

设置hyphenationFactor=1,在计算就返回正常高度了。

如果设置不起效。换下面的方法

找到了解决方案。只需将 NSMutableParagraphStyle 上的 setAllowsDefaultTighteningForTruncation 设置为 YES。

参考 cocoa - Making NSTextField not shrink when NSLineBreakByTruncatingTail is set - Stack Overflow

    NSString *highlightTitle = title;//转换参数NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };//将html文本转换为正常格式的文本NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[highlightTitle dataUsingEncoding:NSUnicodeStringEncoding] options:options documentAttributes:nil error:nil];[attributedString removeAttribute:NSParagraphStyleAttributeName range: NSMakeRange(0, attributedString.length)];[attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributedString.length)];NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];//    paragraphStyle.hyphenationFactor = 1;[paragraphStyle setLineSpacing:space];paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;[paragraphStyle setAllowsDefaultTighteningForTruncation:YES];[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];return attributedString;

http://www.lryc.cn/news/248372.html

相关文章:

  • YOLOv8改进 | 2023 | DWRSeg扩张式残差助力小目标检测 (附修改后的C2f+Bottleneck)
  • ssm+vue的物资物流系统的设计与实现(有报告)。Javaee项目,ssm vue前后端分离项目。
  • 纵行科技获评“汽车物流行业优秀技术装备供应商”
  • Chrome和chromedriver版本不匹配导致的UI自动化测试无法运行的问题
  • [go 面试] 深入理解进程、线程和协程的概念及区别
  • 人工智能即将彻底改变你使用计算机的方式
  • 工艺系统所管理数字化实践
  • VsCode学习
  • Javaweb之Vue组件库Element案例的详细解析
  • Bert-VITS2本地部署遇到的错误
  • 【Ambari】HDFS基于Ambari的常规运维
  • WIFI模块(esp-01s)获取网络时间与天气信息
  • 03、K-means聚类实现步骤与基于K-means聚类的图像压缩(2)
  • Condition 源码解析
  • acwing算法基础之数学知识--求组合数进阶版
  • 基础算法:大数除以除以13
  • 软件版本区分
  • Redis高可用之主从复制及哨兵模式
  • 代理模式,dk动态代理,cglib动态代理
  • Vue2系列 -- 组件自动化全局注册(require.context)
  • 【华为OD题库-038】支持优先级的对列-java
  • python爱心代码高级
  • 基于SSM+Vue的社区共享食堂管理系统
  • MYSQL基础知识之【修改数据,删除数据】
  • 【机器学习】交叉验证 Cross-validation
  • Pycharm修改文件默认打开方式 + CSV Editor插件使用
  • shiro整合redis
  • HarmonyOS(七)——@BuilderParam装饰器
  • 展开运算符(...)
  • Apache Flink(二):数据架构演变