Question

I'm trying to produce a set of custom page numbers. The current page number will be displayed. The others will simply be open circles.

Using the code below, I'm getting the desired results - except, with the new text attributes in iOS7, I cannot figure out how to center the number.

Any help will be appreciated.

Original iOS6 code:

[pageNumber drawInRect:CGRectMake(x,(self.frame.size.height-_currentPageDiameter)/2-1,_currentPageDiameter,_currentPageDiameter) withFont:[UIFont systemFontOfSize:_currentPageDiameter-2] lineBreakMode:UILineBreakModeCharacterWrap alignment:NSTextAlignmentCenter];

My iOS7 code:

NSString *pageNumber = [NSString stringWithFormat:@"%i", i+1];
                    CGContextSetFillColorWithColor(myContext, [[UIColor whiteColor] CGColor]);

UIFont* font = [UIFont systemFontOfSize:_currentPageDiameter-2];
NSDictionary *attrs = @{ NSForegroundColorAttributeName : [UIColor whiteColor],
                                             NSFontAttributeName : font,
                                             NSTextEffectAttributeName : NSTextEffectLetterpressStyle};

CGRect r = CGRectMake(x,(self.frame.size.height-_currentPageDiameter)/2-1,_currentPageDiameter,_currentPageDiameter);
[pageNumber drawInRect:r withAttributes:attrs];
Was it helpful?

Solution

The property you're missing for center alignment is defined the following way. I also added the line break mode you had defined on the iOS6 code:

NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc] init];
paragrapStyle.alignment = NSTextAlignmentCenter;
paragrapStyle.lineBreakMode = NSLineBreakByCharWrapping;

You just place them in the attributes dictionary under the key:

NSArray *attrs = @{
    NSParagraphStyleAttributeName : paragraphStyle,
    ...
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top