-2

I want to rotate a NSString by an angle.

but I dont want the whole string to rotate,instead I want the string head stay in the original place.

How can i make NSString rotate around a certain point , but not the whole matrix rotate...

OK,sorry for that,In fact my problem is I want to rotate around a certain point ,but CGContextRotateCTM is based the origin point.So how can i move the origin point to certain point ,when after rotation, How can i move the origian point back....

sinopec
  • 851
  • 1
  • 9
  • 16
  • I fount this, but dont fix my problem :http://stackoverflow.com/questions/10289898/drawing-rotated-text-with-nsstring-drawinrect – sinopec Nov 27 '12 at 13:21
  • What do you mean by the string head? What have you tried? – Pfitz Nov 27 '12 at 13:21
  • 1
    you need to explain more, what exactly do you mean, are you drawing text via CGContext or just displaying text in UILabel etc. – Asif Mujteba Nov 27 '12 at 13:23
  • 2
    NSString just represents a string, it's distinct from display concerns such as position and angle. Do you mean UILabel? Do you mean Core Graphics drawing? – Clafou Nov 27 '12 at 13:33
  • 1
    -1 - unclear/misleadig question + title. – Vinay W Nov 27 '12 at 13:39
  • OK,sorry for that,In fact my problem is I want to rotate around a certain point ,but CGContextRotateCTM is based the origin point.So how can i move the origin point to certain point ,when after rotation, How can i move the origian point back.... – sinopec Nov 28 '12 at 01:43

1 Answers1

0

Presumably you want to rotate a UILabel around an anchor point. If so, done as follows as lifted from David Rönnqvist here:

#define DEGREES_TO_RADIANS(angle) (angle/180.0*M_PI)


- (void)rotateText:(UILabel *)label 
       aroundPoint:(CGPoint)rotationPoint 
          duration:(NSTimeInterval)duration 
           degrees:(CGFloat)degrees {

    /* Setup the animation */
   [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
    CGPoint transportPoint = CGPointMake((rotationPoint.x - CGRectGetMinX(label.frame))/CGRectGetWidth(label.bounds),
                                         (rotationPoint.y - CGRectGetMinY(label.frame))/CGRectGetHeight(label.bounds));

    [label.layer setAnchorPoint:transportPoint];
    [label.layer setPosition:rotationPoint]; // change the position here to keep the frame 
    [label.layer setTransform:CATransform3DMakeRotation(DEGREES_TO_RADIANS(degrees), 0, 0, 1)];   
    } completion:nil];
} 

If you want it to rotate around an end, set the anchor point (aroundPoint in the method) there using CGPointMake(.. , ..)

Community
  • 1
  • 1
Ōkami
  • 155
  • 2
  • 9