How can I change the font size of a uilabel without changing the font itself? - objective-c

I'm having an issue where I allow the user to select a font for a label in my project, but then when the user sets the size of that label (using a different button), the label resets to a default font. I'd like to be able to retain the font that the user had applied while still allowing the user to change the font size. Any help is appreciated, thanks!
Here's my code..
-(IBAction)setFont{
[userText setFont:[UIFont fontWithName:#"Arial-BoldMT" size:50.0]];
//I had to add a size when setting the font or else I got an error
}
-(IBAction)setFontSize{
[userText setFont:[UIFont systemFontOfSize:24]];
}

Just use the fontWithSize: method on the label's current font:
- (IBAction)setFontSize {
// Keep the same font but change its size to 24 points.
UIFont *font = userText.font;
userText.font = [font fontWithSize:24];
}

The function [UIFont systemFontOfSize:] will always return default system font. You can just make use of the same function that you call in setFont which is [UIFont fontWithName:size:].

Related

Change text parameters in UITextView

I am trying to display some text in a UITextView (no editable) and I am having some problems to change font size, color...
I am using xcode 5 with ios6 appearance. I tried to change parameters in nib file: arribute inspector> text view.
But all I try, seems to do nothing. Nothing changes, I see the text equal as if I didn't modify anything.
I don't write text inside uitextview, I just show it from a variable.
I had the same problems and had already an answer here
You need to set the text of your UITextView and after you can set the font and the color :
_myTextView.text = #"text";
[_myTextView setFont:[UIFont fontWithName:#"Helvetica Neue" size:18.0f]];
_myTextView.textColor = [UIColor whiteColor];
I had a similar problem before. It got fixed when I did the customization after selecting the entire default text.
Here is a sample screenshot :
Hope this helps !

only change font size (and not font name)

What I want to do is change the font size.
I know below statement will change the font size, but it changes the font name because we are using systemFontOfSize
[UIFont systemFontOfSize: 13.0];
I know alternate option is as mentioned below.
[myLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
but I don't want to use fontWithName as I am setting that in IB.
I don't want to play with font name as my app is multi-language and hence I don't want to play with font name.
Any idea how can I just change fontsize and don't change the fontname.
Too bad that the font metrics properties of UIFont are readonly. It'd be nice if they could be adjusted dynamically without having to do this below:
UIFont * fontFromLabel = myLabel.font;
// now we have the font from the label, let's make a new font
// with the same font name but a different size
if(fontFromLabel)
{
// 13, or whatever size you want
UIFont * newFontForLabel = [UIFont fontWithName: fontFromLabel.fontName size: 13.0f];
if(newFontForLabel)
{
[myLabel setFont: newFontForLabel];
}
}

Changing the font of the selected text in Objective-C

I'm trying to create a custom “Change font” NSPopupButton for a Mac App (not an iOS App). I can detect a change in font selection:
long fontItemIndex = [fontPopup indexOfSelectedItem];
NSMenuItem *fontItem = [fontPopup itemAtIndex:(int)selectedFontItemIndex];
NSString *fontName = [selectedFontItem title];
Given this NSString of a font name, I cannot seem to find out how to actually change the selected text in my NSTextView textView to this new font.
I'm simply dazzled by the official documentation: it seems convertFont:toFamily: is what I need. When I do this:
NSFont *font = [NSFont fontWithName:fontName size:12.0];
[textView setFont:font];
It sets all text in the text view, not just the selected text. But when I do this:
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[fontManager convertFont:[fontManager selectedFont] toFamily:fontName];
it doesn't do a thing. What am I missing?
Inside a NSTextView is a NSTextStorage (a subclass of NSAttributedString) and you’ll have to modify the attribute named NSFontAttributeName.
First get the range where you want to change the font attribute:
NSRange selection = textView.selectedRange;
Now add the font attribute to the selection:
NSFont *font = [NSFont fontWithName:fontName size:12.0f];
[self.textView.textStorage addAttributes:#{NSFontAttributeName: font}
range:selection];
Depending on the contents of your NSPopUpButton it should be enough to call fontWithName:size: with title as the font name to get the just selected font. But if the method you already do doesn’t work, you’ll probably have to get a specific font from the font family name. availableMembersOfFontFamily: on NSFontManager will give you a list of all available fonts. You can use one of them to initialize a specific font.
Take a look at the setFont:range: method on NSText, the superclass of NSTextView.
(The ranges, of course, come from the selectedRanges property on NSTextView.)
This was all I needed to change all the text in my textview.
[textview setFont:[NSFont fontWithName:#"Courier" size:14]];

Setting size to custom iOS font doesnt work

I am using some custom fonts in my iphone app. They are displayed correctly, but i cant change their size.
Here is how set this font for UILabel:
myLabel.font=[UIFont fontWithName:#"Cabin-Regular" size:18];
This label is part of uitableview cell, if that could play any role.
Label settings in IB:
I change only color, text and font in code for this label.
What could be wrong?
Are you sure the font is loaded correctly?
You can try:
for (NSString *familyName in [UIFont familyNames]) {
NSLog(#"%#", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"\t%#",fontName);
}
}
This will show you all the fonts that are loaded. Make sure the same name "Cabin-Regular" is showing up exactly the same. A lot of times the font itself isn't loaded and then the font-size won't be used at all.
You need to add your font in your Xcode, if you are adding manually.
Add your font in your project files
Go to your Project name-Info.plist file, right click and click Add Row.
In your new row type Fonts provided by application. If you expand it you can add new items, in there add value to item 0 . The value should be your font file name. like below image
4. Double click on your font, it'll open in external editor, like following image
5.Give that name into your fontWithName: method. If you are setting value to your tableviewcell. the code will be
cell.nameLbl.font = [UIFont fontWithName:#"Cabin" size:18.0];
It works fine for me. I hope it helps :)

Cocoa Touch - Changing tex attributes?

How can I change a textview font and font size..
I've tried this but it doesnt seem to change anything...
typingText.font = [UIFont fontWithName:#"Marker Felt Thin" size:64.0f];
Thanks!
The actual name of the font is "MarkerFelt-Thin".
typingText.font = [UIFont fontWithName:#"MarkerFelt-Thin" size:64.0f];