UILabel font size? - objective-c

I can't seem to modify the font size of a UILabel with the following code:
itemTitle.font = [UIFont systemFontOfSize:25];
As i increase the number 25 to something greater, it seems to only add a top margin to the label, which consequently pushes the text down so much, so that the text gets cut off at the bottom or completely overflows.
i have another UILabel elsewhere with systemFontOfSize 25, and it's much smaller than the itemTitle text. What's going on? Isn't 25 supposed to be an absolute value?
i am so confused on how to programmatically change font size of uilabels.

I have modified the UILabel by following code:
label.font=[label.font fontWithSize:25];
Try this and see whether is it working in your case or not???

Check that your labels aren't set to automatically resize. In IB, it's called "Autoshrink" and is right beside the font setting. Programmatically, it's called adjustsFontSizeToFitWidth.

[label setFont:[UIFont systemFontOfSize:9]];
this works for me.

For Swift 3.1, Swift 4 and Swift 5, if you only want change the font size for a label :
let myLabel : UILabel = ...
myLabel.font = myLabel.font.withSize(25)

**You can set font size by these properties **
timedisplayLabel= [[UILabel alloc]initWithFrame:CGRectMake(70, 194, 180, 60)];
[timedisplayLabel setTextAlignment:NSTextAlignmentLeft];
[timedisplayLabel setBackgroundColor:[UIColor clearColor]];
[timedisplayLabel setAdjustsFontSizeToFitWidth:YES];
[timedisplayLabel setTextColor:[UIColor blackColor]];
[timedisplayLabel setUserInteractionEnabled:NO];
[timedisplayLabel setFont:[UIFont fontWithName:#"digital-7" size:60]];
timedisplayLabel.layer.shadowColor =[[UIColor whiteColor ]CGColor ];
timedisplayLabel.layer.shadowOffset=(CGSizeMake(0, 0));
timedisplayLabel.layer.shadowOpacity=1;
timedisplayLabel.layer.shadowRadius=3.0;
timedisplayLabel.layer.masksToBounds=NO;
timedisplayLabel.shadowColor=[UIColor darkGrayColor];
timedisplayLabel.shadowOffset=CGSizeMake(0, 2);

This worked for me in
Swift 3
label.font = label.font.fontWithSize(40.0)
Swift 4
label.font = label.font.withSize(40.0)

very simple, yet effective method to adjust the size of label text progmatically :-
label.font=[UIFont fontWithName:#"Chalkduster" size:36];
:-)

This worked for me:
sequencerPlayLabel.font = [UIFont fontWithName:kTypeFont size:kTypeFontSize];
-rich

Answers above helped greatly.
Here is the Swift version.
#IBOutlet weak var priceLabel: UILabel!
*.... lines of code later*
self.priceLabel.font = self.priceLabel.font.fontWithSize(22)

In C# These ways you can Solve the problem, In UIkit these methods are available.
Label.Font = Label.Font.WithSize(5.0f);
Or
Label.Font = UIFont.FromName("Copperplate", 10.0f);
Or
Label.Font = UIFont.WithSize(5.0f);

Try to change your label frame size height and width so your text not cut.
[label setframe:CGRect(x,y,widht,height)];

Related

UITextField – opacity of left placeholder

I have a UITextField with opacity 0.2 (the background is red), and I tried to add a white icon as textField.leftView like so:
self.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"lock.png"]];
self.leftView.layer.opacity = 1.0;
self.layer.opacity = 0.2;
self.leftViewMode = UITextFieldViewModeAlways;
The problem is that the leftView seems to get the same opacity as the whole text field, and as they both are white, the result is that I see nothing. Do you see any other way/what I do wrong? Any help is appreciated.
Okay, found the solution. Seems I didn't think it fully through.
The problem was the opacity being "inherited" by all colors of the view, while I only wanted the background to be opaque. So, I just made the background opaque :) Like so:
self.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2];

UIButton Title only displays half of it

I've a UIButton that has a custom FONT for the titleLabel attribute.
For some reason on iOS 6.0 it shows only half of the title. I tried increasing the height of the title. but that didn't work.
What am i missing?
Is this my only option?
btn.titleLabel.font = [UIFont fontWithName:#"Frutiger95-UltraBlack" size:17];
This is how it should look (minus the color change)
Single line labels have a low content compression resistance priority on the vertical axis. So when you increase the font size, they don't increase the height of their intrinsicContentSize. Setting the compressionResistancePriority to UILayoutPriorityDefaultHigh or UILayoutPriorityRequired should fix it.
[btn.titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired
forAxis:UILayoutConstraintAxisVertical];
I think updating the label is enough, but you may need to increase the priority on the button itself as well.
[btn setContentCompressionResistancePriority:UILayoutPriorityRequired
forAxis:UILayoutConstraintAxisVertical];
I think you need to set button title nil, then make your own custom UILabel
and add that label on your button like my example given below-
UILabel *lblloginbtntitle=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, btnLogin.frame.size.width, btnLogin.frame.size.height)];
lblloginbtntitle.text=NSLocalizedString(#"Login", nil);
lblloginbtntitle.textAlignment=NSTextAlignmentCenter;
lblloginbtntitle.textColor=[UIColor whiteColor];
lblloginbtntitle.font=[UIFont fontWithName:#"AvenirNextLTPro-Regular" size:20];
[btnLogin addSubview:lblloginbtntitle];
[btnLogin setTitle:#"" forState:UIControlStateNormal]

Color of custom font in navgation controller (setTextColor): gray instead of white

I am trying to set custom font and color (white) for font in my app's navigation bar title. Instead of white I get gray text color.
Here is the code I'm using to set custom font:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
label.textAlignment = UITextAlignmentCenter;
label.adjustsFontSizeToFitWidth = NO;
label.minimumFontSize = 10.0;
[label setFont:[UIFont fontWithName:#"HelveticaNeueLTCom-MdCn" size:24.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:#"View title"];
[self.navigationItem setTitleView:label];
Same thing happened when I tried to set custom font color in UITableView sections.
I know about some problems with Helvetica Neue in iPhone apps development but the color of this font works well in other parts of my projects.
Am I missing something here?
Have you tried other .ttf fonts just to test if they work? I wonder if there is something wrong with the font file itself. try this directory and see if it works.
The problem was that I've (deliberately) overriden default font for the whole application as described in this answer.
The solution was to remove this override (all the #import's and the remove .m file from the target, or just delete both of the class files.

UIButton with two lines of text in the title (numberOfLines=2)

I'm trying to make a UIButton that has two lines of text in its titleLabel. This is the code I'm using:
UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
[titleButton setTitle:#"This text is very long and should get truncated at the end of the second line" forState:UIControlStateNormal];
titleButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleButton.titleLabel.numberOfLines = 2;
[self addSubview:titleButton];
When I try this, the text only appears on one line. It seems the only way to achieve more than one line of text in UIButton.titleLabel is to set numberOfLines=0 and use UILineBreakModeWordWrap. But this doesn't guarantee the text to be exactly two lines.
Using a plain UILabel, however, does work:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = #"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:titleLabel];
Does anyone know how to make the UIButton work with two lines? Is the only solution to create a separate UILabel to hold the text, and add it as a subview of the button?
You don't need to add a UILabel to the UIButton. That's just extra objects and work.
Set these properties on the titleLabel of your button.
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2;//if you want unlimited number of lines put 0
Swift:
button.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
button.titleLabel!.numberOfLines = 2//if you want unlimited number of lines put 0
Updated answer for more recent iOS versions
Since this is the accepted answer, added #Sean's answer here:
Set these properties on the titleLabel of your button.
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2; // if you want unlimited number of lines put 0
Swift 3 and 4:
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.numberOfLines = 2 // if you want unlimited number of lines put 0
Original answer for an older version of iOS
If you want 2 lines of text on top of your UIButton you should add a UIlabel on top of it that does precisely that.
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = #"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[myButton addSubview:titleLabel]; //add label to button instead.
Updated for interface builder solution
Added #Borut Tomazin's answer for a more complete answer.
Updated this part again since the answer of #Borut Tomazin was improved.
You can do this much easier, with no code required. In Interface Builder set Line Break on UIButton to Word Wrap. Than you can insert multiple lines of title. Just hit Option + Return keys to make new line. You will also need to add this to the User Defined Runtime Attribute in Interface Builder:
titleLabel.textAlignment Number [1]
You can do this much easier, with no code required. In Interface Builder set Line Break on UIButton to Word Wrap. Than you can insert multiple lines of title. Just hit Option + Return keys to make new line.
You will also need to add this to the User Defined Runtime Attribute in Interface Builder:
titleLabel.textAlignment Number [1]
It's that simple. Hope it helps...
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button setTitle: #"Line1\nLine2" forState: UIControlStateNormal];
To avoid completely the need to edit code, and thus the need to subclass your view, in Xcode5 and greater you can follow Borut Tomazin suggestion:
In Interface Builder (or storyboard) set Line Break to
Word Wrap. Than you can insert multiple lines of title. Just hit
Option + Return keys to make new line.
and then, in the User Defined Runtime Attributes you can add
Key path: titleLabel.textAlignment
Type: Number
Value: 1
Note: this may be not completely "future proof" since we are translating the UITextAlignmentCenter constant into its numerical value (and that constant may change as new iOS versions are released), but it seems safe in the near future.
You can modify the needed value directly from Storyboard.
Select the button, go to the identity inspector and add the following key-value pair in the "User defined runtime attributes" section:

Change the Font of label "Comic-Sans-Ms"

i think this is really basic question but i try many way to change the font of UILabel
i try this code
lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 100)];
[lbl setText:#"Demo Text"];
[lbl setFont:[UIFont fontWithName:#"Comic-Sans-Ms" size:20]];
but it's not work can you please tell me why i am not getting "Comic Sans Ms" font in my label. with this code other font are working but i don't know what is problem with "Comic-Sans-MS"
Thanks..
Use custom fonts.
I take it this is iOS? It would then be because Comic Sans is not installed on iOS.