簡體   English   中英

NSAttributedString不適用於SCNText

[英]NSAttributedString doesn't work with SCNText

我想在SceneKit場景中顯示一些文本,其中的角色具有不同的顏色。 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

直接回答您的問題:“為什么3D文本沒有着色?”

顏色可能不被視為“樣式”的一部分,因為他們在文檔中使用了單詞。 可能也是用錯了一個詞,因為它是對文本品質的描述,該品質在使用過的每個位置都會彎曲和扭曲。

他們可能要說的是重量,對齊,下划線,刪除線,字距調整和引導...這類東西由SceneKit解釋。 但是只有反復試驗才能確定什么有效和無效。

Apple應該提供一個表,其中包含SceneKit可以識別和使用的文本的屬性/質量列表。

我可以在Core Text和NSAttributedString文檔中找到“樣式”的唯一用法,建議這些框架考慮將每個段落的這些質量分組的單詞,這在考慮SceneKit文本時也幾乎沒有幫助。


編輯:有關3D和材質的其他信息,以防萬一這對您來說是新聞:

3D語言中的“材料”會在3D對象上創建顏色印象,對光的響應,反射性和其他質量的材料,因此需要創建和應用。 這並不像說二維思維那樣簡單地說“使objectABC為紅色”。

一個單一的文本對象(以3D方式表達,而不是您對Cocoa中的這些東西的看法)可能無法通過SceneKit中的不同材質和/或材質ID自動生成。 材質ID是將不同的材質應用到單個對象的不同部分的方式,例如3ds Max,大多數人在其中創建幾何圖形和材質。

不幸的是,SceneKit沒有遵循此約定,而是使用“元素”作為幾何對象的一部分,每個對象都是索引的一部分,該索引與您提供該幾何的任何材料相對應。

您可以在此處了解更多信息:

https://developer.apple.com/library/ios/documentation/SceneKit/Reference/SCNGeometry_Class/index.html#//apple_ref/doc/uid/TP40012000-CLSCHSCNGeometry-SW15

因此,要獲得所需的內容,您可能需要制作3個文本對象,並為每個對象提供所需顏色的獨特材料, 或者將3字對象分解為3個元素,然后進行有趣的猜測如何將正確的材料放到每個材料上。

編輯:忽略上一段的最后一部分。 您需要制作3個文本對象才能獲得想要的外觀。 Scene Kit中材質分布的元素功能保留在“文本對象”上,因此您不能將單詞級別的文本分解為不同的元素。

從文檔:

文本幾何可能包含一個,三個或五個幾何元素:

  • 如果其trusionDepth屬性為0.0,則文本幾何圖形具有一個與其可見側相對應的元素。

  • 如果其拉伸深度大於零且chamferRadius屬性為0.0,則文本幾何圖形具有三個元素,分別對應於其正面,背面和拉伸側面。

  • 如果拉伸深度和倒角半徑均大於零,則文本幾何圖形具有五個元素,分別對應於其正面,背面,拉伸面,正面倒角和背面倒角。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM