UILabel's numberofLines issue - iphone

I have to display some text(dynamic) which is in three string variables like this:
SanFransisco,California 32122 i.e city,state zipCode
So thought of displaying them in three labels.But I couldnot understand when to use sizetoFit and when not to.
If it is a large text like BrightWood Park,District of Columbia 32123 then Im getting it like BrightWood Park,District of Columb .I couldnot see the zipCode part on the simulator.So Whatever the text mightbe it should be displayed in the simulator.
If city's text is large like Massachusetts Avenue Heights,District of Columbia 32123 then it should be displayed like Massachusetts Avenue Heights,District of
Columbia 32123
Currently Im getting as Massachusetts Avenue Heights,District of Co
NSString *city=[NSString stringWithFormat:#"%#,",self.city];
CGSize constraint4 = CGSizeMake(250, 2000.0f);
CGSize size4=[city sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
lblCity=[[UILabel alloc] init];
[lblCity setFrame:CGRectMake(60,Lane1.frame.size.height+Lane1.frame.origin.y,size4.width,size4.height) ];
lblCity.textAlignment=UITextAlignmentLeft;
lblCity.backgroundColor=[UIColor clearColor];
lblCity.text=[NSString stringWithFormat:#"%#",city];
[lblCity setNumberOfLines:0];
lblCity.highlightedTextColor=[UIColor greenColor];
[lblCity setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[testscroll addSubview: lblCity];
NSString *state=[NSString stringWithFormat:#"%# ",self.state];
CGSize constraint5 = CGSizeMake(250, 2000.0f);
CGSize size5=[state sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
lblState=[[UILabel alloc] init];
[lblState setFrame:CGRectMake(lblCity.frame.origin.x+lblCity.frame.size.width,Lane1.frame.size.height+Lane1.frame.origin.y,size5.width,size5.height) ];
lblState.textAlignment=UITextAlignmentLeft;
lblState.backgroundColor=[UIColor clearColor];
lblState.text=[NSString stringWithFormat:#"%#" ,state];
[lblState setNumberOfLines:0];
lblState.highlightedTextColor=[UIColor greenColor];
[lblState setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[testscroll addSubview: lblState];
NSString *zip=[NSString stringWithFormat:#"%#",self.zip];
CGSize constraint200=CGSizeMake(250,2000.0f);
CGSize size200=[zip sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]constrainedToSize:constraint200 lineBreakMode:UILineBreakModeWordWrap ];
zipCode=[[UILabel alloc] init];
[zipCode setFrame:CGRectMake(lblState.frame.origin.x+lblState.frame.size.width,Lane1.frame.size.height+Lane1.frame.origin.y,size200.width,size200.height) ];
zipCode.textAlignment=UITextAlignmentLeft;
zipCode.backgroundColor=[UIColor clearColor];
zipCode.text=[NSString stringWithFormat:#"%#" ,zip];
[zipCode setNumberOfLines:0];
zipCode.highlightedTextColor=[UIColor greenColor];
[zipCode setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[testscroll addSubview:zipCode];
2.When to use sizetoFit along numberofLines=0 and when not to ?

There are several important points to make autofit UILabel.
At first you have to set label's height with zero
At second you have to set label' numberOfLines to zero too
And in the end you have to call sizeToFit method
Like this:
label.frame = CGRectMake(x, y, width, 0.0f);
label.numberOfLines = 0;
[label sizeToFit];

After get the string, set UILabel size like this
lblCity.text=[NSString stringWithFormat:#"%#",city];
[lblCity setNumberOfLines:0];
[lblCity setFrame:CGRectMake(60,Lane1.frame.size.height+Lane1.frame.origin.y,size4.width,size4.height) ];
[lblCity sizeToFit]; it may be work
///here i tested work fine and show whole text
NSString *city=#"Orlando ";
CGSize constraint4 = CGSizeMake(250, 2000.0f);
CGSize size4=[city sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"size4.width %f height %f ",size4.width,size4.height);
UILabel* lblCity=[[UILabel alloc] init];
[lblCity setFrame:CGRectMake(160,16,size4.width,size4.height) ];
lblCity.textAlignment=UITextAlignmentLeft;
lblCity.backgroundColor=[UIColor clearColor];
lblCity.text=[NSString stringWithFormat:#"%#",city];
[lblCity setNumberOfLines:0];
lblCity.highlightedTextColor=[UIColor greenColor];
[lblCity setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[lblCity sizeToFit];
[self.view addSubview: lblCity];

Related

UILabel have to fit size with out break in middle of the word

I created a UILabel dynamically and i will update the data in to that dynamically.
Am getting output with many number of lines and it is fine
my problem is word break in middle to the next line although i use line break mode word wrap
Help me out of this am googling it from 5hours.
The data passed to that UILabel is orthogonist,endonist,psychologist,cardioligist
CGSize constraint8 = CGSizeMake(190, 2000.0f);
CGSize size8=[temp sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:14] constrainedToSize:constraint8 lineBreakMode:UILineBreakModeWordWrap];
specialities1 =[[UILabel alloc]init];
[specialities1 setFrame:CGRectMake(124,218,190, size8.height)];
specialities1.textAlignment=UITextAlignmentLeft;
specialities1.backgroundColor=[UIColor clearColor];
specialities1.numberOfLines=0;
specialities1.lineBreakMode=UILineBreakModeClip;
specialities1.font=[UIFont fontWithName:#"Helvetica-Bold" size:14];
specialities1.text=[NSString stringWithFormat:#"%# ",temp ];
[specialities1 sizeToFit];
[testscroll addSubview:specialities1];
Change:
specialities1.lineBreakMode=UILineBreakModeClip;
to :
specialities1.lineBreakMode= UILineBreakModeWordWrap;
Change both this line in your code.
According to your code above you are setting 0 lines in your label. So change it to,
specialities1.numberOfLines=size8.height/12;//(=1000, or whatever according to your requirement)
specialities1.lineBreakMode= UILineBreakModeWordWrap;
Source
Get size
NSString *text = [dicOtherInfo objectForKey:#"msg"];
CGSize constraint = CGSizeMake(200, 1000.0f);
CGSize size = [text sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
Set Size
UILabel *lblMsg=[[UILabel alloc] initWithFrame:CGRectMake(5,15, size.width, size.height)];
[lblMsg setLineBreakMode:UILineBreakModeWordWrap];
lblMsg.text = text;
lblMsg.numberOfLines = (size.height/16);
lblMsg.textAlignment=UITextAlignmentLeft;
lblMsg.backgroundColor=[UIColor clearColor];
lblMsg.textColor=[UIColor redColor];
[lblMsg setFont:[UIFont fontWithName:#"Verdana" size:12]];
Try my below method:
-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
and use it like below:
UILabel *lblAddress = [[UILabel alloc]init];
[lblAddress setFrame:CGRectMake(110, 31, 200, 50)];
lblAddress.text = #"your Text ";
lblAddress.lineBreakMode = UILineBreakModeWordWrap;
lblAddress.numberOfLines = 0;
lblAddress.font = [UIFont fontWithName:#"Helvetica" size:12];
lblAddress.frame = CGRectMake(lblAddress.frame.origin.x, lblAddress.frame.origin.y,
200,[self calculateHeightOfTextFromWidth:lblAddress.text :lblAddress.font :200 :UILineBreakModeWordWrap] );
lblAddress.textColor = [UIColor darkGrayColor];
[self.view addSubview:lblAddress];

How to get Map Annotation title style/font in label

Can any one tell me that how can i get the map annotation title's font/style in a UIlabel
I want the exact style/font in the label ..
UILabel *name = [[UILabel alloc]init];
name.text = #"Hello Annotation";
name.textColor = [UIColor whiteColor];
[name setFont: [UIFont fontWithName:#"Helvetica" size:20.0]];
what else should i need to include to get the exact style as of the annotation title ?
try
[name setFont: [UIFont boldSystemFontOfSize:20.0]];
also play with shadow settings.
[name setShadowColor:[UIColor blackColor]];
[name setShadowOffset:CGSizeMake(0, -1)];

Getting CGSize for NSString?

I am trying to get a CGSize for an NSString but I am having issues. Here is the code I am using:
CGFloat actualMessageFontSize;
CGSize messageSize =
[message sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold"
size:14.0f]
minFontSize:14.0f
actualFontSize:&actualMessageFontSize
forWidth:(alertViewWidth - textIndent)
lineBreakMode:NSLineBreakByWordWrapping];
I NSLog the CGSize after this code and the height doesn't change, even if I put a massive NSString into it. Do you know why this might happen and or what I should try instead?
Thanks.
use this method and
-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
and use it like bellow... i use this for UILable see the code..
UILabel *lblAddress = [[UILabel alloc]init];
[lblAddress setFrame:CGRectMake(110, 31, 200, 31)];
lblAddress.text = #"ABDKFNKG KKGK jfnjgdf gdklfg fldgmfkgml f";
lblAddress.lineBreakMode = UILineBreakModeWordWrap;
lblAddress.numberOfLines = 0;///write this line for multiple line and set 0 or anything lese
lblAddress.font = [UIFont fontWithName:#"Helvetica" size:12];
lblAddress.frame = CGRectMake(lblAddress.frame.origin.x, lblAddress.frame.origin.y,
200,[self calculateHeightOfTextFromWidth:lblAddress.text :lblAddress.font :200 :UILineBreakModeWordWrap] );
lblAddress.textColor = [UIColor darkGrayColor];
[self.view addSubview:lblAddress];
You have change frame of UILabel or UITextView which of them might be using to change accordingly.
CGFloat actualMessageFontSize = 12.0;
CGSize messageSize = [message sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:14.0f] minFontSize
//set frame accordingly
yourLabel.frame = CGReactMake(yourLabel.frame.origin.x,yourLabel.frame.origin.y,messageSize.width,messageSize.wiheight,);

UIView with adaptable height according to content - Objective-c

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.

iphone - set label font

How can I set the font of button label to Century Gothic Bold of size 46.
I'm using following code:
[self.titleLabel setFont:[UIFont fontWithName:#"Century Gothic-Bold" size:46]];
Is my code correct or not?
UIButton *NameBtn=[UIButton buttonWithType:UIButtonTypeCustom];
NameBtn=[[UIButton alloc]init];
[NameBtn setBackgroundColor:[UIColor clearColor]];
[NameBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
NSString *textb=[[self.searchList objectAtIndex:indexPath.row] objectForKey:#"name"];
CGSize constraintb = CGSizeMake(310 ,10);
CGSize sizeb = [textb sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:constraintb lineBreakMode:UILineBreakModeWordWrap];
[NameBtn setTitle:textb forState:UIControlStateNormal];
NameBtn.font = [UIFont fontWithName:#"Helvetica-Bold" size: 12.0];
NameBtn.frame = CGRectMake(85, 12, MAX(sizeb.width ,3.0f),12);
[NameBtn addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
NameBtn.tag=indexPath.row;
[self.view addSubview:NameBtn];
I think iOS doesnt support your font. But there is a font called AppleGothic.
see this link
http://www.prepressure.com/fonts/basics/ios-4-fonts
I used yesterday this:
onlineButton.titleLabel.font = [UIFont boldSystemFontOfSize:onlineButton.titleLabel.font.pointSize+3];
So try this:
yourButton.titleLabel.font = [UIFont fontWithName:fontName size:size];