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)];
Related
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];
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];
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];
}
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..!
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.