Could someone please help me on implementing a segmentedViewController by appending image and title(both)..Image on top and title is at the bottom of the image in swift language. I have gone through staackoverflow and could find solution in objective C.While converting it to Swift I have stuck at the following objective-c statement
CGContextDrawImage(context, (CGRect){ {0, (height - image.size.height) / 2}, {image.size.width, image.size.height} }, [image CGImage]);
Link I followed: UISegmentedControl with Image and Title
My Swift conversion code:
extension UIImage {
public func textEmbededImage(image: UIImage, string: String, color:UIColor) -> UIImage {
let font = UIFont.systemFontOfSize(16.0)
let expectedTextSize: CGSize = (string as NSString).sizeWithAttributes([NSFontAttributeName: font])
let width: CGFloat = expectedTextSize.width + image.size.width + 5.0
let height: CGFloat = max(expectedTextSize.height, image.size.width)
let size: CGSize = CGSizeMake(width, height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
let context: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextSetFillColorWithColor(context, color.CGColor)
let fontTopPosition: CGFloat = (height - expectedTextSize.height) / 2.0
let textPoint: CGPoint = CGPointMake(image.size.width + 5, fontTopPosition)
(string as NSString).drawAtPoint(textPoint, withAttributes: [NSFontAttributeName: font])
let flipVertical: CGAffineTransform = CGAffineTransformMake(1, 0, 0, -1, 0, size.height)
CGContextConcatCTM(context, flipVertical)
//I am stuck here.. please help me on it.
//Images upside down so flip them
return image
}