1

I'm working on implementing a (very) simple screensaver using the Screensaver framework in OS X 10.10. Positioning on the center of the screen and displaying a two-line text works without problem, but setting the alignment to NSCenterTextAlignment somehow doesn't (the text is always displayed left-aligned).

- (void)animateOneFrame
{
    // calculate font size based on screen
    NSSize size = [self bounds].size;
    CGFloat fontsize = size.height / 11;

    // set text
    NSMutableParagraphStyle *centredStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [centredStyle setAlignment:NSCenterTextAlignment];

    NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSFont fontWithName:@"Futura" size:fontsize], NSFontAttributeName,
                                    [NSColor orangeColor], NSForegroundColorAttributeName,
                                    centredStyle, NSParagraphStyleAttributeName,
                                nil];
    NSString *theText = @"Simple text spanning\ntwo lines";

    // position text on screen
    NSRect boundingRect = [theText boundingRectWithSize:size options:0 attributes:textAttributes];
    NSPoint point = NSMakePoint((size.width - boundingRect.size.width) / 2.0,
                                (size.height - boundingRect.size.height) / 2.0);

    [theText drawAtPoint: point withAttributes: textAttributes];
}

Any pointers on how to solve this are appreciated.

PS: I know that I don't need to put everything into animateOneFrame but for the moment the goal is to get it working at all:-)

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.