Draw centered multi-line NSString

I would like to draw a centered multi-line NSString. Using

- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment

      

it works like a charm. Unfortunately this method has been deprecated in iOS 7. According to Apple documentation, I should replace using this method

- (void)drawWithRect:(CGRect)rect options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context

      

However, text drawn this way is only centered if the NSString results in multiline text when drawn, but not centered if it outputs a single line of text when drawn.

I passed the following NSMutableParagraphStyle

through the attribute dictionary:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentCenter;

      

I installed options

in NSStringDrawingUsesLineFragmentOrigin

and transferred nil

for context

.

Any help would be really appreciated!

Respectfully!

+3


source to share





All Articles