How to mimic UITableView's UITableViewStylePlain section header style

前端 未结 4 1690
孤街浪徒
孤街浪徒 2020-12-25 09:18

My application uses abbreviations in UITableView section header titles that are hard for VoiceOver to pronounce. As I need to make these titles pronounceable by

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-25 09:40

    Here's an implementation of a UILabel subclass which mimics the background programatically:

    UITableViewStandardHeaderLabel.h

    #import 
    
    @interface UITableViewStandardHeaderLabel : UILabel
    
    @property (nonatomic) CGFloat topInset;
    @property (nonatomic) CGFloat leftInset;
    @property (nonatomic) CGFloat bottomInset;
    @property (nonatomic) CGFloat rightInset;
    
    @end
    

    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
    

提交回复
热议问题