gpt4 book ai didi

ios4 - 调整 CATextLayer 大小以适合 iOS 上的文本

转载 作者:行者123 更新时间:2023-12-02 18:06:01 33 4
gpt4 key购买 nike

到目前为止,我的所有研究似乎都表明不可能准确地做到这一点。一开始我唯一可用的两个选择是:

a) Using a Layout manager for the CATextLayer - not available on iOS as of 4.0

b) Use sizeWithFont:constrainedToSize:lineBreakMode: and adjust the frame of the CATextLayer according to the size returned here.

选项 (b) 是最简单的方法,应该可行。毕竟,它与 UILabels 完美配合。但是,当我将相同的帧计算应用于 CATextLayer 时,框架总是比预期或需要的大一点。

事实证明,CATextLayers 和 UILabels 中的行距(对于相同的字体和大小)是不同的。因此,sizeWithFont(其行距计算将与 UILabels 的行距计算相匹配)不会返回 CATextLayers 的预期大小。

通过使用 UILabel 与 CATextLayer 打印相同的文本并比较结果,进一步证明了这一点。第一行中的文本完美重叠(使用相同的字体),但 CATextLayer 中的行距仅比 UILabel 中的行距短一点。 (抱歉,我现在无法上传屏幕截图,因为我已经拥有的屏幕截图包含 secret 数据,而且我目前没有时间制作示例项目来获取干净的屏幕截图。稍后我将上传它们以供后代使用我有时间)

这是一个奇怪的差异,但我认为可以通过为我在那里使用的 NSAttributedString 指定适当的属性来调整 CATextLayer 中的间距,但情况似乎并非如此。查看 CFStringAttributes.h,我找不到可能与行间距相关的单个属性。

底线:

因此,在需要图层适合其文本的情况下,似乎无法在 iOS 上使用 CATextLayer。我在这一点上是正确的还是我错过了什么?

附注:

  1. 我想使用 CATextLayer 和 NSAttributedString 的原因是因为要显示的字符串在不同点处具有不同的颜色。我想我只需要像往常一样回去手工绘制字符串......当然,总是可以选择修改 sizeWithFont 的结果以获得正确的行高。

  2. 稍微滥用“代码”标签以使帖子更具可读性。

  3. 我无法使用“CATextLayer”标记该帖子 - 令人惊讶的是,目前不存在此类标记。如果有足够声誉的人遇到此帖子,请相应地标记它。

最佳答案

试试这个:

- (CGFloat)boundingHeightForWidth:(CGFloat)inWidth withAttributedString:(NSAttributedString *)attributedString {
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString( (CFMutableAttributedStringRef) attributedString);
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(inWidth, CGFLOAT_MAX), NULL);
CFRelease(framesetter);
return suggestedSize.height;
}

您必须将 NSString 转换为 NSAttributedString。对于CATextLayer,您可以使用以下CATextLayer子类方法:

- (NSAttributedString *)attributedString {

// If string is an attributed string
if ([self.string isKindOfClass:[NSAttributedString class]]) {
return self.string;
}

// Collect required parameters, and construct an attributed string
NSString *string = self.string;
CGColorRef color = self.foregroundColor;
CTFontRef theFont = self.font;
CTTextAlignment alignment;

if ([self.alignmentMode isEqualToString:kCAAlignmentLeft]) {
alignment = kCTLeftTextAlignment;

} else if ([self.alignmentMode isEqualToString:kCAAlignmentRight]) {
alignment = kCTRightTextAlignment;

} else if ([self.alignmentMode isEqualToString:kCAAlignmentCenter]) {
alignment = kCTCenterTextAlignment;

} else if ([self.alignmentMode isEqualToString:kCAAlignmentJustified]) {
alignment = kCTJustifiedTextAlignment;

} else if ([self.alignmentMode isEqualToString:kCAAlignmentNatural]) {
alignment = kCTNaturalTextAlignment;
}

// Process the information to get an attributed string
CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);

if (string != nil)
CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef)string);

CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)), kCTForegroundColorAttributeName, color);
CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)), kCTFontAttributeName, theFont);

CTParagraphStyleSetting settings[] = {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));
CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)), kCTParagraphStyleAttributeName, paragraphStyle);
CFRelease(paragraphStyle);

NSMutableAttributedString *ret = (NSMutableAttributedString *)attrString;

return [ret autorelease];
}

HTH。

关于ios4 - 调整 CATextLayer 大小以适合 iOS 上的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6198084/

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