2010-03-15 9 views
8

एक त्वरित सवाल है, तुरंत उत्तर के लिए (मैं किसी भी नहीं मिल रहा हूँ के बाद से):UITableView titleForSection फ़ॉन्ट

वहाँ खंड के नाम का फ़ॉन्ट iPhone में (titleForSection द्वारा दिए गए) को बदलने के लिए कोई तरीका है?

बहुत बहुत धन्यवाद!

उत्तर

26

आपको viewForHeaderInSection: विधि का उपयोग करना होगा और अपना खुद का दृश्य प्रदान करना होगा। सौभाग्य से यह एक निर्दिष्ट फ़ॉन्ट के साथ एक UILabel हो सकता है, तो आप इसे आसानी से कर सकते हैं।

39

धन्यवाद जसारीन! तुम बिल्कुल सही थे।

मैं अपने कोड यहाँ छोड़ एक ही समस्या के साथ किसी की मदद करने के लिए:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

    NSString *sectionTitle = @"Just a title"; 

    // Create label with section title 
    UILabel *label = [[[UILabel alloc] init] autorelease]; 
    label.frame = CGRectMake(0, 0, 284, 23); 
    label.textColor = [UIColor blackColor]; 
    label.font = [UIFont fontWithName:@"Helvetica" size:14]; 
    label.text = sectionTitle; 
    label.backgroundColor = [UIColor clearColor]; 

    // Create header view and add label as a subview 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; 
    [view autorelease]; 
    [view addSubview:label]; 

    return view; 
} 
+10

यह है महान, हालांकि, UILabel एक सीधा उपवर्ग है UIView का, इसलिए आपको वास्तव में 'हेडर' दृश्य बनाने की आवश्यकता नहीं है और लेबल को सबव्यूव के रूप में जोड़ें। आप लेबल को हेडर व्यू के रूप में ही वापस कर सकते हैं। – Jasarien

+5

यदि आप लेबल को हेडर के रूप में वापस लौटते हैं, तो आप इसे बाईं ओर कुछ ऑफसेट कैसे देंगे? उस मामले में लेबल फ्रेम के orig.x को बदलना काम नहीं करता .. – talkol

+0

केवल एकल पंक्ति शीर्षक) –

5

आप ओवरराइड करने के लिए

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 

अनुभाग शीर्षक के लिए आवश्यक ऊंचाई देना पड़ता है। अन्यथा यह सेल के साथ ओवरलैप होगा।

3

viewForHeaderInSection ठीक काम कर सकते हैं ... लेकिन यहाँ एक विकल्प है कि मुझे (Xcode 5 और iOS7 लक्ष्य) के लिए ठीक काम करता है:

// To set the background color and text color of the Table Headers 
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 
{ 
    // Background color 
    view.tintColor = [UIColor colorWithRed:0.329 green:0.557 blue:0.827 alpha:1.000]; 

    // Text Color & Alignment 
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view; 
    [header.textLabel setTextColor:[UIColor whiteColor]]; 
    [header.textLabel setTextAlignment:NSTextAlignmentCenter]; 
    // Text Font 
    UIFont *saveFont = header.textLabel.font; 
    [header.textLabel setFont:[UIFont fontWithName:saveFont.fontName size:18.0]]; 

    // Another way to set the background color 
    // Note: does not preserve gradient effect of original heade!r 
    // header.contentView.backgroundColor = [UIColor blackColor]; 
} 

Here's what it looks like