2012-04-09 25 views
1

居中文本垂直我有以下代碼繪製文本垂直居中(和水平)在一個矩形:在矩形

CGSize size=[self.buttonText sizeWithFont:font]; 
CGRect textFrame=CGRectMake(self.bounds.size.width/2-size.width/2, self.bounds.size.height/2-size.height/2, size.width, size.height); 
[self.buttonText drawInRect:textFrame withFont:font]; 

然而,我的文字繪製幾個像素太高。我的計算是否正確?

回答

2

你的代碼看起來不錯。嘗試給同一矩陣的背景顏色以查看/調試正在發生的事情。