Rectangle 27 0

Every segment of the segmented control can display a title (an NSString object) or an image (UIImage object). The UISegmentedControl object automatically resizes segments to fit proportionally within their superview, unless they have a specific width set. When you add and remove segments, you can request that the action be animated with sliding and fading effects.

In ios version 5.0 and later, you can customize the appearance of all segmented controls using the appearance proxy (for example: [UISegmentedControl appearance]), or just of a single control.

'uisegmentedcontrol' tag wiki - Stack Overflow

uisegmentedcontrol
Rectangle 27 0

Creating a subclass of UISegmentedControl, overwriting

[MySegmentedControl initWithItems:] 
-(id) initWithItems:(NSArray*)items {
    [self setImage:anImage forState:aState barMetrics:UIBarMetricsDefault];
    [self setTitleTextAttributes:aDict forState:aState];
    [self setDividerImage:anImage forLeftSegmentState:aState rightSegmentState:aState barMetrics:UIBarMetricsDefault];
    ... do more UI stuff ...
}

And in your view controller, you call it with:

-(void)viewDidLoad {
    [self.view addSubview:[MySegmentedControl alloc] initWithItems:@[@"one",@"two"]]];
    ...

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

ios - UISegmentedControl with Image and Title - Stack Overflow

ios uiimage uisegmentedcontrol
Rectangle 27 0

You can do it:

@interface UIImage (UISegmentIconAndText)

+ (id) imageFromImage:(UIImage*)image string:(NSString*)string color:(UIColor*)color;
+ (id) imageFromImage:(UIImage*)image string:(NSString*)string color:(UIColor*)color position:(NSString*)position;

@end
#import "UIImage+UISegmentIconAndText.h"

@implementation UIImage (UISegmentIconAndText)
+ (id) imageFromImage:(UIImage*)image string:(NSString*)string color:(UIColor*)color
{
    UIFont *font = [UIFont systemFontOfSize:16.0];
    CGSize expectedTextSize = [string sizeWithAttributes:@{NSFontAttributeName: font}];
    int width = expectedTextSize.width + image.size.width + 5;
    int height = MAX(expectedTextSize.height, image.size.width);
    CGSize size = CGSizeMake((float)width, (float)height);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    int fontTopPosition = (height - expectedTextSize.height) / 2;
    CGPoint textPoint = CGPointMake(image.size.width + 5, fontTopPosition);

    [string drawAtPoint:textPoint withAttributes:@{NSFontAttributeName: font}];
    // Images upside down so flip them
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, size.height);
    CGContextConcatCTM(context, flipVertical);
    CGContextDrawImage(context, (CGRect){ {0, (height - image.size.height) / 2}, {image.size.width, image.size.height} }, [image CGImage]);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
@end

in your code implement Segment:

UIImage *imageWithText = [UIImage imageFromImage:[UIImage imageNamed:@"images/jobaids_tab_icon"] string:@"Hello", nil) color:[UIColor whiteColor]];

[_segmentedController setImage:imageWithText forSegmentAtIndex:0];

Works well, but font sizes are off. The text size seems to get smaller as the size of the string increases

iphone - UISegmentedControl with Image and Title - Stack Overflow

iphone objective-c ios cocoa-touch uisegmentedcontrol
Rectangle 27 0

Assuming segmentedControl is in your .h as

@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;

Do this in your .m for your viewController and set up the segments in code

[self.segmentedControl insertSegmentWithTitle:@"Title with Image" atIndex:0 animated:YES];
        [self.segmentedControl setImage:[UIImage imageNamed:@"whatever.png"] forSegmentAtIndex:0];

no, it doesn't work, it replaces the title with the image

No, it does not work. I was in error. You may want to try this one on github -> [github.com/u10int/URBSegmentedControl

ios - UISegmentedControl with Image and Title - Stack Overflow

ios uiimage uisegmentedcontrol
Rectangle 27 0

I don't think this is possible using a segmented control, unless you want to use an image for the text, since UISegmentedControl doesn't support attributed strings. I would use 3 UIButtons with attributed strings as the titles, so you can have different font sizes for different lines. The triangle at the bottom can be drawn easily enough using a CAShapeLayer. It would be best to subclass UIButton so you can put the logic necessary to construct the title with different fonts, and do the drawing outside of your view controller class.

ios - Custom buttons with swift - Stack Overflow

ios swift button uibutton
Rectangle 27 0

UISegmentedControl* searchButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"", nil]];
[searchButton  insertSegmentWithImage:[UIImage imageNamed:@"up_button.png"] atIndex:0 animated:YES];
[searchButton  insertSegmentWithImage:[UIImage imageNamed:@"down_button.png"] atIndex:1 animated:YES];
searchButton.segmentedControlStyle = UISegmentedControlStyleBar;
searchButton.tintColor = [UIColor colorWithRed:0.27 green:0.60 blue:0.20 alpha:1.00];
[searchButton  setMomentary:YES];

I don't think this is possible. The documentation says the following: A segmented control can display a title (an NSString object) or an image (UIImage object). if you want to set title : [searchButton insertSegmentWithTitle:@"Up" atIndex:0 animated:NO]; [searchButton insertSegmentWithTitle:@"Down" atIndex:1 animated:NO];

iphone - UISegmentedControl with Image and Title - Stack Overflow

iphone objective-c ios cocoa-touch uisegmentedcontrol