Skip to content

maximbilan/UISegmentedControl_IconAndText

Repository files navigation

Icon and text in UISegmentedControl

A simple category for adding icon and text together to default UISegmentedControl.

alt tag

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

About

A simple category for adding icon and text together to default UISegmentedControl

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy