2011-01-23 162 views

回答

12

實際的字體大小取決於用戶在設置設置 - >通用 - > TextSize。通常情況下,你不應該使用固定的字體大小,而應該使用類似:

[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] 

顯然取決於你所需要的東西。無論如何,如果你創建一個UITableViewCell風格UITableViewCellStyleSubtitle,然後cell.text的字體是相同的對象

[UIFont preferredFontForTextStyle: UIFontTextStyleBody] 

和cell.detailTextLabel的字體相同的對象

[UIFont preferredFontForTextStyle: UIFontTextStyleCaption1]. 

你使用以「Body」,「Subheadline」,「Footnote」,「Caption1」,「Caption2」結尾的常量,從最大到最小的字體獲得字體,因此,如果您希望稍小或更大的文本,您就知道該使用什麼。 「標題」與「正文」大小相同,但大膽。

最好在運行時創建一個單元格,並從中獲取字體。

51

你可以隨時在代碼中爲這些標籤設置任何字體,所以如果你想要一些保證的固定值,你最好這樣做,因爲尺寸值可能因許多因素(單元格的樣式,sdk版本,操作系統版本等)而異。

我模擬器上測試了4.2版本的SDK,並得到如下結果(沒有額外的性能設置電池):

  1. UITableViewCellStyleSubtitle

    爲textLabel:黑體加粗,大小: labelFontSize + 1(18像素)
    detailsLabel:黑體,尺寸:systemFontSize(14像素)

  2. UITableViewCellStyleValue1

    爲textLabel:黑體粗體,尺寸:labelFontSize(17像素)
    detailsLabel:黑體粗體,大小:systemFontSize + 1(15像素)

  3. UITableViewCellStyleValue2

    爲textLabel :Helvetica Bold,尺寸:smallSystemFontSize(12 px)
    細節標籤:Helvetica,尺寸:labelFontSize(17 px)

+0

你是怎麼得到這個的?我試着記錄下來,並得到無用的字體大小信息。 – Moshe 2012-02-05 18:52:37

+0

@Moshe,坦率地說,我只是玩了不同的價值觀,並將它們與默認值進行了比較 - 所以沒有100%保證這些值是準確的。 – Vladimir 2012-02-05 19:16:40

5

當我在iPad上5.0模擬器上運行此:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleValue2 
      reuseIdentifier:CellIdentifier] autorelease]; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 
//set text to get font size > 0 

NSLog(@"cellStyleValue2 text font: %@\n", cell.textLabel.font); 
NSLog(@"cellStyleValue2 detail font: %@\n", cell.detailTextLabel.font); 

我看到:

cellStyleValue2文本字體:FONT-FAMILY: 「黑體」; font-weight:bold; font-style:normal; font-size:12px

cellStyleValue2 detail font:font-family:「Helvetica」; font-weight:bold; font-style:normal;字體大小:15像素

由於這些參數明顯變化,記錄的字體對象是知道沒有猜測工作好辦法...