- #import "CustomTabBarItem.h"@interface CustomTabBarItem() {
- NSString * _title;
- UIOffset _imagePositionAdjustment;
- NSDictionary * _unselectedTitleAttributes;
- NSDictionary * _selectedTitleAttributes;
- }@property UIImage * unselectedBackgroundImage;@property UIImage * selectedBackgroundImage;@property UIImage * unselectedImage;@property UIImage * selectedImage;@end@implementation CustomTabBarItem - (id) initWithFrame: (CGRect) frame {
- self = [super initWithFrame: frame];
- if (self) { [self commonInitialization];
- }
- return self;
- } - (id) init {
- return [self initWithFrame: CGRectZero];
- } - (void) commonInitialization {
- // Setup defaults
- [self setBackgroundColor: [UIColor clearColor]];
- _title = @"";
- _titlePositionAdjustment = UIOffsetZero;
- if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
- _unselectedTitleAttributes = @ {
- NSFontAttributeName: [UIFont systemFontOfSize: 10],
- NSForegroundColorAttributeName: [UIColor colorWithRed: 255 / 255.f green: 255 / 255.f blue: 255 / 255.f alpha: 1.0f],
- };
- } else {#
- if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0 _unselectedTitleAttributes = @ {
- UITextAttributeFont: [UIFont systemFontOfSize: 10],
- UITextAttributeTextColor: [UIColor colorWithRed: 255 / 255.f green: 255 / 255.f blue: 255 / 255.f alpha: 1.0f],
- };#endif
- }
- _selectedTitleAttributes = [_unselectedTitleAttributes copy];
- _badgeBackgroundColor = [UIColor redColor];
- _badgeTextColor = [UIColor whiteColor];
- _badgeTextFont = [UIFont systemFontOfSize: 12];
- _badgePositionAdjustment = UIOffsetZero;
- } - (void) drawRect: (CGRect) rect {
- CGSize frameSize = self.frame.size;
- CGSize imageSize = CGSizeZero;
- CGSize titleSize = CGSizeZero;
- NSDictionary * titleAttributes = nil;
- UIImage * backgroundImage = nil;
- UIImage * image = nil;
- CGFloat imageStartingY = 0.0f;
- if ([self isSelected]) {
- image = [self selectedImage];
- backgroundImage = [self selectedBackgroundImage];
- titleAttributes = [self selectedTitleAttributes];
- if (!titleAttributes) {
- titleAttributes = [self unselectedTitleAttributes];
- }
- } else {
- image = [self unselectedImage];
- backgroundImage = [self unselectedBackgroundImage];
- titleAttributes = [self unselectedTitleAttributes];
- }
- imageSize = [image size];
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSaveGState(context);
- [backgroundImage drawInRect: self.bounds];
- // Draw image and title
- if (! [_title length]) { [image drawInRect: CGRectMake(roundf(frameSize.width / 2 - imageSize.width / 2) + _imagePositionAdjustment.horizontal, roundf(frameSize.height / 2 - imageSize.height / 2) + _imagePositionAdjustment.vertical, imageSize.width, imageSize.height)];
- } else {
- if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
- titleSize = [_title boundingRectWithSize: CGSizeMake(frameSize.width, 20) options: NSStringDrawingUsesLineFragmentOrigin attributes: @ {
- NSFontAttributeName: titleAttributes[NSFontAttributeName]
- }
- context: nil].size;
- imageStartingY = roundf((frameSize.height - imageSize.height - titleSize.height) / 2);
- [image drawInRect: CGRectMake(roundf(frameSize.width / 2 - imageSize.width / 2) + _imagePositionAdjustment.horizontal, imageStartingY + _imagePositionAdjustment.vertical, imageSize.width, imageSize.height)];
- CGContextSetFillColorWithColor(context, [titleAttributes[NSForegroundColorAttributeName] CGColor]);
- [_title drawInRect: CGRectMake(roundf(frameSize.width / 2 - titleSize.width / 2) + _titlePositionAdjustment.horizontal, imageStartingY + imageSize.height + _titlePositionAdjustment.vertical, titleSize.width, titleSize.height) withAttributes: titleAttributes];
- } else {#
- if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0 titleSize = [_title sizeWithFont: titleAttributes[UITextAttributeFont] constrainedToSize: CGSizeMake(frameSize.width, 20)];
- UIOffset titleShadowOffset = [titleAttributes[UITextAttributeTextShadowOffset] UIOffsetValue];
- imageStartingY = roundf((frameSize.height - imageSize.height - titleSize.height) / 2);
- [image drawInRect: CGRectMake(roundf(frameSize.width / 2 - imageSize.width / 2) + _imagePositionAdjustment.horizontal, imageStartingY + _imagePositionAdjustment.vertical, imageSize.width, imageSize.height)];
- CGContextSetFillColorWithColor(context, [titleAttributes[UITextAttributeTextColor] CGColor]);
- UIColor * shadowColor = titleAttributes[UITextAttributeTextShadowColor];
- if (shadowColor) {
- CGContextSetShadowWithColor(context, CGSizeMake(titleShadowOffset.horizontal, titleShadowOffset.vertical), 1.0, [shadowColor CGColor]);
- }
- [_title drawInRect: CGRectMake(roundf(frameSize.width / 2 - titleSize.width / 2) + _titlePositionAdjustment.horizontal, imageStartingY + imageSize.height + _titlePositionAdjustment.vertical, titleSize.width, titleSize.height) withFont: titleAttributes[UITextAttributeFont] lineBreakMode: NSLineBreakByTruncatingTail];#endif
- }
- }
- // Draw badges
- if ([[self badgeValue] length]) {
- CGSize badgeSize = CGSizeZero;
- if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
- badgeSize = [_badgeValue boundingRectWithSize: CGSizeMake(frameSize.width, 20) options: NSStringDrawingUsesLineFragmentOrigin attributes: @ {
- NSFontAttributeName: [self badgeTextFont]
- }
- context: nil].size;
- } else {#
- if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0 badgeSize = [_badgeValue sizeWithFont: [self badgeTextFont] constrainedToSize: CGSizeMake(frameSize.width, 20)];#endif
- }
- CGFloat textOffset = 2.0f;
- if (badgeSize.width < badgeSize.height) {
- badgeSize = CGSizeMake(badgeSize.height, badgeSize.height);
- }
- CGRect badgeBackgroundFrame = CGRectMake(roundf(frameSize.width / 2 + (image.size.width / 2) * 0.9) + [self badgePositionAdjustment].horizontal, textOffset + [self badgePositionAdjustment].vertical, badgeSize.width + 2 * textOffset, badgeSize.height + 2 * textOffset);
- if ([self badgeBackgroundColor]) {
- CGContextSetFillColorWithColor(context, [[self badgeBackgroundColor] CGColor]);
- CGContextFillEllipseInRect(context, badgeBackgroundFrame);
- } else if ([self badgeBackgroundImage]) { [[self badgeBackgroundImage] drawInRect: badgeBackgroundFrame];
- }
- CGContextSetFillColorWithColor(context, [[self badgeTextColor] CGColor]);
- if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
- NSMutableParagraphStyle * badgeTextStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; [badgeTextStyle setLineBreakMode: NSLineBreakByWordWrapping]; [badgeTextStyle setAlignment: NSTextAlignmentCenter];
- NSDictionary * badgeTextAttributes = @ {
- NSFontAttributeName: [self badgeTextFont],
- NSForegroundColorAttributeName: [self badgeTextColor],
- NSParagraphStyleAttributeName: badgeTextStyle,
- };
- [[self badgeValue] drawInRect: CGRectMake(CGRectGetMinX(badgeBackgroundFrame) + textOffset, CGRectGetMinY(badgeBackgroundFrame) + textOffset, badgeSize.width, badgeSize.height) withAttributes: badgeTextAttributes];
- } else {#
- if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0[[self badgeValue] drawInRect: CGRectMake(CGRectGetMinX(badgeBackgroundFrame) + textOffset, CGRectGetMinY(badgeBackgroundFrame) + textOffset, badgeSize.width, badgeSize.height) withFont: [self badgeTextFont] lineBreakMode: NSLineBreakByTruncatingTail alignment: NSTextAlignmentCenter];#endif
- }
- }
- CGContextRestoreGState(context);
- }#pragma mark - Image configuration - (UIImage * ) finishedSelectedImage {
- return [self selectedImage];
- } - (UIImage * ) finishedUnselectedImage {
- return [self unselectedImage];
- } - (void) setFinishedSelectedImage: (UIImage * ) selectedImage withFinishedUnselectedImage: (UIImage * ) unselectedImage {
- if (selectedImage && (selectedImage != [self selectedImage])) { [self setSelectedImage: selectedImage];
- }
- if (unselectedImage && (unselectedImage != [self unselectedImage])) { [self setUnselectedImage: unselectedImage];
- }
- } - (void) setBadgeValue: (NSString * ) badgeValue {
- _badgeValue = badgeValue;
- [self setNeedsDisplay];
- }#pragma mark - Background configuration - (UIImage * ) backgroundSelectedImage {
- return [self selectedBackgroundImage];
- } - (UIImage * ) backgroundUnselectedImage {
- return [self unselectedBackgroundImage];
- } - (void) setBackgroundSelectedImage: (UIImage * ) selectedImage withUnselectedImage: (UIImage * ) unselectedImage {
- if (selectedImage && (selectedImage != [self selectedBackgroundImage])) { [self setSelectedBackgroundImage: selectedImage];
- }
- if (unselectedImage && (unselectedImage != [self unselectedBackgroundImage])) { [self setUnselectedBackgroundImage: unselectedImage];
- }
- }@end
来源: http://lib.csdn.net/snippet/ios/42775