Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maximbilan/uisegmentedcontrol_iconandtext
A simple category for adding icon and text together to default UISegmentedControl
https://github.com/maximbilan/uisegmentedcontrol_iconandtext
icon ios text ui ui-components ui-design uikit uisegmentedcontrol
Last synced: about 2 months ago
JSON representation
A simple category for adding icon and text together to default UISegmentedControl
- Host: GitHub
- URL: https://github.com/maximbilan/uisegmentedcontrol_iconandtext
- Owner: maximbilan
- License: mit
- Created: 2015-06-08T09:37:43.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-09-21T13:18:29.000Z (over 6 years ago)
- Last Synced: 2024-04-20T11:04:27.792Z (8 months ago)
- Topics: icon, ios, text, ui, ui-components, ui-design, uikit, uisegmentedcontrol
- Language: Objective-C
- Homepage:
- Size: 27.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Icon and text in UISegmentedControl
A simple category for adding icon and text together to default UISegmentedControl.
![alt tag](https://raw.github.com/maximbilan/UISegmentedControl_IconAndText/master/img/img1.png)
## Using
[self.segmentedControl setImage:[UIImage imageFromImage:[UIImage imageNamed:@"star"] string:@"First" font:[UIFont color:[UIColor clearColor]] forSegmentAtIndex:0];
## Implementation
+ (id)imageFromImage:(UIImage *)image string:(NSString *)string font:(UIFont *)font color:(UIColor *)color
{
CGSize expectedTextSize = [string sizeWithAttributes:@{NSFontAttributeName: font}];
CGFloat width = expectedTextSize.width + image.size.width;
CGFloat height = MAX(expectedTextSize.height, image.size.width);
CGSize size = CGSizeMake(width, height);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGFloat fontTopPosition = (height - expectedTextSize.height) * 0.5;
CGPoint textPoint = CGPointMake(0, fontTopPosition);
[string drawAtPoint:textPoint withAttributes:@{NSFontAttributeName: font}];
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, size.height);
CGContextConcatCTM(context, flipVertical);
CGContextDrawImage(context, (CGRect){ {expectedTextSize.width, (height - image.size.height) * 0.5}, {image.size.width, image.size.height} }, [image CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}