custom font with UILabel in navigation bar [duplicate] - iphone

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Custom font in iPhone
I'm trying to create a custom label to set it as a titleView of my navigationItem, but I'm not able to set the label font to a custom font included im my project.
I have a custom font file called "BADER_AL_GORDABIA-2_0.ttf" and the real font name as shown in FontBook is "bader_al gordabia-2"
I have followed these steps:
checked the deployment target of my app, and it is set to iOS 5.1
added the font file to project resources, and verified that is is added to the build target in the "Copy Bundle Resources" in Build Phases.
added a new key in my info.plist file as "Fonts provided by application", and added the "BADER_AL_GORDABIA-2_0.ttf" inside it.
used the real font name "bader_al gordabia-2" in fontWithName method.
I was able to load the font using
UIFont * customFont = [UIFont fontWithName:#"bader_al gordabia-2" size:18.0f];
and I made sure that customFont is not null (by printing its description), but the problem is I can't apply this custom font to a UILabel.
my code is:
UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,10,200,24)];
titleLabel.backgroundColor = [UIColor clearColor];
UIFont * customFont = [UIFont fontWithName:#"bader_al gordabia-2" size:18.0f];
[titleLabel setFont:customFont];
titleLabel.text = #"أخبار الجمعية";
//NSLog(#"%#", [customFont description]);
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
[self.navigationItem setTitleView:titleLabel];
I have tried to search stack Overflow and I wasn't able to find a suitable answer.
I have tried also to use a custom label as specified in the following link:
http://refactr.com/blog/2012/09/ios-tips-custom-fonts/

This is how i am doing it,
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 100)];
navLabel.backgroundColor = [UIColor clearColor];
navLabel.text = #"Set Up";
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0];
navLabel.shadowOffset = CGSizeMake(0, -2);
navLabel.font = [UIFont fontWithName:#"Questrial" size:28.0];
navLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navLabel;
[navLabel release];
Only above code is not working ... So you need to go to your app plist file and add a new row and add this key to the row,
Fonts provided by application
and add the font name to the value of item0,
According to my code it is, "Questrial-Regular.ttf"
Also another thing,
That is you may not know the font name correctly. So you need to find the correct font name. Add this code to your ViewdidLoad and it will log the family names to the terminal.
NSLog(#"%#",[UIFont familyNames]);
And you can find correct font name according to your font.
Follow this steps, it worked for me and i think it will help you too.

Here's what you should do - First add and call the following method anywhere:
+(void)logAllFonts{
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog(#"%#: %#", fontFamily, fontNames);
}
}
This will log all the fontNames present in your project including the external fonts that you have added both in the project and the plist.
Now you go through that list in the console and find the exact font name for the font you added (bader_al gordabia-2). Use this name in fontWithName:
Hope this helps

Related

UILabel Multiple Line in iOS6

I have to show multiple line in UILabel (If text is large). Below is my code. I am using separate properties for different iOS versions. Please help me out..
labelLocation.numberOfLines=2;
labelLocation.font=[UIFont systemFontOfSize:25];
if ([[[UIDevice currentDevice]systemVersion]floatValue]>=6) {
labelLocation.lineBreakMode=NSLineBreakByTruncatingTail;
labelLocation.minimumScaleFactor=10.0/[UIFont labelFontSize];
}else{
labelLocation.lineBreakMode=UILineBreakModeTailTruncation;
labelLocation.minimumFontSize=10;
}
labelLocation.text=#"Can we make UILabeltext in 2 lines if name is large";
these two line together works
labelLocation.numberOfLines=0;
labelLocation.lineBreakMode = NSLineBreakByWordWrapping;
you can set
yourlabelname.lineBreakMode = NSLineBreakByWordWrapping;
yourlabelname.numberOfLines = give how many lines you want for your label(e.g.2,3,etc...)
and check if your outlet is set properly.
Try this labelLocation.numberOfLines=0;
I suppose, that your label has to small height. Two lines in systemFontOfSize 25 need height about 60.
If label is to small, system doesn't wrap line.
change ur code to this
labelLocation.numberOfLines=0;
labelLocation.font=[UIFont systemFontOfSize:40];
labelLocation.lineBreakMode=NSLineBreakModeWordWrap;
labelLocation.text=#"Can we make UILabeltext in 2 lines if name is large";
I would personally recommend you to calculate the height required to show the text and then show it onto the label...never hard code text display components such as UITextView and UILable.
NSString *str = #"This is to test the lable for the auto increment of height. This is only a test. The real data is something different.";
`UIFont * myFont = [UIFont fontWithName:#"Arial" size:12];//specify your font details here
//then calculate the required height for the above text.
CGSize lableSiZE = [str sizeWithFont:myFont constrainedToSize:CGSizeMake(240, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
//initialize your label based on the height you got from the above..you can put whatever width you prefer...
UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, lableSiZE.width, lableSiZE.height)];
myLable.text = str;
myLable.numberOfLines=0;
myLable.font=[UIFont fontWithName:#"Arial" size:12];
//myLable.backgroundColor=[UIColor redColor];
myLable.lineBreakMode = NSLineBreakByWordWrapping;

How to create a UILabel or UITextView with bold and normal text in it?

I am trying to create a UILabel or UITextView with bold and normal text inside.
I have gone through the attributedstring but when I am setting this in label of my custom cell it doesn't display any text.
I have also used the UITextView setContentToHTMLString: method, but it is undocumented and app get rejected.
Can anyone give some sort of solution to this?
Use "NSAttributedString" to set multiple font text in a single label & use CATextLayer to render it:
just #import "NSAttributedString+Attributes.h"
and then implement it like this:
NSString *string1 = #"Hi";
NSString *string2 = #"How are you ?";
NSMutableAttributedString *attr1 = [NSMutableAttributedString attributedStringWithString:string1];
[attr1 setFont:[UIFont systemFontOfSize:20]];
NSMutableAttributedString *attr2 = [NSMutableAttributedString attributedStringWithString:string2]
[attr2 setFont:[UIFont boldSystemFontOfSize:20]];
[attr1 appendAttributedString:attr2]
CATextLayer *textLayer = [CATextLayer layer];
layer.string = attr1;
layer.contentsScale = [[UIScreen mainScreen] scale];
(Your_text_label).layer = textLayer;
OR (if you want to render on a view directly)
[(Your_View_Name).layer addSublayer:textLayer];
Up until iOS 6.0, you couldn't do this with a normal UILabel or UITextView, but you can use NSAttributedString objects with a few possible open source solutions.
Like TTAttributedLabel or OHAttributedLabel.
One solution built into the iOS SDK, you could also use a CATextLayer which has a string property that can be set to a NSAttributedString.
And, like the commenters below say, yes you can do this with the "attributedText" property. Horray! (for Apple listening to developer's very often repeated feature requests)
I know this is an old thread, but this is something I just discovered myself. At least in Xcode version 4.6.3 this is possible by using an attributed textView. What's even better is that it's possible to all be done in Interface Builder!
Here are the steps:
Place your textView at the desired location
Select the textView and open up the Attributes tab under the Utilities panel
Change the textView text to "attributed"
Enter your desired text
Now, highlight whatever text you want bolded, underlined, etc.
Click on the "T" button next to the fontName
In the popup, select your desired typeface (ex: Bold)
You should see the desired typeface displayed in the Utilities panel
Enjoy!
The following code is for iOS 6.0 and above. The result is that the text "This is bold" will be in bold and "This is not bold." will be normal text.
if ([self.registrationLabel respondsToSelector:#selector(setAttributedText:)])
{
// iOS6 and above : Use NSAttributedStrings
const CGFloat fontSize = 17;
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
//UIColor *foregroundColor = [UIColor clearColor];
// Create the attributes
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
boldFont, NSFontAttributeName, nil];
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
regularFont, NSFontAttributeName, nil];
const NSRange range = NSMakeRange(0,12); // range of " 2012/10/14 ". Ideally this should not be hardcoded
// Create the attributed string (text + attributes)
NSString *text = #"This is bold and this is not bold.;
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:text
attributes:subAttrs];
[attributedText setAttributes:attrs range:range];
// Set it in our UILabel and we are done!
[self.registrationLabel setAttributedText:attributedText];
}
If you have an issue with editing the attributed text in the inspector, copy/paste the text into a rich text editor, adjust it, switch the textView Text options to Attributed and paste. Vwala.

How to use custom font files with space in file names

i have to add custom font file .Name of file if #"sample font.ttf".I do following steps.
Drop the file (sample font.ttf) into your project.  Open up your Info.plist file, create a key called UIAppFonts and make it an array. Add the filename of the font as a value
But it not working ..I thought the reason may by space in filename.when i get this file it was a compressed with filename #"sample_font.ttf".when i decompress it ,it removes #"_" and get "sample font.ttf"
Then i install it in font book .the name in window is first word file that is "sample"
I try with various way
UIFont *font = [UIFont fontWithName:#"sample font" size:14];
UIFont *font = [UIFont fontWithName:#"sample" size:14];
UIFont *font = [UIFont fontWithName:#"samplefont" size:14];
UIFont *font = [UIFont fontWithName:#"samplefont-Bold" size:14];
but not working. what exact font name should have to given. ios is 5.0 .Plz help me.
Just double click the font file and install it and then it will open Font book
go to Preview menu->>Show font info
There you can see the name of the font and use that name in UIFont *font = [UIFont fontWithName:#"nameAsSeenInShowFonts" size:14];
Note: The font file name and font name can be different. So in your .plist you use font file name and in your code you use font name
if you try in Ios5 I think you forgot to include your font file in TestApp target membershipI
Except all these, you can use an alternet option for custom fonts. I have implemented this concept jst before some days.
First download this. This is FontLabel. Drop it in your project.
Note that If u want to use fonts for label than only this will help u. If so, than u can use FontLabel object instead of ur label object with same behavior.
For exa.
FontLabel *label;
label = [[FontLabel alloc] initWithFrame:CGRectMake(75, 100, 104, 54) fontName:#"Script MT Bold" pointSize:30.0f];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = nil;
[label sizeToFit];
label.opaque = NO;
You can treat label object same as ur UILabel object. FontLabel is subclass of UILabel..

iphone : How to draw line on Label?

I want to make my label as shown in the image
I know I can get this effect by putting image view on it.
but is there any other method to do ?
How can I put line on label ?
Try this,
UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 271, 26)];
blabel.text = #"Hellooooooo";
blabel.textAlignment = UITextAlignmentCenter;
blabel.backgroundColor = [UIColor clearColor];
blabel.textColor = [UIColor blackColor];
blabel.font = [UIFont systemFontOfSize:14];
[scrollDemo addSubview:blabel];
//underline code
CGSize expectedLabelSize = [#"Hellooooooo" sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake((blabel.frame.size.width - expectedLabelSize.width)/2, expectedLabelSize.height + (blabel.frame.size.height - expectedLabelSize.height)/2, expectedLabelSize.width, 1);
viewUnderline.backgroundColor=[UIColor blackColor];
[scrollDemo addSubview:viewUnderline];
[viewUnderline release];
The line above will appear below the text. You just need to change Y for UIView and it'll do wonders :)
put another label with "_" over it
transparent background.
you can create UIView with line's height and width and give background color to it. Put UIView over your UILabel .
For one of my projects I've created an UILabel subclass, which supports multiline text, underline, strikeout, underline/strikeout line offset, different text alignment and different font sizes.
Please see provided link for more info and usage example.
https://github.com/GuntisTreulands/UnderLineLabel
Place a UIImageView with line image on your label so when you run application it will fit.

How can I set the font size of UITextView?

I have to set the font size and font family of a dynamically created textView, so that it gives same appearance as the one generated using Interface builder. Any idea how to do this?
UITextView *textView
...
[textView setFont:[UIFont fontWithName:#"ArialMT" size:16]]
You try this
[textView setFont:[UIFont systemFontOfSize:15]];
or
[textView setFont:[UIFont boldSystemFontOfSize:15]];
or you want to give fontname and size then you try this
[textView setFont:[UIFont fontWithName:#"arial" size:16]]
There is a property called font in UITextView,
#property(nonatomic, retain) UIFont *font
You can do like this:
yourTextView.font = builderTextView.font
may be I am too late but wanted to post my answer as its most relevant with current SDK.
I have used following line of code and work like charm in iOS 8.0
Swift Version:
self.textView.font = [UIFont systemFontOfSize:fontSize];
where fontSize is CGFloat Value.
In IB, select the text area and use the inspector to set the editability (it’s next to the text color). To set the font, you have to bring up the font window (command-T). It may help to have some text in the view when you change the font and size.
textView.font = UIFont(name: "Times New Roman", size: 20)
This worked for me (Swift 4 using in Swift Playgrounds on Xcode).