Make the label in an iOS UISegmentedcontrol span 2 lines - ios

In my app, I am using a UISegmentedcontrol. Because the label in each of the three segments spans two lines, I need to customize the UISegmentedcontrol. I want to do two things: (1) Increase the height of the UISegmentedcontrol, and (2) Make the text label span two lines
I found the solution to #1 here: iOS: change the height of UISegmentedcontrol and here Change Height of UISegmentedControl and here http://iphonedevsdk.com/forum/iphone-sdk-development/74275-change-height-of-segmented-control.html
I couldn't find out how to make the label inside the UISegmentedcontrol span two lines. Any help would be greatly appreciated!
Cheers,
Frank

try this...
create a Category for UISegmentedControl ,suppose UISegmentedControl+Multiline
in UISegmentedControl+Multiline.h
#import <UIKit/UIKit.h>
#interface UISegmentedControl (Multiline)
- (void)insertSegmentWithMultilineTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;
#end
and in UISegmentedControl+Multiline.m
#import "UISegmentedControl+Multiline.h"
#interface UIView (LayerShot)
- (UIImage *)imageFromLayer;
#end
#implementation UIView (LayerShot)
- (UIImage *)imageFromLayer {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#end
#implementation UISegmentedControl (Multiline)
- (void)insertSegmentWithMultilineTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setTextColor:[self tintColor]];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont systemFontOfSize:13]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setNumberOfLines:0];
[label setText:title];
[label sizeToFit];
[self insertSegmentWithImage:[label imageFromLayer] atIndex:segment animated:animated];
}
#end
finally import UISegmentedControl+Multiline.h in your class and use as follows
UISegmentedControl * segmentControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(3, 66, 314, 30)];
[segmentControl insertSegmentWithMultilineTitle:#"First\nLine" atIndex:0 animated:YES];
[segmentControl insertSegmentWithMultilineTitle:#"Second\nLine" atIndex:1 animated:YES];
[segmentControl insertSegmentWithMultilineTitle:#"Third\nLine" atIndex:2 animated:YES];
[segmentControl setSelectedSegmentIndex:0];
[segmentControl addTarget:self action:#selector(segmentControlClicked:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentControl];

Related

ios 8 - scroll does not work

I am using UIScrollView . At run time I am adding UIButton into scrollview. In iOS 7 it work perfectly but in iOS 8 I am getting problem on scrolling (it is very hard to scroll )..
Here is my code . Please suggest me What am I doing wrong or in iOS 8 is there any new property? .
-(void)renderAllCatealogue:(NSArray *)catalogueArr
{
[[scollViewCatlog subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
int x=0;
for (NSDictionary *dict in catalogueArr)
{
UIView *view =[[UIView alloc]initWithFrame:CGRectMake(x, 8, 1, 16)];
[view setBackgroundColor:[UIColor whiteColor]];
[scollViewCatlog addSubview: view];
//calculate width
UIFont *font=[UIFont fontWithName:BOLD_FONT size:15];
CGFloat width=[[CommonUISetting sharedInstance]widthOfString:[dict valueForKey:#"catalogTitle"] withFont:font];
UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(x+10, 1, width, 30)];
[btn setTitle:[dict valueForKey:#"catalogTitle"] forState:UIControlStateNormal];
[btn setTag:[[dict valueForKey:#"catalogId"] integerValue]];
[btn.titleLabel setFont:font];
[btn addTarget:self action:#selector(catlogBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn.titleLabel setFont:[UIFont fontWithName:BOLD_FONT size:15]];
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[scollViewCatlog addSubview:btn];
x=x+width +15;
}
[scollViewCatlog setContentSize:CGSizeMake(x, 1)];
}
i got solution .
I create a subclass of UIScrollview and add this method in it.
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return YES;
}
I think you should use UICollectionView in order to generate something you want. UICollectionView allows to create vertical as well as horizontal infinite scroll panel. It is similar as the UITableView, you just have to setup some DataSource Methods and Delegate Methods.
look here for sample of UICollectionView

How to create gmail recipient format in iOS?

I need to create a view like below from the array of emails that I receive which also has the delete option.
Something similar to that of gmail's mail recipient except in my this should be scrollview. My main issue is to create the background with delete button which stretches based on the email length. My current approach is to use 3 images one for the beginning, one for the end with delete button and one in general for middle that will stretch. Is there any other or better way to do this?
NOTE:Need to support from iOS 5 and above
Here you go, I have created a SuperLabel for you though it might need some tweak.. But it will help you for sure..
SuperLabel.h
#import <UIKit/UIKit.h>
#interface SuperLabel : UIView
- (id)initWithFrame:(CGRect)frame andTitle:(NSString *)title;
#end
SuperLabel.m
#import "SuperLabel.h"
#define MAX_HEIGHT 25.0
#implementation SuperLabel
- (id)initWithFrame:(CGRect)frame andTitle:(NSString *)title
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//Design your label view
self.backgroundColor=[UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1.0];
self.layer.borderColor=[[UIColor orangeColor] CGColor];
self.layer.borderWidth=1.0;
self.layer.cornerRadius=5.0;
//Add a Label
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, 100.0, MAX_HEIGHT)];
label.font=[UIFont systemFontOfSize:12.0];
label.textColor=[UIColor grayColor];
label.text=title;
[label sizeToFit];
[self addSubview:label];
//We will get resized frame after using sizeToFit after setting the text in the label.
CGRect rect=label.frame;
//Add a button
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:#"x" forState:UIControlStateNormal];
btn.titleLabel.font=[UIFont systemFontOfSize:12.0];
btn.titleLabel.textColor=[UIColor grayColor];
rect.origin.x=rect.origin.x+rect.size.width;
rect.origin.y=0;
rect.size=CGSizeMake(25.0, MAX_HEIGHT);
[self addSubview:btn];
[btn addTarget:self action:#selector(deleteMe:) forControlEvents:UIControlEventTouchDragInside];
btn.frame=rect;
//Change the whole frame of the label view
frame.size.height=MAX_HEIGHT;
frame.size.width=btn.frame.origin.x+btn.frame.size.width;
self.frame=frame;
}
return self;
}
-(IBAction)deleteMe:(id)sender{
[self removeFromSuperview];
}
#end
And finally add to your view by using following code..
SuperLabel *label=[[SuperLabel alloc] initWithFrame:CGRectZero andTitle:#"myemail#gmail.com"];
[self.view addSubview:label];
Cheers..

How can I load a ViewController or View into another ViewController with transparent background?

In my app I want to have a custom popup to appear when called from a UIButton. Currently I'm using TSAlertView, but I want to implement more UI items and have a more custom build. So what I would like to do is load a viewcontroller or view within another viewcontroller and be able to pass information along between them both. See the example below:
From what I've read, supposedly it is not good practice to load another viewcontroller within another. In this case I would assume a uiview would be the recommended way. Is this correct?
I would like the viewcontroller behind the popup to have dimmed look, maybe a dark color with the opacity set at 70%. How can I achieve this?
Essentially I'm trying to build something similar to an AlertView with the appearance of another view.
If you intend to completely block the background view, you could set viewControllerB to Form Sheet.
When you are ready to show viewControllerB, use:
[viewControllerA presentViewController:viewControllerB animated:TRUE completion:nil]
You will need a way to dismiss viewControllerB; a button, network action, etc.
An example with Storyboards:
- (void)someMethod {
UIStoryboard *storyboard = self.storyboard;
ViewControllerB *viewCotrollerB = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerB"];
[self presentModalViewController:viewControllerB animated:YES];
}
Hi SReca,I was also having same requirement(like shown in above image) to create view like UIalertview which you are saying.
Here you will have to create a new UIView class instead of UIViewController, with no need of .xib.
In the new UIView's class in init method you will require to create your userinterface programmatically. And keep its height width as much as you want.
And for loading this new UIView you will have to just create object of that uiview and and call initwithframe it will present that view programmatically like uialerview.
Here is code for referece.
ModalPreviewViewController_iPhone.h
#interface ModalPreviewViewController_iPhone : UIView
#end
ModalPreviewViewController_iPhone.m
just modify this example code as per your need in initWithFrame
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
popUpView = [[UIView alloc] initWithFrame:CGRectMake(30,30,260,420)];
popUpBgView = [[UIView alloc] init];
popUpBgView.frame =CGRectMake(0,0,260,420);
[popUpBgView setBackgroundColor:[UIColor colorWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1]];
[popUpView addSubview:popUpBgView];
imageGalleryView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 260, 360)];
[imageGalleryView setBackgroundColor:[UIColor redColor]];
CGRect btnTempFrame=CGRectMake(5, 385, 90, 30);
btnTemp=[[UIButton alloc]initWithFrame:btnTempFrame];
[btnTemp setBackgroundImage:[UIImage imageNamed:#"btn_plain_iphone.png"] forState:UIControlStateNormal];
[btnTemp.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
[btnTemp.titleLabel setTextColor:[UIColor whiteColor]];
[btnTemp addTarget:self action:#selector(onDownloadClick:) forControlEvents:UIControlEventTouchUpInside];
CGRect btnSubscribeFrame=CGRectMake(100, 385, 90, 30);
UIButton *btnSubscribe=[[UIButton alloc]initWithFrame:btnSubscribeFrame];
[btnSubscribe setBackgroundImage:[UIImage imageNamed:#"btn_plain_iphone.png"] forState:UIControlStateNormal];
[btnSubscribe.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
[btnSubscribe.titleLabel setTextColor:[UIColor whiteColor]];
[btnSubscribe setTitle:#"Subscribe" forState:UIControlStateNormal];
[btnSubscribe addTarget:self action:#selector(moveTosubscribe:) forControlEvents:UIControlEventTouchUpInside];
CGRect closeButtonFrame=CGRectMake(195, 385, 60, 30);
UIButton *closeButton=[[UIButton alloc]initWithFrame:closeButtonFrame];
[closeButton setBackgroundImage:[UIImage imageNamed:#"btn_plain_iphone.png"] forState:UIControlStateNormal];
[closeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
[closeButton.titleLabel setTextColor:[UIColor whiteColor]];
[closeButton setTitle:#"Close" forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(closeView:) forControlEvents:UIControlEventTouchUpInside];
CGRect swipeLabelFrame=CGRectMake(0, 0, 140, 15);
UILabel *swipeLabel=[[UILabel alloc]initWithFrame:swipeLabelFrame];
[swipeLabel setBackgroundColor:[UIColor clearColor]];
[swipeLabel setText:#"> SWIPE TO SEE MORE"];
[swipeLabel setTextColor:[UIColor lightGrayColor]];
[swipeLabel setFont:[UIFont systemFontOfSize:12]];
CGRect dateLabelFrame=CGRectMake(5, 365, 100, 15);
dateLabel=[[UILabel alloc]initWithFrame:dateLabelFrame];
[dateLabel setBackgroundColor:[UIColor clearColor]];
[dateLabel setText:#"July 2013 #93"];
[dateLabel setTextColor:[UIColor whiteColor]];
[dateLabel setFont:[UIFont systemFontOfSize:13]];
CGRect priceLabelFrame=CGRectMake(155, 365, 100, 15);
priceLabel=[[UILabel alloc]initWithFrame:priceLabelFrame];
[priceLabel setBackgroundColor:[UIColor clearColor]];
[priceLabel setText:#"$3.9"];
[priceLabel setTextColor:[UIColor colorWithRed:71.0/255 green:191.0/255 blue:226.0/255 alpha:1]];
[priceLabel setFont:[UIFont systemFontOfSize:13]];
[priceLabel setTextAlignment:NSTextAlignmentRight];
CGRect labelFrame=swipeLabel.frame;
labelFrame.origin=CGPointMake(popUpView.frame.origin.x+popUpView.frame.size.width - labelFrame.size.width, popUpView.frame.origin.y - labelFrame.size.height);
[swipeLabel setFrame:labelFrame];
[popUpView addSubview:imageGalleryView];
[popUpView addSubview:btnTemp];
[popUpView addSubview:closeButton];
[popUpView addSubview:btnSubscribe];
[popUpView addSubview:dateLabel];
[popUpView addSubview:priceLabel];
[UIView beginAnimations:#"curlup" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:popUpView cache:YES];
[self addSubview:popUpView];
[self addSubview:swipeLabel];
[UIView commitAnimations];
UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
self.backgroundColor = color;
[[[UIApplication sharedApplication] keyWindow] addSubview:self];
[self initializeVariables];
}
return self;
}
And in Parent View controller just create object of UIView Class and call it with initWithFrame and size which you want
ModalPreviewViewController_iPhone *mpvc=[ModalPreviewViewController_iPhone alloc];
if(appDelegate.isIphone5)
mpvc=[mpvc initWithFrame:CGRectMake(0,0,320,480)];
else
mpvc=[mpvc initWithFrame:CGRectMake(0,0,320,568)];
And for dismissing UivewClass
write just following sentence to dismiss it ,in one of its(UIviewClass's) method like closeView
[self removeFromSuperview];
And let me know if you face any problem

How to create a multiline navigation bar similar to the one in WhatsApp in iOS?

Does anyone have a clue on how to create a multiline navigation/top bar in iOS? I want something similar to the status line in WhatsApp (the one that displays last seen or the current status information).
Any advice would be appreciated.
I want to achieve something like in the image (Francis Whitman / last seen [...]):
By default, a UINavigationController will use the title property of the currently-displayed view controller for the title in its navigation bar. You can have it display any arbitrary view, however, by setting the titleView property of the view controller’s navigationItem property, as so:
// In your view controller’s implementation (.m) file:
- (id)initWithNibName:(NSString *)nibName
bundle:(NSStirng *)nibBundle
{
self = [super initWithNibName:nibName bundle:nibBundle];
if (self) {
UIView *myTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f,
0.0f,
250.0f,
44.0f)];
[myView setBackgroundColor:[UIColor greenColor]];
[[self navigationItem] setTitleView:myTitleView];
}
return self;
}
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/2, 200)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setNumberOfLines:0];
[label setText:_certificate.name];
self.navigationItem.titleView = label;

How to create a very custom uibutton like UITableViewCellStyleSubtitle in iOS

I would like to create a button with an image, title and description similar to the UITableViewCellStyleSubtitle. I would like to do this programmatically.
Does anyone know how I can accomplish this?
You could create a category of the UIButton class. Here's a barebones setup for you:
In the .h file:
#interface UIButton (YourCategoryName)
- (void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description;
#end
In the .m file:
#implementation UIButton (YourCategoryName)
- void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description {
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 200, 50)]];
[self addSubview:titleLabel];
[titleLabel release];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image];
[self addSubview:imageView];
[imageView release];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 200, 100)];
[self addSubview:descriptionLabel];
[descriptionLabel release];
}
In the above code, you can adjust the frames as needed. Once this has been setup, just create your button like normal in your view/viewcontroller and call the method above:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 300, 200);
[myButton setupButton:myTitle image:myImage description:myDesc];
Subclass UIControl and override drawRect and/or add subviews to create the desired appearance.

Resources