开发者

How to show the Font Preview in a mac Application?

I have to show the Font Preview of a font. For that I just Copied the character set from the font and then I showed as the Preview. This is my coding.

     NSString *fontName=@"aFontName";

    CFStringRef CFFontName = (CFStringRef)fontName;
    CTFontRef fontRef = CTFontCreateWithName(CFFontName, 0.0,NULL);

    NSCharacterSet *characterset = (NSCharacterSet *) CTFontCopyCharacterSet (fontRef); 
    unichar idx;
    NSString *str=[[NSString alloc]init];
    for( idx = 0; idx < 65000; idx++ )
    {
        if ([characterset characterIsMember: idx])
        {
            if ( isprint(idx) ) {
                NSString *str2 = [NSString stringWithFormat:@"%c",idx];
                str=[str stringByAppendingString:str2];
            }
            else {
                NSString *str1 = [NSString stringWithFormat:@"%C",idx];
                str=[str stringByAppendingString:str1];
            }
        }


    }

Then Finally in the TextView I showed the Strings from the str as Font Preview. But str has lot of unwanted characters(For the Hindi Language Font it is showing the English character also) . I think it is not the correct way for showing the Preview of a font. Is there is any other wa开发者_JS百科y for showing the font preview like FontBook application in mac?


FontBook is using a private routine to get a sample string from the font. If private routines are okay for your app, then you can simply define:

CFStringRef CTFontCopySampleString(CTFontRef font, int options);

and call like:

NSString* sample = (NSString *) CTFontCopySampleString(fontRef, 0); 

and you'll get the same string as FontBook shows.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜