2013-06-05 52 views
0

下面的UITableView單元格根據搜索類型交替文本位置。文本標籤設置爲標準,詳細信息文本當前設置爲斜體。我希望這是相反的。基於查詢格式的UITableView單元格文本字體

即如果genusSpecies:animal.commonName然後爲textLabel = ItalicFont否則如果animal.commonName:genusSpecies然後爲textLabel = StandardFont

我想這個規則應用到下面的代碼。

- (void)setAnimal:(ITanimal *)animal forType:(ITSectionType)type { 
    _animal = animal; 

    BOOL isCommonType = (type == ITSectionTypeCommonName); 
    NSString *genusSpecies = animal.species]; 

    [self.textLabel setFont:ItalicFont]; 
    [self.textLabel setText:(!isCommonType)? genusSpecies : animal.commonName]; 

    [self.detailTextLabel setFont:StandardFont]; 
    [self.detailTextLabel setText:(!isCommonType)? animal.commonName : genusSpecies]; 

} 
+0

問題是什麼?只需將相同類型的條件表達式添加到標籤文本設置的字體設置中,即可完成。它很醜,但很有用。 – allprog

+0

我已經調整了上面的問題,使其更清晰。 – Steve

+0

'if genusSpecies:animal.commonName ...'是什麼意思?我很確定這些表達式沒有':'運算符。是不是有任何機會? – allprog

回答

0

的問題是有點含糊,但我的感覺是,你必須在地方几乎一切。只需創建一個BOOL,您可以使用它來決定是否顯示屬物種。就像這樣:

BOOL isGenuSpecies = (boolean expression to decide if this is a genus species) 

UIFont *textLabelFont = isGenusSpecies? StandardFont : ItalicFont; 
UIFont *detailTextLabelFont = isGenusSpecies? ItalicFont : StandardFont; 

[self.textLabel setFont: textLabelFont]; 

[self.detailTextLabel setFont: detailTextLabelFont]; 

這工作只要你有StandardFontItalicFont先前定義somehere。

+0

謝謝,這樣做! – Steve

0

爲斜體,你可以使用

[UIFont fontWithName:@"Helvetica-Oblique" size:13.0] 
+0

這不是我問的問題的答案。我在問一個條件,字體樣式已經設置好了。 – Steve

相關問題