简体   繁体   中英

rotate text by 90 degrees in iOS

  NSString *strFromInt = [NSString stringWithFormat:@"%d",score];
    NSString *strFromInt2 = [NSString stringWithFormat:@"%ld",(long)highscore];
    CGContextRef gc = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(gc, 0, 0, 0, 1);
    UIFont *uif = [UIFont systemFontOfSize:32];
    NSString *combind = [NSString stringWithFormat:@"%@%@%@%@", str, strFromInt, str2, strFromInt2];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextRotateCTM(context, -(M_PI/2)); 
    CGContextRestoreGState(context);
    [combind  drawAtPoint:CGPointMake(100, 100) withAttributes:@{NSFontAttributeName:uif}];

My code works for printing out the highscore, but my current app is landscape instead of portrait so the text is showing up at a 90 degree angle. Is there any way to fix this (without switching to label as that would require a lot of changing)?

Have you tried using CGContextGetTextMatrix() to see what the current text matrix looks like? The text matrix gets concatenated to the current transformation matrix. Perhaps it has a value in it that you don't want?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM