iOS中的C字符串看起来很模糊

时间:2015-09-21 14:40:54

标签: ios xcode core-text

我一直在尝试以弧形呈现文本。文本按预期呈现,但看起来很模糊。我该如何解决这个问题。

- (UIImage*) createMenuRingWithFrame:(CGRect)frame
{
    NSArray* sections = [[NSArray alloc] initWithObjects:@"daily", @"yearly", @"monthly", @"weekly",nil];
    CGRect imageSize = frame;
    float perSectionDegrees = 360 / [sections count];
    float totalRotation = 135;
    float fontSize = ((frame.size.width/2) /2)/2;
    self.menuItemsFont = [UIFont fontWithName:@"Avenir" size:fontSize];


    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, imageSize.size.width, imageSize.size.height, 8, 4 * imageSize.size.width, colorSpace,(CGBitmapInfo) kCGImageAlphaPremultipliedFirst);


    CGPoint centerPoint = CGPointMake(imageSize.size.width / 2, imageSize.size.height / 2);
    double radius = (frame.size.width / 2)-2;


    for (int index = 0; index < [sections count]; index++)
    {
        BOOL textRotationDown = NO;
        NSString* menuItemText = [sections objectAtIndex:index];
        CGSize textSize =  [menuItemText sizeWithAttributes:
                            @{NSFontAttributeName: self.menuItemsFont}];

        char* menuItemTextChar = (char*)[menuItemText cStringUsingEncoding:NSASCIIStringEncoding];

        if (totalRotation>200.0 && totalRotation <= 320.0) {
            textRotationDown = YES;
        }
        else
            textRotationDown= NO;

        float x = centerPoint.x + radius * cos(DEGREES_TO_RADIANS(totalRotation));
        float y = centerPoint.y + radius * sin(DEGREES_TO_RADIANS(totalRotation));

        CGContextSaveGState(context);

        CFStringRef font_name = CFStringCreateWithCString(NULL, "Avenir", kCFStringEncodingMacRoman);

        CTFontRef font = CTFontCreateWithName(font_name, fontSize, NULL);

        CFStringRef keys[] = { kCTFontAttributeName };

        CFTypeRef values[] = { font };

        CFDictionaryRef font_attributes = CFDictionaryCreate(kCFAllocatorDefault, (const void **)&keys, (const void **)&values, sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

        CFRelease(font_name);

        CFRelease(font);


        CFStringRef string = CFStringCreateWithCString(NULL, menuItemTextChar, kCFStringEncodingMacRoman);

        CFAttributedStringRef attr_string = CFAttributedStringCreate(NULL, string, font_attributes);

        CTLineRef line = CTLineCreateWithAttributedString(attr_string);

        CGContextTranslateCTM(context, x, y);


         CGContextRotateCTM(context, DEGREES_TO_RADIANS(totalRotation - (textRotationDown?275:90)));

        CGContextSetTextPosition(context,0 - (textSize.width / 2), 0 - (textSize.height / (textRotationDown?20:4)));

        CTLineDraw(line, context);

        CFRelease(line);

        CFRelease(string);

        CFRelease(attr_string);

        CGContextRestoreGState(context);

        totalRotation += perSectionDegrees;
    }

    CGImageRef contextImage = CGBitmapContextCreateImage(context);

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:contextImage];
}

1 个答案:

答案 0 :(得分:4)

一个问题是您不允许屏幕分辨率。使您的位图上下文大两倍,或大三倍;适当地乘以所有值(如果您只是在开始时应用比例CTM,这是最简单的);然后在最后,不要拨打imageWithCGImage:,而是拨打imageWithCGImage:scale:orientation:,设置相应的比例。

如果您使用UIGraphicsBeginImageContextWithOptions创建了上下文,则会自动发生(如果您提供了第三个零参数),或者您可以明确设置第三个参数以提供scale因为上下文,因此从它衍生的图像。但是,通过手动构建上下文,您可以放弃为其提供scale

的容量