Я хочу отобразить текст внутри сцены SceneKit, где символы имеют другой цвет. В documentation of SCNText состоянии:NSAttributedString не работает с SCNText
При создании текста геометрии из приписываемой строки, стили SceneKit текст в соответствии с атрибутами в строке, и свойство объекта SCNText определяет стиль по умолчанию для частей строки, не имеют атрибутов стиля.
Я пробовал это, но он не работает. SCNText, похоже, игнорирует атрибуты моей строки. Я проверил, правильно ли сохранено содержимое моей NSAttributedString и добавлено его в UILabel, где он отображается по назначению.
Почему 3D-текст не окрашен?
Вот небольшое тестовое приложение (из шаблона Single View Application) и скриншот:
#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