如何在 Objective C 中获取随机 unicode 字符

发布于 2024-11-07 01:09:36 字数 886 浏览 5 评论 0 原文

我想知道如何获取随机 Unicode 字符,以便每次运行时都会生成不同的值。这是我到目前为止获得的角色:

    NSFont *arialUnicode = [[NSFontManager sharedFontManager] 
    fontWithFamily:@"Arial Unicode MS"
    traits:0 
    weight:5 
    size:dirtyRect.size.height*0.6];

    NSGlyph *glyphs = (NSGlyph *)malloc(sizeof(NSGlyph) * 1); 

    CTFontGetGlyphsForCharacters((CTFontRef)arialUnicode, (const UniChar *)L"\u2668", (CGGlyph *)glyphs, 1);

它改编自我朋友发给我的绘图教程:http://cocoawithlove.com/2011/01/advanced-drawing-using-appkit.html

实际上是一个很好的教程:)

但我想知道如何使用任何字符而不仅仅是花心。我发现购买将 2668 的值更改为行中的其他值:

CTFontGetGlyphsForCharacters((CTFontRef)arialUnicode, (const UniChar *)L"\u2668", (CGGlyph *)glyphs, 1);

我可以让它改变字符,但我想自动化它,以便它自动选择不同的字符。

谢谢

I was wondering how to get a random Unicode character so that every time this is run it generates a different value. This is what I have so far to get a character:

    NSFont *arialUnicode = [[NSFontManager sharedFontManager] 
    fontWithFamily:@"Arial Unicode MS"
    traits:0 
    weight:5 
    size:dirtyRect.size.height*0.6];

    NSGlyph *glyphs = (NSGlyph *)malloc(sizeof(NSGlyph) * 1); 

    CTFontGetGlyphsForCharacters((CTFontRef)arialUnicode, (const UniChar *)L"\u2668", (CGGlyph *)glyphs, 1);

It is adapted from a drawing tutorial my friend sent me: http://cocoawithlove.com/2011/01/advanced-drawing-using-appkit.html

Quite a nice tutorial actually :)

But I want to know how to use any character as opposed to just the floral heart. I have found that buy changing the value of 2668 to some other value in the line:

CTFontGetGlyphsForCharacters((CTFontRef)arialUnicode, (const UniChar *)L"\u2668", (CGGlyph *)glyphs, 1);

I can make it change character but i want to automate this so that it automatically chooses different characters.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱人如己 2024-11-14 01:09:36

如果你真的想要一个随机字符,比如

UniChar uc = arc4random() % (0xffffu + 1);        
CTFontGetGlyphsForCharacters((CTFontRef)arialUnicode, &uc, (CGGlyph *)glyphs, 1);

但是取决于你想要做什么

  • 在 Cocoa 中显示文本有更简单的方法,特别是 NSTextField
  • Unicode 中有如此多的字符,单个字体不太可能包含所有字形。

您真的想要一个随机的 unicode 代码点还是想要从可用字符的子集中进行选择?请参阅 http://www.unicode.org/charts/ 了解 Unicode 的作用覆盖。

If you truly want a random character, something like

UniChar uc = arc4random() % (0xffffu + 1);        
CTFontGetGlyphsForCharacters((CTFontRef)arialUnicode, &uc, (CGGlyph *)glyphs, 1);

But depending on what you are trying to do

  • There are much easier ways to display text in Cocoa, particularly NSTextField
  • There are so many characters in Unicode it's highly unlikely a single font will contain all the glyphs.

Do you really want a random unicode code point or do you want to select from a subset of the available characters? See http://www.unicode.org/charts/ to get an idea of just how much Unicode covers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文