开发者

How to get a random unicode character in objective C

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)arial开发者_如何学PythonUnicode, (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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜