如何在xcode中创建顺时针角度文本

时间:2015-07-07 10:36:05

标签: ios

我试图在Xcode中创建顺时针文本。我目前的编码就是这样显示。

enter image description here

但我想将外部文本更改为

enter image description here

2 个答案:

答案 0 :(得分:1)

使用此库:https://github.com/javenisme/CurvaView

CoreTextArcView * cityLabel = [[[CoreTextArcView alloc] initWithFrame:rect1
                                                                        font:font1
                                                                        text:@"Hello this is radious arc with text"
                                                                      radius:85
                                                                     arcSize:110
                                                                       color:color1] autorelease];

删除一个文件的弧,如下所示(在项目目标的构建阶段将-fno-objc-arc设置为该库&.m文件):

enter image description here

像这样输出

enter image description here

答案 1 :(得分:1)

这是我执行此操作的代码,它会绘制属性字符串。 我无法发布图片,但结果恰到好处。 如果角度介于100°和260°之间,文本也会自动反转

enter image description here

#define degreesToRadians(degrees) (( degrees ) / 180.0 * M_PI )
#define radiansToDegrees(radians) (( radians ) * ( 180.0 / M_PI ) )


+ (void)drawCurvedStringOnLayer:(CALayer *)layer
         withAttributedText:(NSAttributedString *)text
                    atAngle:(float)angle
                 withRadius:(float)radius {
// angle in radians

CGSize textSize = CGRectIntegral([text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
                                                    options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                    context:nil]).size;

float perimeter = 2 * M_PI * radius;
float textAngle = (textSize.width / perimeter * 2 * M_PI); // text curved width

float textRotation;
float textDirection;
if (angle > degreesToRadians(10) && angle < degreesToRadians(170))
{
    //down
    textRotation = 0.5 * M_PI ;
    textDirection = - 2 * M_PI;
    angle += textAngle / 2 + degreesToRadians(6);
}
else
{
    //up
    textRotation = 1.5 * M_PI ;
    textDirection = 2 * M_PI;
    angle -= textAngle / 2; // + degreesToRadians(6);
}

for (int c = 0; c < text.length; c++)
{
    NSRange range = {c, 1};
    NSAttributedString* letter = [text attributedSubstringFromRange:range];
    CGSize charSize = CGRectIntegral([letter boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
                                                          options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                          context:nil]).size;

    float letterAngle = ( (charSize.width / perimeter) * textDirection );

    float x = radius * cos(angle + (letterAngle/2));
    float y = radius * sin(angle + (letterAngle/2));

    //NSLog(@"Char %@ with size: %f x %f", letter, charSize.width, charSize.height);
    //NSLog(@"Char %@ with angle: %f x: %f y: %f", letter, angle, x, y);

    CATextLayer *text = [layers drawTextOnLayer:layer
                                       withText:letter
                                          frame:CGRectMake(layer.frame.size.width/2 - charSize.width/2 + x,
                                                           layer.frame.size.height/2 - charSize.height/2 + y,
                                                           charSize.width, charSize.height)
                                        bgColor:nil
                                        opacity:1];

    text.transform = CATransform3DMakeAffineTransform( CGAffineTransformMakeRotation(angle - textRotation) );

    angle += letterAngle;
}
}



+ (CATextLayer *)drawTextOnLayer:(CALayer *)layer
                    withText:(NSAttributedString *)text
                       frame:(CGRect)frame
                     bgColor:(UIColor *)bgColor
                     opacity:(float)opacity {

    CATextLayer *textLayer = [[CATextLayer alloc] init];
    textLayer.needsDisplayOnBoundsChange = true;
    //[textLayer setFont:(__bridge CFTypeRef)(font)];
    //[textLayer setFontSize:fontSize];
    [textLayer setFrame:frame];
    [textLayer setString:text];
    [textLayer setAlignmentMode:kCAAlignmentCenter];
    //    [textLayer setForegroundColor:color.CGColor];
    [textLayer setBackgroundColor:bgColor.CGColor];
    [textLayer setContentsScale:[UIScreen mainScreen].scale];
    [textLayer setOpacity:opacity];
    [layer addSublayer:textLayer];
}