Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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;
}