2013-11-28 17 views
1

剛剛更新到iOS 7我的項目,解決警告棄用像sizeWithFont:爲什麼CustomBadge在iOS 7中以黑色繪製文本?

前:

CGSize stringSize = [badgeString sizeWithFont:[UIFont boldSystemFontOfSize:12]]; 

後:

CGSize stringSize = [badgeString sizeWithAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}]; 

和文字顏色變成了黑色: enter image description here

有什麼建議嗎?

代碼比drawns文字是:

[badgeTextColor set]; 
CGFloat sizeOfFont = 13.5*badgeScaleFactor; 
if ([self.badgeText length]<2) { 
    sizeOfFont += sizeOfFont*0.20; 
} 
UIFont *textFont = [UIFont boldSystemFontOfSize:sizeOfFont]; 

CGSize textSize = [self.badgeText sizeWithAttributes: @{NSFontAttributeName:textFont, 
                 NSForegroundColorAttributeName:self.badgeTextColor}]; 

[self.badgeText drawAtPoint:CGPointMake((rect.size.width/2-textSize.width/2), (rect.size.height/2-textSize.height/2)) withAttributes:@{NSFontAttributeName:textFont}]; 

我已經添加NSForegroundColorAttributeName:self.badgeTextColor但它不工作

前... [self.badgeTextColor = [UIColor whiteColor]];

+0

那你明確地設置文字顏色爲別的東西嗎? – chuthan20

+0

默認是白色,我將添加代碼 –

回答

1

我已經解決了,我有在我自己的解決方案中的一個錯誤是正確的添加這個 NSForegroundColorAttributeName:self.badgeTextColor}

但不是在textSize初始化

它必須[self.badteTxt draw....

[self.badgeText drawAtPoint:CGPointMake((rect.size.width/2-textSize.width/2), 
(rect.size.height/2-textSize.height/2)) withAttributes:@{NSFontAttributeName:textFont, 
NSForegroundColorAttributeName:self.badgeTextColor}]; 
+0

優秀。謝謝你。我已經移植過,但沒有包含第二個字典鍵/值。 – VaporwareWolf

相關問題