gpt4 book ai didi

ios - CoreText 属性字符串高度计算不准确

转载 作者:行者123 更新时间:2023-11-28 19:02:11 24 4
gpt4 key购买 nike

CoreText 没有给出属性字符串的正确高度(它短了一行或更多)。我在 SO 上看到很多关于此的帖子,但无法理解或找到解决方案。有人可以解释 Core Text 高度计算的工作原理吗?这是我编写的示例代码,显示了不准确的高度计算。

上下文

我有一个 Collection View ,其中单元格的高度由其中的内容决定。我在单元格中显示文本段落。我想通过使用核心文本进行高度计算来节省一些性能。我已经看到,通过核心文本的高度计算,我可以节省大约 300 毫秒。

代码

//高度计算

+ (CGFloat)getHeight
{
NSString *text = @"The Apple HIG recommends to use a common color for links and buttons and we did just that. By using the same color throughout the app we trained the user to always associate blue to a link.The Apple HIG recommends to use a common color for links and buttons and we did just that.By using the same color throughout the app we trained the user to always associate blue to a link.";

NSAttributedString *attrStr = [self attributedString:text withLinespacing:3 withLineBreakMode:NSLineBreakByWordWrapping];

CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attrStr));
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(frameSetter,
CFRangeMake(0, attrStr.length),
NULL,
CGSizeMake(320, 9999),
NULL);

return suggestedSize.height;
}

//在Cell即将显示时加载相同的文本

- (void)loadData
{
NSString *text = @"The Apple HIG recommends to use a common color for links and buttons and we did just that.By using the same color throughout the app we trained the user to always associate blue to a link.The Apple HIG recommends to use a common color for links and buttons and we did just that.By using the same color throughout the app we trained the user to always associate blue to a link.";

NSAttributedString *attrStr = [[self class] attributedString:text withLinespacing:3 withLineBreakMode:NSLineBreakByWordWrapping];

// UILabel element
self.textLabel.attributedText = attrStr;

self.layer.borderColor = [UIColor blueColor].CGColor;
self.layer.borderWidth = 1.0f;
}

//生成带前导、字体和换行符的属性字符串

+ (NSAttributedString *)attributedString:(NSString *)string
withLinespacing:(CGFloat)linespacing
withLineBreakMode:(NSLineBreakMode)lineBreakMode
{
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:string];
NSInteger strLength = [string length];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = linespacing;
style.lineBreakMode = lineBreakMode;

[attrStr addAttributes:@{NSParagraphStyleAttributeName: style,
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15]} range:NSMakeRange(0, strLength)];

return attrStr;
}

以上代码使用core text计算高度,使用UILabel显示文字。 UILabel 对单元格 {Top:17, Leading:13px, Trailing:13px}

有 3 个约束

enter image description here

最佳答案

CTFramesetterSuggestFrameSizeWithConstraints 已知存在错误,返回不正确的高度值。您遇到的缺失行错误很常见,据我所知没有好的解决方案,只有丑陋的解决方法,永远不会给出 100% 准确的结果。

对于 iOS7 及更高版本,我建议转移到 TextKit。不知何故,在内部执行的计算确实可以正常工作,同时也基于 Core Text。使用 NSLayoutManagerusedRectForTextContainer: 返回正确的结果。

可以看到更完整的答案here .虽然不是 100% 完全切合主题,但还是有一些关于 Core Text 计算错误的讨论。

关于ios - CoreText 属性字符串高度计算不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23851893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com