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

时间:2022-09-17 20:20:58

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:

我想知道如何获得一个随机的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);

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

它改编自我朋友寄给我的绘图教程: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:

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

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

CTFontGetGlyphsForCharacters((CTFontRef)arialUnicode,(const UniChar *)L“\ u2668”,(CGGlyph *)字形,1);

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

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

Thank you

1 个解决方案

#1


1  

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
  • 有更简单的方法可以在Cocoa中显示文本,尤其是NSTextField

  • There are so many characters in Unicode it's highly unlikely a single font will contain all the glyphs.
  • Unicode中有这么多字符,单个字体包含所有字形的可能性极小。

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.

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

#1


1  

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
  • 有更简单的方法可以在Cocoa中显示文本,尤其是NSTextField

  • There are so many characters in Unicode it's highly unlikely a single font will contain all the glyphs.
  • Unicode中有这么多字符,单个字体包含所有字形的可能性极小。

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.

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