使用 CoreText 进行段落对齐
我有以下代码来使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑
CTParagraphStyleCreate
由于某种原因返回NULL
导致dictionaryWithObjectsAndKeys:
创建一个空字典。但我看不到你的段落样式设置有任何错误。看看我的
CoreTextLabel.m
使用段落样式设置的类源。I would suspect that
CTParagraphStyleCreate
returnsNULL
for some reason causingdictionaryWithObjectsAndKeys:
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.