CarbonKit icon indicating copy to clipboard operation
CarbonKit copied to clipboard

Possible to use a custom UIView as item?

Open simonarcher opened this issue 8 years ago • 5 comments

Hi there,

Im looking to customize the title bar and use a 'custom view' of sorts for each item. Specifically, im hoping to have an image with a label below it. (So not just a UIImage or a text title, as I see you can do straight out the box)

I tried to create a new UIView and add it to the Items array, but it seems to crash... I could VERY well be doing it wrong, but I just want to know is it supported to show/pass a UIView into the Item array that it initialises with, or not.

If it is, could you perhaps provide me a very brief example with how I could do this?

Thank you VERY much! :)

simonarcher avatar May 07 '16 14:05 simonarcher

Digging a little deeper, I noticed a comment saying that it specifically only supports Strings and ImageViews.

Wondering if there is any plan to support a custom view within the segment controller.

Thanks!

simonarcher avatar May 07 '16 16:05 simonarcher

I managed to add an icon with text in the tabs by creating a new uiimage using following code, it's not the best solution but for now it does the job :)

func generateImageWithText(text: String,image:UIImage) -> UIImage{

        let imageView:UIImageView = UIImageView(image: image)
        imageView.backgroundColor = UIColor.clearColor()
        imageView.frame = CGRectMake(0,0, image.size.width / 1.75, image.size.height / 1.75)



        var label:UILabel = UILabel(frame: CGRectMake(0, 0, imageView.frame.width * 2, imageView.frame.height ))

        switch(text.characters.count){
        case 0:
            // huh, what impossible
            label = UILabel(frame: CGRectMake(0, 0, imageView.frame.width, imageView.frame.height ))

            break
        case 1:
            label = UILabel(frame: CGRectMake(0, 0, imageView.frame.width * 1.6, imageView.frame.height ))
            break
        case 2:
            label = UILabel(frame: CGRectMake(0, 0, imageView.frame.width * 2, imageView.frame.height ))
            break
        case 3:
            label = UILabel(frame: CGRectMake(0, 0, imageView.frame.width * 2.5, imageView.frame.height ))
            break
        case 4:
            label = UILabel(frame: CGRectMake(0, 0, imageView.frame.width * 3, imageView.frame.height ))
            break
        case 5:
            label = UILabel(frame: CGRectMake(0, 0, imageView.frame.width * 3.5, imageView.frame.height ))
            break
        default:
            label = UILabel(frame: CGRectMake(0, 0, imageView.frame.width * 2, imageView.frame.height ))
            break
        }

        label.backgroundColor = UIColor.clearColor()
        label.textAlignment = .Right
        label.textColor = UIColor.darkGrayColor()
        label.font = PingvalueFonts.LIGHT_FONT_14
        label.text = text


        UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0)
        imageView.layer.renderInContext(UIGraphicsGetCurrentContext()!)

        label.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let imageWithText:UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return imageWithText
    }

ghost avatar Sep 06 '16 10:09 ghost

@ghost Objective C code?

Neeraj204 avatar Mar 20 '17 07:03 Neeraj204

Well its quite not possible to use custom view because of following reasons.

CarbonTabSwipeSegmentedControl is subclass of UISegmentedControl in UIKit. Following is the method that is used to set tab items and it only accepts String and Image. - (instancetype)initWithItems:(nullable NSArray *)items; // items can be NSStrings or UIImages. control is automatically sized to fit content

aamir-nazir avatar May 19 '17 13:05 aamir-nazir

@Neeraj204 Objective C code converted from swift. feel free to point out any mistakes

- (UIImage*) generateImageWithText:(UIImage*)img text:(NSString*)text
{
    UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
    imgView.backgroundColor = [UIColor clearColor];
    imgView.frame = CGRectMake(0, 0, img.size.width/1.75, img.size.height/1.75);

    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, imgView.frame.size.width*2, imgView.frame.size.height*2)];

    switch (text.length)
    {
	    case 0:
	    {
		    lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
	    }
	    break;
	    case 1:
	    {
		    lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, imgView.frame.size.width*1.6, imgView.frame.size.height)];
	    }
		    break;
	    case 2:
	    {
		    lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, imgView.frame.size.width*2, imgView.frame.size.height)];
	    }
		     break;
	    case 3:
	    {
		     lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, imgView.frame.size.width*2.5, imgView.frame.size.height)];
	    }
		     break;
	    case 4:
	    {
		     lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, imgView.frame.size.width*3, imgView.frame.size.height)];
	    }
		    break;
	    case 5:
	    {
		    lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, imgView.frame.size.width*3.5, imgView.frame.size.height)];
	    }
		    break;
	    default:
	    {
		    lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, imgView.frame.size.width*2, imgView.frame.size.height)];
	    }
		    break;
    }

    lbl.backgroundColor = [UIColor clearColor];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.textColor = [UIColor whiteColor];
    lbl.text = text;

    UIGraphicsBeginImageContextWithOptions(lbl.bounds.size, NO, 0);
    [imgView.layer renderInContext:UIGraphicsGetCurrentContext()];

    [lbl.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *imageWithText = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

	    return imageWithText;
}

rajdhakate avatar May 26 '17 07:05 rajdhakate