Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with titleFormatter in ios 7.1 #104

Closed
ostatnicky opened this issue Apr 17, 2015 · 4 comments
Closed

Problem with titleFormatter in ios 7.1 #104

ostatnicky opened this issue Apr 17, 2015 · 4 comments

Comments

@ostatnicky
Copy link

It's the similar problem as issue #92 but for titleFormatter.

I have this code:

UIColor *greenColor = [UIColor greenColor];
UIColor *blueColor = [UIColor blueColor];
UIColor *backgroundColor = [UIColor grayColor];

NSArray *items = [NSArray arrayWithObjects:[NSLocalizedString(@"First", @"") uppercaseString], [NSLocalizedString(@"Second", @"") uppercaseString], nil];

HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:items];
segmentedControl.frame = CGRectMake(0, 0, viewWidth, 38);
[segmentedControl addTarget:self action:@selector(didChangeValue:) forControlEvents:UIControlEventValueChanged];
[segmentedControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
    UIColor *textColor = (selected) ? greenColor : blueColor;

    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : textColor, NSFontAttributeName: [UIFont systemFontOfSize:17.0]}];
    return attString;
}];
segmentedControl.selectionIndicatorColor = greenColor;
segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
segmentedControl.selectionIndicatorHeight = 3;
segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;
segmentedControl.selectionIndicatorBoxOpacity = 0;
segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 20, 0, 20);
segmentedControl.backgroundColor = backgroundColor;
[self.view addSubview:segmentedControl];

Log is saying:

HMSegmentedControlExample[6675] <Error>: CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

I tried it on 1.5.1.

@yaoandw
Copy link

yaoandw commented May 4, 2015

in HMSegmentedControl.m
try replace

 titleLayer.string = [self attributedTitleAtIndex:idx];

with

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
                titleLayer.string = [self attributedTitleAtIndex:idx];
            }else{
                NSAttributedString *str = [self attributedTitleAtIndex:idx];
                titleLayer.string = str.string;
                [str enumerateAttributesInRange:NSMakeRange(0, str.length) options:(0) usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
                    UIFont *font = attrs[NSFontAttributeName];
                    UIColor *color = attrs[NSForegroundColorAttributeName];
                    [titleLayer setFont:(__bridge CFTypeRef)(font.fontName)];
                    [titleLayer setFontSize:font.pointSize];
                    [titleLayer setForegroundColor:color.CGColor];
                }];
            }

@pranavss11
Copy link

Any updates on this? It would be nice to have a pod update for this @HeshamMegid

@HeshamMegid
Copy link
Owner

@pranavss11 I haven't had the time to work on a fix yet and test it on iOS 7, but feel free to send a pull request and I'll merge it.

The correct fix should be similar to d8e6e4a.

@HeshamMegid
Copy link
Owner

This should be fixed in the latest release. Feel free to reopen if it's not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants