如何模仿UITableView的UITableViewStylePlain部分标题样式

Bri*_*ngo 12 cocoa-touch objective-c uitableview uikit ios

我的应用程序使用UITableView部分标题标题中的缩写,VoiceOver很难发音.由于我需要通过VoiceOver使这些标题可以发音,我需要给出标题标题a accessibilityLabel.

似乎这样做的唯一方法是绘制自定义节标题单元格.我想模仿标准Apple UIKit为这些自定义部分标题提供的样式,但我不确定如何模仿Apple的这个元素的详细外观.

模仿UITableViewStylePlain节标题样式的最佳方法是什么?

更新:我很清楚如何创建自定义标题单元格.我正在寻找的是一种技术,可以模仿Apple为普通UITableView部分标题单元格提供的标题单元格样式.

Bar*_*ttJ 11

如果有人仍然感兴趣,我看起来非常接近以下代码(使用上面评论中的Mark Adams图片,但我稍微调整了它们,因为我的应用程序也有横向模式):

- (UIView *)tableView:(UITableView *)tbl viewForHeaderInSection:(NSInteger)section
{
    UIView* sectionHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tbl.bounds.size.width, 18)];
    sectionHead.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    sectionHead.userInteractionEnabled = YES;
    sectionHead.tag = section;

    UIImageView *headerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"PlainTableViewSectionHeader.png"]];
    headerImage.contentMode = UIViewContentModeScaleAspectFit;

    [sectionHead addSubview:headerImage];
    [headerImage release];

    UILabel *sectionText = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, tbl.bounds.size.width - 10, 18)];
    sectionText.text = text;
    sectionText.backgroundColor = [UIColor clearColor];
    sectionText.textColor = [UIColor whiteColor];
    sectionText.shadowColor = [UIColor darkGrayColor];
    sectionText.shadowOffset = CGSizeMake(0,1);
    sectionText.font = [UIFont boldSystemFontOfSize:18];

    [sectionHead addSubview:sectionText];
    [sectionText release];

    return [sectionHead autorelease];
}
Run Code Online (Sandbox Code Playgroud)


小智 8

这是一个UILabel子类的实现,它以编程方式模拟背景:

UITableViewStandardHeaderLabel.h

#import <UIKit/UIKit.h>

@interface UITableViewStandardHeaderLabel : UILabel

@property (nonatomic) CGFloat topInset;
@property (nonatomic) CGFloat leftInset;
@property (nonatomic) CGFloat bottomInset;
@property (nonatomic) CGFloat rightInset;

@end
Run Code Online (Sandbox Code Playgroud)

UITableViewStandardHeaderLabel.m:

/*!
 * @class UITableViewStandardHeaderLabel
 * @brief Reimplementation of the UILabel used for a standard UITableView's group headers for customization purposes
 */

@implementation UITableViewStandardHeaderLabel

@synthesize topInset, leftInset, bottomInset, rightInset;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }

    return self;
}

- (void)drawTextInRect:(CGRect)rect
{
    UIEdgeInsets insets = {self.topInset, self.leftInset, self.bottomInset, self.rightInset};

    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

- (void)drawRect:(CGRect)rect
{   
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGGradientRef backgroundGradient;
    CGColorSpaceRef rgbColorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0f, 1.0f };
    CGFloat components[8] = { 144.0f/255.0f, 159.0f/255.0f, 171.0f/255.0f, 1.0f,
                              /* start */ 183.0f/255.0f, 192.0f/255.0f, 200.0f/255.0f, 1.0f /* end */ };

    rgbColorspace = CGColorSpaceCreateDeviceRGB();
    backgroundGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);

    CGRect currentBounds = self.bounds;
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMinY(currentBounds));
    CGPoint bottomCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds));

    CGContextDrawLinearGradient(context, backgroundGradient, topCenter, bottomCenter, 0);

    UIColor *topBorderLineColor = [UIColor colorWithRed:113.0f/255.0f  green:125.0f/255.0f blue:133.0f/255.0f alpha:1.0];
    UIColor *secondLineColor = [UIColor colorWithRed:165.0f/255.0f  green:177.0f/255.0f blue:187.0f/255.0f alpha:1.0];
    UIColor *bottomBorderLineColor = [UIColor colorWithRed:151.0f/255.0f  green:157.0f/255.0f blue:164.0f/255.0f alpha:1.0];

    [topBorderLineColor setFill];
    CGContextFillRect(context, CGRectMake(0, 0, CGRectGetMaxX(currentBounds), 1));

    [bottomBorderLineColor setFill];
    CGContextFillRect(context, CGRectMake(0, CGRectGetMaxY(currentBounds)-1, CGRectGetMaxX(currentBounds), 1));

    [secondLineColor setFill];
    CGContextFillRect(context, CGRectMake(0, 1, CGRectGetMaxX(currentBounds), 1));

    [super drawRect:rect];
}

@end
Run Code Online (Sandbox Code Playgroud)


Ara*_*yan 0

我将创建一个自定义 UIView 类并向其添加一个 UILabel 作为部分标题文本。对于背景,使用 UIImageView 并加载相应的图像作为节标题的背景。使用 UIView 的 addSubView: 方法分配此 UIImageView。

在 UITableViewController 中,您可以设置 tableView.sectionHeaderHeight 来自定义所有节标题的高度。使用 UITableViewDelegate 方法:

tableView:viewForHeaderInSection:
Run Code Online (Sandbox Code Playgroud)

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDelegate

您应该返回自定义 UIView 的实例,并将文本标签作为该部分的标题。

您应该向 UILabel 添加阴影并调整所有颜色以适合默认样式。由于节标题也稍微透明,您可以使用以下命令设置 UIView alpha

self.alpha = 0.9f;
Run Code Online (Sandbox Code Playgroud)

  • 我制作了一些自定义图像用于节标题。视网膜准备好了。您应该能够使用 UILabel 完成文本样式设置。http://cl.ly/4Joo (5认同)