- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想在 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 文本没有颜色?
这是一个小型测试应用程序(来自单 View 应用程序模板)和屏幕截图:
#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 和 Material 的其他内容,以防万一这对您来说是新闻:
3D 中的“ Material ”会在 3D 对象上创建颜色印象、对光的响应、反射率和 Material 的其他品质,并且需要创建和应用。这远不像在 2D 思维中说“让 objectABC 变红”那么简单。
单个文本对象(在 3D 中,不是您在 Cocoa 中对这些东西的看法)可能无法在 SceneKit 中使用不同的 Material 和/或 Material ID 自动制作。 Material ID 是在大多数人创建几何体和 Material 的 3ds Max 中如何将不同 Material 应用于单个对象的不同部分。
不幸的是,SceneKit 不遵循此约定,而是使用“元素”作为几何对象的一部分,每个元素都是索引的一部分,该索引对应于您提供该几何的任何 Material 。
您可以在这里阅读更多相关信息:
因此,要获得您正在寻找的内容,您可能需要制作 3 个文本对象并为每个对象提供您想要的颜色的独特 Material ,或者将 3 个单词对象分解为 3 个元素,然后有趣地猜测如何将正确的 Material 放到每个元素上。
编辑:忽略上一段的最后一部分。您需要制作 3 个文本对象以获得您想要的外观。 Scene Kit 中素材分布的元素特性保留在文本对象上,因此您不能将单词级别的文本分解为不同的元素。
来自文档:
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/
我对 Core Text 的行距一无所知。我正在使用 NSAttributedString 并在其上指定以下属性:- kCTFont属性名称- kCTParagraphStyleAttributeNa
我想显示 3 行 NSAttributedString。有没有办法根据宽度和行数确定所需的高度? 而且我不想创建 UILabel 来进行大小计算,因为我希望计算在后台线程中完成。 最佳答案 我想知道为
我想用另一个 NSAttributedString 替换 NSAttributedString 的子字符串(例如 @"replace")。 我正在寻找与 NSString 的 stringByRepl
我正在尝试在另一个文本中创建多行单独文本以实现以下文本样式。 我已经尝试使用下面的代码来实现目标,但是代码的第三部分产生了一个问题(中等字体) private func createLimit
我有一个 NSAttributedString 并希望在每个 “***” 处拆分它。所以实际上,我想在该字符串出现的任何地方分割一个 NSAttributedString 。结果应该类似于 NSAtt
尝试在 UITextView 中使用 NSAttributedString 显示上标/下标文本在 iOS13 中似乎已损坏 - 除非有人知道否则? 奇怪的是,如果我使用 UIFont systemFo
尝试在 UITextView 中使用 NSAttributedString 显示上标/下标文本在 iOS13 中似乎已损坏 - 除非有人知道否则? 奇怪的是,如果我使用 UIFont systemFo
我有一个富文本编辑器,用户可以在其中编辑字体、颜色等。模型将此信息作为 NSAttributedString 的实例保存在内存中。当我想存储此信息并写入磁盘时,我通过以下函数将属性字符串转换为 htm
这个问题在这里已经有了答案: Cannot convert value of type NSAttributedString.DocumentAttributeKey to .DocumentRea
我想将一个 html 字符串转换为 NSAttributedString,然后处理该字符串(更改颜色、字体大小、字体系列、背景-、前景色...),然后将字符串转换回来从 NSAttributedStr
为什么 NSAttributedString 不继承 NSString ? NSAttributedString 的父类为 NSObject 为什么? 最佳答案 为什么你希望它是 NSString 的
我们正在调用 NSAttributesString 的 initWithRTFD 将 NSData 转换为 attributedString。从 NSTextView 中,我们读取字符串并将其转换为
我想为标签设置样式。没有什么疯狂的,只是一些颜色,改变特定单词的字体大小,等等。我可以用 NSAttributedString 做到这一点,但是在 iOS 7 新引入的 TextKit 中这样做可能/
我正在尝试使用 RTFDFromRange 方法将 NSAttributedString 转换为 NSData。得到这个: No visible @interface for 'NSAttribute
有什么方法可以连接两个 NSAttributedString 吗?或者 NSAttributedString 和 NSString ?我尝试过这样的事情: NSAttributedString *at
有没有人知道那里有任何代码可以让我阅读包含 NSAttributedString 中的图像的 RTF? 是的,我知道有 RTFD 格式和 initWithRTF 方法...但是 initWithRTF
我正在使用 drawGlyphsForGlyphRange渲染 NSAttributedString (在 NSView drawRect 中) 有什么方法可以默认 NSAttributedStrin
这个问题在这里已经有了答案: How to use NSUnderlineStyle.PatternDot (1 个回答) 4 个月前关闭。 我有以下代码尝试为标签的字符串加下划线。 let attr
我已经花了3个小时来解决这个问题,但是有人知道为什么吗?请帮我! 下面的代码将图像添加为属性字符串的附件, UIImage *img = [UIImage imageNamed:imgName]; N
我有一个本地化的字符串: "%@ some text" = "%@ some text"; 格式说明符%@可以出现在本地化字符串中的任何位置。 问题是这个字符串应该是一个NSAttributedStr
我是一名优秀的程序员,十分优秀!