使用 CoreText 进行段落对齐

发布于 2024-12-13 20:31:31 字数 1455 浏览 1 评论 0原文

我有以下代码来使用 CoreText 格式化一段文本。

(...)

CTFontRef fontRef = CTFontCreateWithName((CFStringRef)fontName,
                                         [self.fontSize floatValue], NULL);

CTTextAlignment alignment = kCTJustifiedTextAlignment;

CTParagraphStyleSetting settings[]={
    {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment},
};

CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, 
                                           sizeof(settings)/sizeof(settings[0]));

self.attrs = [NSDictionary dictionaryWithObjectsAndKeys:
         //The following line doesn't work!
         (id)paragraphStyle, kCTParagraphStyleAttributeName,
         (id)self.color.CGColor, kCTForegroundColorAttributeName,
         (id)fontRef, kCTFontAttributeName,
         nil];

CFRelease(fontRef);
CFRelease(paragraphStyle);

NSMutableAttributedString* aString = [[[NSMutableAttributedString alloc] initWithString:@""] autorelease];

(...)

[aString appendAttributedString:[[[NSAttributedString alloc] initWithString:text attributes:attrs] autorelease]];

如果我删除以下属性对(值/键),则一切正常(颜色、字体等)。

(id)paragraphStyle, (NSString*)kCTParagraphStyleAttributeName,

如果我将此属性放在 attrs 字典中,则当我使用 CTFrameGetVisibleStringRange 时,程序会进入无限循环。

我做错了什么?


更新:在 Mattias 回答后,我发现代码不是问题。文本中间丢失了一些 HTML 标记(垃圾),并且由于某种原因,FrameSetter 丢失并返回空范围。当文本上没有 HTML 标签时,一切正常。

I have the following code to format a paragraph of text with CoreText.

(...)

CTFontRef fontRef = CTFontCreateWithName((CFStringRef)fontName,
                                         [self.fontSize floatValue], NULL);

CTTextAlignment alignment = kCTJustifiedTextAlignment;

CTParagraphStyleSetting settings[]={
    {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment},
};

CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, 
                                           sizeof(settings)/sizeof(settings[0]));

self.attrs = [NSDictionary dictionaryWithObjectsAndKeys:
         //The following line doesn't work!
         (id)paragraphStyle, kCTParagraphStyleAttributeName,
         (id)self.color.CGColor, kCTForegroundColorAttributeName,
         (id)fontRef, kCTFontAttributeName,
         nil];

CFRelease(fontRef);
CFRelease(paragraphStyle);

NSMutableAttributedString* aString = [[[NSMutableAttributedString alloc] initWithString:@""] autorelease];

(...)

[aString appendAttributedString:[[[NSAttributedString alloc] initWithString:text attributes:attrs] autorelease]];

If I remove the following attribute pair (value/key), everything works (color, font, etc.).

(id)paragraphStyle, (NSString*)kCTParagraphStyleAttributeName,

If I put this attribute on attrs dictionary, the program enters on an infinite loop when I use CTFrameGetVisibleStringRange.

What I'm doing wrong?


UPDATE: After Mattias answer I figured out that the code wasn't the problem. There were some HTML tags lost in the middle of the text (garbage) and for some reason the FrameSetter was getting lost and returning and empty range. When there's no HTML tags on text everything went ok.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2024-12-20 20:31:31

我怀疑 CTParagraphStyleCreate 由于某种原因返回 NULL 导致 dictionaryWithObjectsAndKeys: 创建一个空字典。但我看不到你的段落样式设置有任何错误。

看看我的 CoreTextLabel.m 使用段落样式设置的类源。

I would suspect that CTParagraphStyleCreate returns NULL for some reason causing dictionaryWithObjectsAndKeys: to create an empty dictionary. But I can't see any error with your paragraph style setting.

Take a look at my CoreTextLabel.m class source which uses paragraph style settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文