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

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

NSMutableParagraphStyle *style = \[NSMutableParagraphStyle allocinit];

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;
相关推荐
AIsoft_868911 分钟前
科会通录音工具倍速回放怎么切换速度
ios·智能手机
老李IT笔记1 小时前
设备显示在线但指令没回执:三类静默失效的排查顺序
ios·iphone
芯械研究所2 小时前
她宁、益博思、倍思、绿联哪款ipad电容笔好?四款电容笔款式实测解读
ios·ipad·电容笔·电容笔推荐·电容笔测评
Yannik17 小时前
iOS CocoaPods私有库完整搭建+踩坑实战总结
ios·cocoapods
终端安全笔记20 小时前
绕过工具把 iOS 27 列进支持范围:从它的更新节奏能读出什么
安全·ios
AIsoft_868920 小时前
线上培训录屏怎么转文字?科会通操作指南
ios·智能手机
weixin_403810131 天前
苹果脚本编写软件有哪些?怎么选择 iOS脚本技术路线
ios
2501_915918412 天前
在 Xcode 中运行和调试单元测试的完整指南
ide·vscode·macos·ios·单元测试·个人开发·xcode
茉莉玫瑰花茶2 天前
GO [ 标准库 · HTTP 高可用 ]
ios·iphone·xcode