iphone - set label font - iphone

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];

Related

how to set the scroll view contentsize dynamically?

I had an application in which i am setting the content size of the scroll view dynamically according to the number of buttons which i am creating from an array.like this `
for(int i =0;i<[sarray count];i++)
{
NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
NSString *head=[dicttable objectForKey:#"cat"];
btn= [UIButton buttonWithType:UIButtonTypeCustom];
int j=i+1;
btn.frame = CGRectMake((j-1)*87,0,87, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:head forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[Scroller addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
if(btn.tag==0)
{
sendActionsForControlEvents:UIControlEventTouchUpInside];
}
else
{
btn.selected=NO;
}
}
}
[Scroller setContentSize:CGSizeMake([sarray count]*85, 44)];
`
but here the problem is after the last button also the scroll view scrolls .it need not to be happend,i need the scrolling ends with last button on the view,can anybody help me on this
set scrollview bounces to NO
scrollView.bounces = NO;
yes as you said it looses the smoothness. But no other choice. If you found any sol please update the answer.
hey i am sending you complete example to make button dynamicay and seting the content size scrollview on number of count ,i had just given u one example code u have to set the size of frame of button as u requried
UIScrollView *scroll_View=[[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:scroll_View];
int y=0;
for(int i =1;i<10;i++)
{
// NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
// NSString *head=[dicttable objectForKey:#"cat"];
UIButton* btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(y,89,100, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
NSString *str=[NSString stringWithFormat:#"%d",i];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:str forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[scroll_View addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
y=110*i;
}
[scroll_View setContentSize:CGSizeMake(y, 400)];
enjoy coding
You just declare one yMargin variable of type CGFloat and initialize it with some required value as 0. Now every time whenever you are adding a new subview vertically do y+=lbl.frame.size.height;
Now setContentSize:CGSizeMake([sarray count]*85, y margin+someRequiredMargin)];
(someRequiredMargin - you want to add below )

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];

UILabel's numberofLines issue

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];

setting UIButton font

I have the following code for setting up text in a UIButton
[self.fullListButton_ setTitle:[NSString stringWithFormat:#"%d", [self.newsfeedItem_.newsfeedToSubjects count] - 3] forState:UIControlStateNormal];
The issue is I can't seem to set the font for this. How do I do so?
In the newer version of iOS:
UIButton * myButton;
myButton.titleLabel.font = [UIFont systemFontOfSize: 12];
Or any other font mechanism.
Look here for the UIButton portions:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instp/UIButton/titleLabel
And here for the UIFont stuff:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIFont_Class/Reference/Reference.html
In your code:
self.fullListButton_.titleLabel.font = [UIFont systemFontOfSize: 12];
or
self.fullListButton_.titleLabel.font = [UIFont fontWithName:#"Arial" size:12];

Change the Font of UINavigationBar title

i am having this code to show the button as the title of the navbar,and then in button code i write the code to change the font of the unbutton text label.but i didn't get the correct font version with the below code.
UIButton *titleButton = [[UIButton alloc]init];
titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[titleButton setFrame:CGRectMake(0, 0, 170, 35)];
[titleButton setTitle:[NSString stringWithFormat:#"%# %#",delegatee.selectedBook,delegatee.selectedChapter]forState:UIControlStateNormal];
titleButton.titleLabel.font =[UIFont fontWithName:#"Georgia" size:10.0f];
[titleButton addTarget:self action:#selector(ClickbtnChaperselection:)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;
iput georgia but i didn't get the correct font
Thanks.
Try this :
UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[titleButton setFrame:CGRectMake(0, 0, 170, 35)];
[titleButton setTitle:[NSString stringWithFormat:#"%# %#",delegatee.selectedBook,delegatee.selectedChapter]forState:UIControlStateNormal];
titleButton.titleLabel.font =[UIFont fontWithName:#"Georgia" size:10.0f];
[titleButton addTarget:self action:#selector(ClickbtnChaperselection:)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;
try this
for button :- use in Navigation Title view , this is work IOS 4 and IOS 5 ,I use .....
UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[titleButton setFrame:CGRectMake(0, 0, 170, 35)];
[titleButton setTitle:#"dee"forState:UIControlStateNormal];
UIFont *font=[UIFont fontWithName:#"A_Nefel_Sereke" size:21];
titleButton.titleLabel.font =font;
titleButton.titleLabel.textColor = [UIColor redColor];
[titleButton addTarget:self action:#selector(ClickbtnChaperselection:)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;
I think you have problem in your font name ... Please check
Why don't you just put a label instead of a button as the title ?
Here's code for a title label:
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView)
{
titleView = [[UILabel alloc] initWithFrame:CGRectZero];
[titleView setBackgroundColor:[UIColor clearColor]];
[titleView setFont:[UIFont fontWithName:#"Georgia" size:10.0f]];
[self.navigationItem setTitleView:titleView];
[titleView release];
}
titleView.text = title;
If you need it to be UIButton, it shouldn't be too hard to adjust the code to your needs.
Good luck !