如何将数字置于圆圈中?

时间:2014-05-13 07:15:53

标签: objective-c ios7.1

我试图制作一组自定义页码。将显示当前页码。其他人只是开放的圈子。

使用下面的代码,我得到了理想的结果 - 除了使用iOS7中的新文本属性,我无法弄清楚如何将数字居中。

任何帮助将不胜感激。

原始iOS6代码:

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

我的iOS7代码:

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];

1 个答案:

答案 0 :(得分:1)

中心对齐缺少的属性按以下方式定义。我还添加了你在iOS6代码中定义的换行模式:

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

您只需将它们放在键下的属性字典中:

NSArray *attrs = @{
    NSParagraphStyleAttributeName : paragraphStyle,
    ...
};