2014-05-13 55 views
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

你缺少中心對準的屬性,定義方式如下。我還添加了你對iOS6的代碼中定義的換行模式:

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

你只需將它們放置在屬性下的密鑰詞典:

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

太謝謝你了。那正是我需要的! –