UIView with adaptable height according to content - Objective-c - iphone

I would like to create an UIView that fits his content. The content is an UILabel with dinamic text.
UIView *myview = [ [UIView alloc ] initWithFrame:CGRectMake(10.0, 10.0, 700.0, 100.0)];
myview.backgroundColor = [UIColor redColor];
[self.view addSubview:myview];
UILabel *myLabel = [ [UILabel alloc ] initWithFrame:CGRectMake(10.0, 10.0, 500.0, 43.0) ];
myLabel.textAlignment = UITextAlignmentLeft;
myLabel.textColor = [UIColor whiteColor];
myLabel.backgroundColor = [UIColor blackColor];
myLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(36.0)];
[myView addSubview:myLabel];
myLabel.numberOfLines = 0;
myLabel.text = #"This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. ";
[myLabel sizeToFit];
I attach a screenshot:
Screenshot

Try this after [myLabel sizeToFit]:
[myView setFrame:myLabel.frame];

This will do the trick:
NSString *tempString = #"This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. ";
CGSize constSize = { 260.0f, 20000.0f };
CGSize textSizeTitle = [tempString sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:constSize lineBreakMode:UILineBreakModeWordWrap];
UILabel *EventTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 260,textSizeTitle.height)];
[EventTitle setText:tempString];
[EventTitle setBackgroundColor:[UIColor clearColor]];
[EventTitle setFont:[UIFont fontWithName:#"TrebuchetMS-Bold" size:20]];
[EventTitle setNumberOfLines:0];
[EventTitle setUserInteractionEnabled:NO];
[EventTitle setTextColor:[UIColor whiteColor]];
[newsBox addSubview:EventTitle];
Just ensure, font size of textSizeTitle & EventTitle should be same.

Related

How to do parameter passing for Creating N number or labels programmatically

I am doing an application where I am creating a LABELS programmatically..What my Requirement is I need to create some 10 LABELS programmatically, so only there Position and Text label changes. Instead of Writing the same function for every Label Thought of doing it By parameter passing, where only v need pass only the Label name and Position of the label...how to implement it?
//Below code shows creation of Labels With Different Names, instead I want to have one function and do parameter passing rather than creating a function for each label.
-(void)Vendorname
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,80,150,20)];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.textColor = UIColorFromRGB(0x124F73);
myLabel.text = #"Vendor Name";
[self.view addSubview:myLabel];
[myLabel release];
}
-(void)address
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,110,350,20)];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.textColor = UIColorFromRGB(0x124F73);
myLabel.text = #"84-21 Gwanheon dong,jungo-gu,seoul,south korea";
[self.view addSubview:myLabel];
[myLabel release];
}
-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.text = labelTitle;
return [myLabel autorelease];
}
[self.view addSubview:[self createLabel:CGRectMake(50,80,150,20):#"ur Title"]];
-(void)createLabel:(NSString *)text :(CGRect)frame{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.textColor = UIColorFromRGB(0x124F73);
myLabel.text = text;
[self.view addSubview:myLabel];
[myLabel release];
}
call the above method...
[self createLabel:#"label1" :frame];
try this one
int x=50;
int y=80
[self.view addSubview:[self createLabel:CGRectMake(x,y,150,20) title:#"Vendore"]];
-(UILabel*)createLabel:(CGRect)frame title:(NSString*)title{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.text = title;
return [myLabel autorelease];
}

ContainerView hide UILabel

I want to use UILabel in ContainerView.
So I am using this code for that.
UILabel *myLabel = [[[UILabel alloc] initWithFrame:CGRectMake(16, 60, 300, 150)] autorelease];
myLabel.numberOfLines = 0;
myLabel.font = [UIFont systemFontOfSize:13.5];
myLabel.text = [theQuiz objectAtIndex:row+3] ;
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.layer.cornerRadius = 8.0;
[myLabel sizeToFit];
[self.view addSubview:myLabel];
//ContainerView
UIView *ChallengeView = [[UIView alloc] initWithFrame:CGRectMake(8, 55, 300, 10 + Challenge.frame.size.height)];
ChallengeView.layer.borderColor = [[UIColor purpleColor ] CGColor];
[ChallengeView setBackgroundColor:[UIColor whiteColor]];
ChallengeView.layer.cornerRadius = 8 ;
ChallengeView.layer.borderWidth = 1.5;
[self.view addSubview:ChallengeView];
[ChallengeView release];
Now problem is that when i set background color for ContainerView it hides the text of myLabel
Any Solution ??
What is happening is that you containerView is being added above label either you add label after containerView or do this:
[self.view bringSubviewToFront:myLabel];
You add the ChallengeView first , Then add the myLabel.
Otherwise u can do as like #xs2bush said,
[self.view bringSubviewToFront:myLabel];
Bcz the ChallengeView hides the label.

Adding a UILabel onto a UINavigationController

I want to draw a long string of text underneath the Title on a UINaviagtionController.
I tried adding a UILabel to by doing :
[appDelegate.NavControll.view addSubview:testLabel];
And the Label appears but doesn't animate/move with the navbar and just seems independent of it.
Is there a way to achieve this or should I leave it alone and come up with another idea for displaying this string somewhere else.
Many Thanks,
Code
Looks a little bit ugly, but it works
You can easily customize appearance by yourself:
self.navigationItem.title = #"Needed title"; // for back button on pushed view
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"Title";
titleLabel.font = [UIFont fontWithName:#"Helvetica" size:18];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.center = CGPointMake(30, 10);
[titleLabel sizeToFit];
[customTitleView addSubview:titleLabel];
[titleLabel release];
UILabel *detailsLabel = [[UILabel alloc] init];
detailsLabel.text = #"details";
detailsLabel.font = [UIFont fontWithName:#"Helvetica" size:9];
detailsLabel.backgroundColor = [UIColor clearColor];
[detailsLabel sizeToFit];
detailsLabel.center = CGPointMake(50, 35);
[customTitleView addSubview:detailsLabel];
[detailsLabel release];
self.navigationItem.titleView = customTitleView;
[customTitleView release];
you can add a label in your title bar and then on that label you can show your title and your long string both.
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[titleLabel setBackgroundColor:[UIColor clearColor]];
// here's where you can customize the font size
[titleLabel setFont:[UIFont boldSystemFontOfSize:16.0]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setText:#"hello\nhii"];
[titleLabel setNumberOfLines:2];
[titleLabel setLineBreakMode:UILineBreakModeTailTruncation];
[titleLabel sizeToFit];
[titleLabel setCenter:[self.navigationItem.titleView center]];
[self.navigationItem setTitleView:titleLabel];
[titleLabel release];

UINavigationBar title

I am trying to use a UILabel to replace the title at the UINavigationBar, the code is as follows:
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setBackgroundColor:[UIColor blackColor]];
UILabel * nav_title = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 220, 25)];
nav_title.font = [UIFont fontWithName:#"Arial-BoldMT" size:18];
nav_title.textColor = [UIColor whiteColor];
nav_title.adjustsFontSizeToFitWidth = YES;
nav_title.text = title;
nav_title.backgroundColor = [UIColor clearColor];
[bar addSubview:nav_title];
[nav_title release];
The problem is that, how do I remove the original title of the bar? I didn't declare any self.title = #"title", but it always shows it there:
If I do self.title = nil, then everything is gone... How do eliminate this mysterious title from the navbar and just use the UILabel I created.
Why don't you just do self.title = #""?
EDIT: Try this?
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setBackgroundColor:[UIColor blackColor]];
UILabel * nav_title = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 220, 25)];
nav_title.font = [UIFont fontWithName:#"Arial-BoldMT" size:18];
nav_title.textColor = [UIColor whiteColor];
nav_title.adjustsFontSizeToFitWidth = YES;
nav_title.text = title;
self.title = #"";
nav_title.backgroundColor = [UIColor clearColor];
[bar addSubview:nav_title];
[nav_title release];
Use self.navigationItem.titleView = nav_title; instead of adding your label as a subview.
Use this:
UILabel *label = [[UILabel alloc]init];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:self.title];
label.adjustsFontSizeToFitWidth=YES;
label.lineBreakMode=UILineBreakModeWordWrap;
label.numberOfLines=0;
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];
Hope this will help u..!

UINavigationItem titleView position

I'm using UINavigationItem's titleView property to set a custom UILabel with my desired font size/color. Here's my code:
self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 44.0)];
self.headerLabel.textAlignment = UITextAlignmentCenter;
self.headerLabel.backgroundColor = [UIColor clearColor];
self.headerLabel.font = [UIFont boldSystemFontOfSize:20.0];
self.headerLabel.textColor = [UIColor colorWithRed:0.259 green:0.280 blue:0.312 alpha:1.0];
self.navigationItem.titleView = self.headerLabel;
In the navigation bar I also have a left bar button. The result is: the text isn't properly centered. I've tried setting the x origin of the label, but this has no effect.
In stead of initWithFrame just use init and put [self.headerLabel sizeToFit] after your last line of code.
If you make the headerLabel a subview of the titleView, you can then set headerLabel's frame to control where it goes within the titleView.
The way you are doing it now, you don't have that control. I think the OS chooses the titleView's frame for you based on the space available.
Hope this helps!
I've used custom title labels for my nav bars in every app I have in the app store. I've tested many different ways of doing so and by far the easiest way to use a custom label in a navigation bar is to completely ignore titleView and insert your label directly into navigationController.view.
With this approach, it's easy to have the title label's frame always match the navigationBar's frame -- even if you are using a custom navBar with a non-standard size.
- (void)viewDidLoad {
[self.navigationController.view addSubview:self.titleLabel];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didRotateFromInterfaceOrientation:
(UIInterfaceOrientation)fromInterfaceOrientation {
[self frameTitleLabel];
}
- (UILabel *) titleLabel {
if (!titleLabel) {
titleLabel = [[UILabel alloc]
initWithFrame:self.navigationController.navigationBar.frame];
titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
titleLabel.text = NSLocalizedString(#"Custom Title", nil);
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
}
return titleLabel;
}
- (void) frameTitleLabel {
self.titleLabel.frame = self.navigationController.navigationBar.frame;
}
The one caveat to this approach is that your title can flow over the top of any buttons you have in the navBar if you aren't careful and set the title to be too long. But, IMO, that is a lot less problematical to deal with than 1) The title not centering correctly when you have a rightBarButton or 2) The title not appearing if you have a leftBarButton.
I have a same problem; I just somehow solved this issue by calculating the title length and set the label frame width accordingly. Although this is not a perfect one but can be manageable. Here is the code.
label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.font = [ UIFont fontWithName: #"XXII DIRTY-ARMY" size: 32.0 ];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.0f];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor orangeColor];
//label.text=categoryTitle;
CGFloat verticalOffset = 2;
NSString *reqSysVer = #"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
if (categoryTitle.length > 8)
{
label.frame = CGRectMake(0, 0, 300, 44);
}else {
label.frame = CGRectMake(0, 0, 80, 44);
}
self.navigationItem.titleView = label;
self.navigationItem.title=label.text;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setTintColor:[UIColor newBrownLight]];
}
Just calculate exact frame size needed and align to left:
UIFont* font = [UIFont fontWithName:#"Bitsumishi" size:20];
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [title sizeWithFont:font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeCharacterWrap];
CGRect frame = CGRectMake(0, 0, expectedLabelSize.width, expectedLabelSize.height);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.font = font;
label.textAlignment = UITextAlignmentLeft;
label.text = title;
self.titleView = label;
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
lbl.text = #"Home";
lbl.textAlignment = UITextAlignmentCenter;
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:20];
lbl.shadowColor = [UIColor blackColor];
lbl.shadowOffset = CGSizeMake(0,1);
self.navigationItem.titleView = vw;
[self.navigationItem.titleView addSubview:lbl];
What worked for me was to update the titleView frame in the viewDidAppear method.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView *titleView = self.navigationItem.titleView;
CGRect navBarFrame = self.navigationController.navigationBar.frame;
[titleView setFrame:CGRectMake((CGRectGetWidth(navBarFrame) - TitleWidth) / 2, (CGRectGetHeight(navBarFrame) - TitleHeight) / 2, TitleWidth, TitleHeight)];
}