I'd like to draw an NSAttributedString
(or NSString
) vertically on iOS. By "vertically" I mean:
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With