• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - NSAttributedString 不适用于 SCNText

[复制链接]
菜鸟教程小白 发表于 2022-12-12 08:53:31 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想在 SceneKit 场景中显示一些文本,其中字符具有不同的颜色。 documentation of SCNText状态:

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.

我试过这个,但它不起作用。 SCNText 似乎忽略了我的字符串的属性。我检查了我的 NSAttributedString 的内容是否正确,并将其添加到 UILabel 中,并按预期显示。

为什么 3D 文本没有着色?

这是一个小型测试应用程序(来自 Single View Application 模板)和屏幕截图:

Screenshot of test 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



Best Answer-推荐答案


直接回答您的问题:“为什么 3D 文本没有着色?”

颜色可能不被视为“风格”的一部分,因为他们在文档中使用了这个词。这也可能是用错词,因为它是对文本质量的描述,在它曾经使用过的每个地方都会弯曲和扭曲。

他们的意思很可能是诸如重量、对齐、下划线、删除线、字距调整和前导之类的东西......这种性质的东西由 SceneKit 解释。但只有反复试验才能确定什么有效,什么无效。

Apple 应该提供一个表格,其中包含 SceneKit 识别和使用的文本的属性/质量列表。

我可以在 Core Text 和 NSAttributedString 文档中找到“样式”的唯一用法,建议这些框架考虑使用这个词来对每个段落的这些品质进行分组,这在考虑 SceneKit 文本时也几乎没有帮助。


编辑:关于 3D 和 Material 的其他内容,以防万一这对您来说是新闻:

3D 语言中的“ Material ”在 3D 对象上创建颜色、对光的响应、反射性和其他 Material 质量的印象,需要创建和应用。这不像在 2D 思维中说“让 objectABC 变成红色”那么简单。

单个文本对象(在 3D 中,不是您在 Cocoa 中如何看待这些东西)可能无法在 SceneKit 中使用不同的 Material 和/或 Material ID 自动制作。 Material ID 是在大多数人创建几何体和 Material 的 3ds Max 中,如何将不同 Material 应用于单个对象的不同部分。

不幸的是,SceneKit 不遵循此约定,而是使用“元素”作为几何对象的一部分,每个对象都是与您提供该几何图形的任何 Material 相对应的索引的一部分。

您可以在此处阅读更多相关信息:

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

因此,要获得您要查找的内容,您可能需要制作 3 个文本对象,并为每个对象提供所需颜色的独特 Material ,或将 3 个单词对象分解为 3 个元素,然后有趣地猜测如何在每个元素上添加正确的 Material 。

编辑:忽略上述段落的最后一部分。您需要制作 3 个文本对象才能获得所需的外观。 Scene Kit 中 Material 分布的元素特性是保留在 Text Objects 上的,所以不能将文字在单词级别分解成不同的 Elements。

来自文档:

A text geometry may contain one, three, or five geometry elements:

  • If its extrusionDepth property is 0.0, the text geometry has one element corresponding to its one visible side.

  • If its extrusion depth is greater than zero and its chamferRadius property is 0.0, the text geometry has three elements, corresponding to its front, back, and extruded sides.

  • If both extrusion depth and chamfer radius are greater than zero, the text geometry has five elements, corresponding to its front, back, extruded sides, front chamfer, and back chamfer.

关于ios - NSAttributedString 不适用于 SCNText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32610914/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap