Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing vertical text on iOS

I'd like to draw an NSAttributedString (or NSString) vertically on iOS. By "vertically" I mean:

Vertical text example

Apple's documentation says a standard attribute on an NSAttributedString is NSVerticalGlyphFormAttributeName, but unfortunately, says:

"In iOS, horizontal text is always used and specifying a different value is undefined."

The same document also mentions NSTextLayoutSectionsAttribute, which seems to support NSTextLayoutOrientation, which allows NSTextLayoutOrientationHorizontal or NSTextLayoutOrientationVertical.

None of those 4 terms get any hits on SO at the moment. Just that lonely whistling sound.

However, I don't have any idea how to set this up, whether it works with iOS, or if can be used for vertical string rendering in the style of [myAttributedString drawAtPoint: whereItOughtaGo].

Thanks.

like image 738
Benjohn Avatar asked Mar 05 '14 11:03

Benjohn


People also ask

How do I write text vertically?

Position text vertically in a shape or text boxRight-click the border of the shape or text box. On the shortcut menu, select Format Shape, and then select Text Box in the left pane. Under Text layout, select the option that you want in the Vertical alignment list. Select Close.

How do I rotate text on iPad?

Rotate an objectTap the object to select it, place two fingers on it, then turn your hand in the direction you want to rotate the object. After you start the rotation, you can continue by dragging with a single finger.

How do I rotate text in Apple?

Found out how to do this when I did it by accident: Select the text box, then hold down Command. With two fingers on the trackpad (forefinger and middle finger), keep one finger stationary (forefinger) and let the second finger rotate the textbox in whichever direction you want.


1 Answers

If it just a single lined text as in your example you can use various workarounds. I would personally create a UILabel subclass as follows:

@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

Now you just have to replace:

UILabel *lbl = [[UILabel alloc] init];

with:

UILabel *lbl = [[VerticalLabel alloc] init];

to get vertical text.

like image 122
Fazil Avatar answered Sep 20 '22 00:09

Fazil