Skip to content

Instantly share code, notes, and snippets.

@guilhermearaujo
Created June 9, 2014 20:46
Show Gist options
  • Save guilhermearaujo/d11bfc85745c76653f9c to your computer and use it in GitHub Desktop.
Save guilhermearaujo/d11bfc85745c76653f9c to your computer and use it in GitHub Desktop.
UISegmentedControl+Multiline
//
// UISegmentedControl+Multiline.h
//
// Created by Guilherme Araújo on 6/9/14.
// Copyright (c) 2014 Guilherme Araújo. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UISegmentedControl (Multiline)
- (void)insertSegmentWithMultilineTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;
- (void)insertSegmentWithMultilineAttributedTitle:(NSAttributedString *)attributedTitle atIndex:(NSUInteger)segment animated:(BOOL)animated;
@end
//
// UISegmentedControl+Multiline.m
//
// Created by Guilherme Araújo on 6/9/14.
// Copyright (c) 2014 Guilherme Araújo. All rights reserved.
//
#import "UISegmentedControl+Multiline.h"
@interface UIView (LayerShot)
- (UIImage *)imageFromLayer;
@end
@implementation UIView (LayerShot)
- (UIImage *)imageFromLayer {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
@implementation UISegmentedControl (Multiline)
- (void)insertSegmentWithMultilineTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setTextColor:[self tintColor]];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont systemFontOfSize:13]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setNumberOfLines:0];
[label setText:title];
[label sizeToFit];
[self insertSegmentWithImage:[label imageFromLayer] atIndex:segment animated:animated];
}
- (void)insertSegmentWithMultilineAttributedTitle:(NSAttributedString *)attributedTitle atIndex:(NSUInteger)segment animated:(BOOL)animated {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setTextColor:[self tintColor]];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont systemFontOfSize:13]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setNumberOfLines:0];
[label setAttributedText:attributedTitle];
[label sizeToFit];
[self insertSegmentWithImage:[label imageFromLayer] atIndex:segment animated:animated];
}
@end
@ugurcetinkaya
Copy link

Thanks for this gits. I have a question. When i add a mutliline segment, text color is always UISegmentedControl 's tintColor. If i change it, it at this Category, is still same color. Do you know why this happened? How can i change mutliline segment' title color for unselected case?

@acegreen
Copy link

acegreen commented Jul 2, 2016

@ugurcetinkaya see the comment above NSAttributedString

// the underlying attributed string drawn by the label, if set, the label ignores the properties above.

Also the label is converted to an image and the segmentedControl image tintColor takes over. You will need to override that programmatically or in storyboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment