можно ли повернуть символ с помощью NSAttributedString

Используя 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;


}

Вывод :

введите здесь описание изображения


person Anand    schedule 12.08.2014    source источник
comment
Как вращать струну?   -  person trojanfoe    schedule 12.08.2014
comment
stackoverflow .com/questions/1899775/   -  person Mitul Bhadeshiya    schedule 12.08.2014
comment
weblog.invasivecode.com/core-text   -  person Mitul Bhadeshiya    schedule 12.08.2014


Ответы (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];
}
person Shamsudheen TK    schedule 12.08.2014