【问题标题】:is it possible to rotate the character using NSAttributedString是否可以使用 NSAttributedString 旋转字符
【发布时间】:2014-10-05 06:28:30
【问题描述】:

使用 NSAttributedString 我们可以改变字符串的颜色等。是否可以倾斜字符串中的特定字符。 例如

我们将不胜感激。

我找到了一个不符合标准的解决方案。如果有人更正了有帮助的代码

- (void)drawRect:(CGRect)rect {

    CTFontRef sysUIFont = CTFontCreateUIFontForLanguage(kCTFontSystemFontType,
                                                        14.0, NULL);


    // blue
    CGColorRef color = [UIColor blueColor].CGColor;

    // single underline
    //NSNumber *underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle];

    NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                    (__bridge id)sysUIFont, (id)kCTFontAttributeName,
                                    color, (id)kCTForegroundColorAttributeName, nil];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);

    CGContextScaleCTM(context, 1.0, -1.0);


    for (int i =0 ; i < 4; i++)
    {

        NSString *letter =[@"TEXT" substringWithRange:NSMakeRange(i, 1)];

         NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:letter attributes:attributesDict];

       // CGSize letterSize =[letter sizeWithAttributes:@{NSFontAttributeName:self.font}];


        // draw
        CTLineRef line = CTLineCreateWithAttributedString(
                                                          (CFAttributedStringRef)stringToDraw);

        if(i==3){
            CGContextRotateCTM (context, radians(90));

             //18 & 17 are hard coded values
            CGContextSetTextPosition(context, (10.0 * i)- 18, 10.0 - (17*i));

        }
        else{
        CGContextSetTextPosition(context, 10.0 * i, 10.0);
        }

        CTLineDraw(line, context);

        // clean up
        CFRelease(line);

        stringToDraw = nil;

    }


    CFRelease(sysUIFont);
    //stringToDraw = nil;


}

输出:

【问题讨论】:

标签: ios core-text nsmutablestring


【解决方案1】:

NSAffineTransform 类提供了用于创建、连接和应用仿射变换的方法。

rotateByDegrees: 将旋转因子(以度为单位)应用于接收器的变换矩阵。

rotateByRadians: 将旋转因子(以弧度测量)应用于接收器的变换矩阵。

示例:

- (void)drawRect:(NSRect)rect
{
    NSAttributedString * myString = [[NSAttributedString alloc] initWithPath:mypath documentAttributes:NULL];
    NSAffineTransform* xform = [NSAffineTransform transform];
    [myString drawAtPoint:NSMakePoint(10,10)];
    [xform rotateByDegrees:-90.0];
    [xform concat];
    [myString drawAtPoint:NSMakePoint(-150,0)];
    [xform invert];
    [xform concat];
    [myString drawAtPoint:NSMakePoint(15,15)];
    [myString release];
}

【讨论】:

    猜你喜欢
    • 2013-09-22
    • 2013-09-13
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    相关资源
    最近更新 更多