我有下一个问题(下面的示例代码):
我使用CATextLayerNSAttributedString显示样式化的文本。一切正常,直到我将笔触添加到属性。当我添加笔画时-文本变得不可见,并且在显示中我只能看到笔画。
拜托,我不明白会发生什么,为什么我不能同时使用文字和笔画?
谢谢。

UIFont* font = [UIFont fontWithName: size:];
UIColor* strokeColor = [UIColor colorWithRed: green: blue: alpha:1.f];
UIColor* foregroundColor = [UIColor colorWithRed: green: blue: alpha:1.f];
CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithNameAndSize(...);
CTFontRef ctFont = CTFontCreateWithFontDescriptor(...);

NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                            strokeWidth, kCTStrokeWidthAttributeName,
                            [strokeColor CGColor], kCTStrokeColorAttributeName,
                            ctFont, kCTFontAttributeName,
                            [foregroundColor CGColor], kCTForegroundColorAttributeName,
                            nil];

NSAttributedString* attributedText =
[[NSAttributedString alloc] initWithString:text attributes:attributes];

CATextLayer* infoTextLayer = [[[CATextLayer alloc] init] autorelease];
infoTextLayer.string = attributedText;
...
infoTextLayer.frame = frame;
[self.layer addSublayer:infoTextLayer];

最佳答案

对于文本和笔触,您的strokeWidth必须为负,仅笔画必须为正。从Apple Docs:

kCTStrokeWidthAttributeName。此属性解释为字体点大小的百分比,它控制文本绘制模式:正值仅影响笔画。负值表示笔划和填充。

关于ios - 添加笔触时文本不可见,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14414387/

10-11 22:02
查看更多