在iOS上绘制垂直文本

Ben*_*ohn 6 nsstring nsattributedstring ios

我想在iOS上垂直绘制NSAttributedString(或NSString).通过"垂直"我的意思是:

垂直文本示例

Apple的文档说明了一个标准属性,但不幸的NSAttributedStringNSVerticalGlyphFormAttributeName,它说:

"在iOS中,始终使用水平文本,并且未定义指定不同的值."

同样的文件也提到NSTextLayoutSectionsAttribute,似乎支持NSTextLayoutOrientation,允许NSTextLayoutOrientationHorizontalNSTextLayoutOrientationVertical.

这4个术语中没有一个在目前的SO上有任何命中.只是那种孤独的吹口哨声.

但是,我不知道如何设置它,是否适用于iOS,或者是否可以用于风格的垂直字符串渲染[myAttributedString drawAtPoint: whereItOughtaGo].

谢谢.

Faz*_*zil 7

如果它只是一个单行文本,如您的示例,您可以使用各种解决方法.我个人创建一个UILabel子类如下:

@implementation VerticalLabel

- (id)initWithCoder:(NSCoder *)aDecoder
{
  self = [super initWithCoder:aDecoder];

  if (self) {
    self.numberOfLines = 0;
  }

  return self;
}

- (void)setText:(NSString *)text {
  NSMutableString *newString = [NSMutableString string];

  for (int i = text.length-1; i >= 0; i--) {
    [newString appendFormat:@"%c\n", [text characterAtIndex:i]];
  }

  super.text = newString;
}

@end
Run Code Online (Sandbox Code Playgroud)

现在你只需要替换:

UILabel *lbl = [[UILabel alloc] init];
Run Code Online (Sandbox Code Playgroud)

有:

UILabel *lbl = [[VerticalLabel alloc] init];
Run Code Online (Sandbox Code Playgroud)

获得垂直文本.


Emm*_*uel 6

您可以使用CoreText(获取字形)和CoreGraphics(绘制CGGlyphs)绘制垂直文本.一个简单的示例,它只处理一个字体属性(处理每个属性的有效范围,如果想要处理不同的字体,字体大小等...)

#import "NSAttributedString+VerticalDrawing.h"

@implementation TestView
- (void)drawRect:(CGRect)rect
{
    NSAttributedString *aString = [[NSAttributedString alloc] initWithString:@"This is a vertical text"
                                                                  attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14.]}];
    [aString drawVerticalAtPoint:CGPointMake(10, 10)];
}
@end

@implementation NSAttributedString (VerticalDrawing)

- (void)drawVerticalAtPoint:(CGPoint)location
{
    UIFont *font = [[self attributesAtIndex:0 effectiveRange:NULL] objectForKey:NSFontAttributeName];
    NSUInteger myLength = [self length];

    CTFontRef ctfont = CTFontCreateWithName((CFStringRef)[font fontName], [font pointSize], NULL);

    CGGlyph *glyphs = malloc(sizeof(CGGlyph) * myLength);
    UniChar *characters = malloc(sizeof(UniChar) * myLength);
    CGSize *advances = malloc(sizeof(CGSize) * myLength);

    [[self string] getCharacters:characters range:NSMakeRange(0,myLength)];

    CTFontGetGlyphsForCharacters(ctfont, characters, glyphs, myLength);
    CTFontGetAdvancesForGlyphs(ctfont, kCTFontHorizontalOrientation, glyphs, advances, myLength);

    free(characters);

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGFontRef cgfont = CTFontCopyGraphicsFont(ctfont, NULL);

    CGContextSetFont(ctx, cgfont);
    CGContextSetFontSize(ctx, CTFontGetSize(ctfont));

    CGAffineTransform textMatrix = CGAffineTransformMakeTranslation(location.x, location.y);
    textMatrix = CGAffineTransformScale(textMatrix, 1, -1);

    CGContextSetTextMatrix(ctx, textMatrix);

    CGFloat lineHeight = CTFontGetAscent(ctfont) + CTFontGetDescent(ctfont) + CTFontGetLeading(ctfont);
    location.y = -CTFontGetAscent(ctfont);

    CGFloat maxAdvance = 0.;

     NSUInteger i;
     for (i = 0; i < myLength; i++)
         maxAdvance = MAX(maxAdvance, advances[i].width);

    for (i = 0; i < myLength; i++)
    {
        location.x = (maxAdvance - advances[i].width) * 0.5;

        CGContextShowGlyphsAtPositions(ctx, &glyphs[i], &location, 1);
        location.y -= lineHeight;
    }

    free(glyphs);
    free(advances);
    CGFontRelease(cgfont);
    CFRelease(ctfont);
}
@end
Run Code Online (Sandbox Code Playgroud)

样品