Created
June 9, 2014 20:46
-
-
Save guilhermearaujo/d11bfc85745c76653f9c to your computer and use it in GitHub Desktop.
UISegmentedControl+Multiline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.