Question

I'm working on an app where I need all lines of text to be Justified alignment. The app will display 2 lines of text over the top of an image, creating an Advert like feeling to the picture. The problem is that both these lines of text need to be justified, but the 2nd line of text will never be justified. Or if I draw both lines separately then neither will be justified.

If you specify kCTJustifiedTextAlignment in CoreText or NSJustifiedTextAlignment on NSTextField/View all but the last line of text are justified. The last line is aligned naturally.

Is there a way to force the final line of a textfield or textview to be justified, so fill the width of the view?

I've thought about using CTLine to draw each line separately, and specifying something on those to make all of them justified, but I'm not sure how to go about that either.

Thanks

Was it helpful?

Solution

Thanks to omz's comment I stumbled across CTLineCreateJustifiedLine, which gives me just what I wanted. I'd still be interested if someone has a solution using a Cocoa Control (NSTextView), but i'd be surprised if there was anything tbh.

CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)attString);
line = CTLineCreateJustifiedLine(line, 1.0, dirtyRect.size.width);

CTLineDraw(line, context);
CFRelease(line);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top