UITextView formatting with fontname and bold - objective-c

In a UITextView is it possible to set the fontname and then set it to bold?
I know that font name and size can be set using
fontWithName:(NSString *)fontName size:(CGFloat)fontSize
But once this is done how can i make it bold?

The system font on the iPhone is a good choice for many purposes. You can easily select it in a regular, bold or italic style using built-in font class methods. For example
UIFont *mainTitleFont = [UIFont boldSystemFontOfSize:14.0];
UIFont *subTitleFont = [UIFont SystemFontOfSize:14.0];
UIFont *textFont = [UIFont italicSystemFontOfSize:12.0];
But what if you want a font using both bold and italic at the same time, or a different typeface altogether? In that case, you can use the “fontWithName” method as follows.
UIFont *altFont = [UIFont fontWithName:#"Courier-Bold" size:14.0];
Reference

Use the Bold variant of the font, e.g. #"Helvetica-Bold" as the fontName. If using the default system font is ok for you, you can also use boldSystemFontOfSize: method instead.

UIFont *f = [UIFont fontWithName:#"Verdana-Bold" size:12];
yourTextView.font = f;

If you want to give font with name then you can refer below link. It have all font which are provided by iOS with their font names.
iOS Fonts
Thanks.

Related

NSFont convert font to bold or italic

I'm using the following code to change a font to bold on iOS.
UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithName:font.fontName size:font.pointSize];
UIFontDescriptor *styleDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:[fontDescriptor symbolicTraits] | UIFontDescriptorTraitBold];
UIFont *boldFont = [UIFont fontWithDescriptor:styleDescriptor size:0];
I'm trying to do the equivalent thing on Mac OS X. I tried this but it's picking a bolder font than the iOS code.
NSFont *boldFont = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSFontBoldTrait];
I want the Mac code to behave exactly like the iOS code and pick the same bold font.

change the default font of UIFontTextStyleHeadline

While using Interface builder & iOS7 UIFontTextStyleHeadline
I set my headline label to UIFontTextStyleHeadline
(I guess it can be done also with the following code:)
myHeadlineTextLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
Q:
this works well, yet I would like to have the same functionality only using HelveticaNeue-Thin
just as an example:
titleCustomLabel.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:(**Dynamic font size as the user defined in his setting**)];
what would be an elegant way to do so?
UIFont has the fontSize property. You can make use of that.
titleCustomLabel.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline].pointSize];
(don't use fontSize at the end)

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

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