I want to display some text inside a SceneKit scene, where the characters have a different color. The documentation of SCNText states:
When you create a text geometry from an attributed string, SceneKit styles the text according to the attributes in the string, and the properties of the SCNText object determine the default style for portions of the string that have no style attributes.
I tried this but it doesn't work. SCNText seems to ignore the attributes of my string. I checked if the content of my NSAttributedString is correct and added it to an UILabel, where it displays as intended.
Why is the 3D text not colored?
Here is a small test application (from the Single View Application template) and a screenshot:
#import "ViewController.h"
#import <SceneKit/SceneKit.h>
@interface ViewController ()
@end
@implementation ViewController
- (void) viewDidLoad
{
[super viewDidLoad];
NSAttributedString* str = [ViewController makeSomeText];
[self addSceneViewWithText: str];
[self addLabelWithText: str];
}
+ (NSMutableAttributedString*) makeSomeText
{
NSDictionary* redAttr = @{NSForegroundColorAttributeName : [UIColor redColor]};
NSDictionary* greenAttr = @{NSForegroundColorAttributeName : [UIColor greenColor]};
NSDictionary* blueAttr = @{NSForegroundColorAttributeName : [UIColor blueColor]};
NSAttributedString* redStr = [[NSAttributedString alloc] initWithString: @"red" attributes: redAttr];
NSAttributedString* greenStr = [[NSAttributedString alloc] initWithString: @"green" attributes: greenAttr];
NSAttributedString* blueStr = [[NSAttributedString alloc] initWithString: @"blue" attributes: blueAttr];
NSMutableAttributedString* str = [[NSMutableAttributedString alloc] init];
[str appendAttributedString: redStr];
[str appendAttributedString: greenStr];
[str appendAttributedString: blueStr];
return str;
}
- (void) addSceneViewWithText: (NSAttributedString*) string
{
SCNView* view = [[SCNView alloc] initWithFrame: self.view.bounds];
view.scene = [SCNScene scene];
view.backgroundColor = [UIColor grayColor];
view.autoenablesDefaultLighting = true;
view.allowsCameraControl = true;
SCNNode* root = view.scene.rootNode;
[self.view addSubview: view];
SCNNode* cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.automaticallyAdjustsZRange = true;
[root addChildNode: cameraNode];
SCNText* text = [SCNText textWithString: string extrusionDepth: 3];
// text.firstMaterial.diffuse.contents = [UIColor yellowColor];
SCNNode* textNode = [SCNNode nodeWithGeometry: text];
textNode.rotation = SCNVector4Make (1, 0, 0, 0.5f);
[root addChildNode: textNode];
SCNVector3 text_center;
float dummy;
[text getBoundingSphereCenter: &text_center radius: &dummy];
textNode.position = SCNVector3Make (-text_center.x, -text_center.y, -150);
}
- (void) addLabelWithText: (NSAttributedString*) string
{
CGRect bounds = self.view.bounds;
UILabel* label = [[UILabel alloc] initWithFrame: CGRectMake (0, 50, bounds.size.width, 50)];
label.attributedText = string;
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview: label];
}
- (void) didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end