how to pass font family and fontsize dynamically to UILabel? - iphone

cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:8.0];
Is it possible to pass the name of the font family and size dynamically to label text of the cell in iPhone?
NSString *selectorString =#"Helvetica-Bold";
cell.textLabel.font = [UIFont fontNamesForFamilyName:#"%#",selectorString];

Try this its helps you.
UILabel *titlelbl=[[UILabel alloc] initWithFrame:CGRectMake(80, 0, 150, 40)];
titlelbl.font = [UIFont fontWithName:#"Helvetica-Bold" size:17];
titlelbl.textAlignment=UITextAlignmentCenter;
titlelbl.textColor=[UIColor blackColor];

you can set like this
float fontSize = 14.0f;
NSString *selectorString =#"Helvetica-Bold";
cell.textLabel.font = [UIFont fontWithName:[NSString stringWithFormat:#"%#",selectorString] size:fontSize];

#define FONT_HELVETICA_BOLD #"Helvetica-Bold"
#define FONT_SIZE 14.of
cell. textLabel.font=[UIFont fontWithName:FONT_HELVETICA_BOLD size:FONT_SIZE];

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

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

iPhone/Objective - C - How to display the whole text of a long string in a UILabel

I have a very long string which is something like that below...
[[sqrt(221)-10,-sqrt(221)-10],[11,11]],[[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*3,rootof([[-3439170900,-50638826700,864180632700,9670592794500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[1477010700,22974524100,-369910322100,-4442729593500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*3],[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*2+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*4,rootof([[-3439170900,-50638826700,864180632700,9670592794500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*2+rootof([[1477010700,22974524100,-369910322100,-4442729593500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*4]],[sqrt(sqrt(221)+15),sqrt(-sqrt(221)+15)]
How can i display the whole thing in a UILabel with more than one lines (0)?
Also, although there are no linebreaks ("\n") in the string, the result is displayed like below....
[[sqrt(221)-10,-sqrt(221)- 10],[11,11]],[[rootof([[474368400,2 37184200,- 125944810200,258530778000],[1, 0,- 472,884,42215]])/(sqrt(221)*78270
Any help?
PS I could upload an image but i am not allowed beacause i am a new user...
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
set the UILabel as a multiline
MyLbl.numberOfLines = xxx;
if you have not enough space for set the UIlabel then also set the textView As an not editable property:
Mytxt = [[UITextView alloc]initWithFrame:CGRectMake(xx.0, x.0, xx.0,xx.0)];
Mytxt.transform = transform;
Mytxt.adjustsFontSizeToFitWidth = YES;
Mytxt.textColor = [UIColor blackColor];
Mytxt.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[Mytxt setFont:[UIFont fontWithName:#"Arial" size:xx.0]];
Mytxt.backgroundColor = [UIColor clearColor];
Mytxt.delegate = self;
Mytxt.userInteractionEnabled=YES;
Mytxt.editable=NO;
i hope you get the answer.
You can use UITextView with auto size with non editable mode instead of UILabel (or)
CGSize labelSize = yourString sizeWithFont:cellFont
constrainedToSize:CGSizeMake(190.0f, MAXFLOAT); lineBreakMode:UILineBreakModeWordWrap];
This labelsize will dynamically define the label height you can assign to your label
try this code
NSString *yourString = #"[[sqrt(221)-10,-sqrt(221)-10],[11,11]],[[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])";
UILabel *lblNameDesc=[[UILabel alloc] initWithFrame:CGRectMake(55, 10, 250, 21)];
[lblNameDesc setNumberOfLines:0];
lblNameDesc.text=yourString;
[lblNameDesc setBackgroundColor:[UIColor clearColor]];
[lblNameDesc setTextColor:[UIColor colorWithRed:64.0/255.0 green:64.0/255.0 blue:64.0/255.0 alpha:1.0f]];
[self.view addSubview:lblNameDesc];
// [lblNameDesc release];
[lblNameDesc sizeToFit];
supoose your string is. make your fond and width what ever you want:-
NSString longString = #"dsjkghsdkjnfgvsdjgsl,v.........................";
CGSize maximumLabelSize = CGSizeMake(200,MAXFLOAT);
CGSize expectedLabelSize = [longString sizeWithFont:[UIFont fontWithName:#"Aial-BoldMT" size:15.0] constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, expectedLabelSize.height)];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 0;
myLabel.text = longString ;
may this will help you
Try This...
textLabel.lineBreakMode = YES;
textLabel.numberOfLines = 10; // or many that you need...
You want UILineBreakModeCharacterWrap if you want your text to be evenly distributed along several lines (so it doesn't try to break at work boundaries).

Problem when implementing 'labelFontSize' property

I have been trying to add this code into my project:
reasonLabel.font = [UIFont labelFontSize:15];
but I keep getting this warning:
Class method '+labelWithSize:' not found (return type defaults to 'id')
How would I fix this?
Thanks,
Seb
You can use with
[UIFont fontWithName:#"Arial" size:14]
or
[UIFont systemFontOfSize:14]
[UIFont labelFontSize] returns the standard system UILabel font size, it is not an initializer. You must use something like fontWithSize:(CGFloat)fontSize or systemFontOfSize:(CGFloat)fontSize
Use
reasonLabel.font = [UIFont systemFontOfSize: [UIFont labelFontSize]];
and if you want to create a smaller or bigger font, just add or subtract:
reasonLabel.font = [UIFont systemFontOfSize: [UIFont labelFontSize] + 2];
reasonLabel.font = [UIFont systemFontOfSize: [UIFont labelFontSize] - 1];
For bold fonts use this:
reasonLabel.font = [UIFont boldSystemFontOfSize: [UIFont labelFontSize]];

Style UIlabel text like the font in the Settings panel

How do you style a UILabel text like the text below (created using titleForFooterInSection)?
Use the below , you might need to alter the values of color and Size, Try with different-2 combination of both..
UILabel* label = [[UILabel alloc] init];
label.shadowColor = [UIColor grayColor];
label.shadowOffset = CGSizeMake(0,1);
See the UILabel Documentation.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:14];
label.shadowColor = [UIColor colorWithWhite:1 alpha:0.5];
label.shadowOffset = CGSizeMake(1,1);
label.textColor = [UIColor colorWithRed:(76.0f/255.0f) green:(86.0f/255.0f) blue:(108.0f/255.0f) alpha:1.0f];