NSAttributedString change style to bold without changing pointSize?

nsattributedstring bold
change font size of attributed string swift
bold non bold text in a single uilabel
nsattributedstring color
nsattributedstring get font
nsattributedstring default font

I am digging into NSAttributedStrings on iOS. I have a model that is returning a persons first and last name as NSAttributesString. (I don't know if it is a good idea to deal with attributed strings in models!?) I want the first name to be printed regular where as the last name should be printed in bold. What I don't want is to set a text size. All I found so far is this:

- (NSAttributedString*)attributedName {
    NSMutableAttributedString* name = [[NSMutableAttributedString alloc] initWithString:self.name];
    [name setAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]} range:[self.name rangeOfString:self.lastname]];
    return name;

However, this will, of course, override the font size of the last name, which gives a very funny look in a UITableViewCell where the first name will be printed in the regular text size of the cell's label and the last name will be printed very small.

Is there any way to achieve what I want?

Thanks for your help!

Here's a Swift extension that makes text bold, while preserving the current font attributes (and text size).

public extension UILabel {

    /// Makes the text bold.
    public func makeBold() {
        //get the UILabel's fontDescriptor
        let desc = self.font.fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitBold)
        //Setting size to '0.0' will preserve the textSize
        self.font = UIFont(descriptor: desc, size: 0.0)


Multiple Font, Style for UILable Text using NSAttributed String., Twiiter Notification, Text includes both Bold & Normal font. Create multiple labels, assigning different font style & other property, alligning them together, A big NO. Intiate NSMutableAttributedString , with multiple style property & assign to systemFont(ofSize: 17)], defines the font to be default system font & font size with  NSAttributedString change style to bold without changing pointSize? (4) I am digging into NSAttributedStrings on iOS. I have a model that is returning a persons first and last name as NSAttributesString. (I don't know if it is a good idea to deal with attributed strings in models!?)

Use technique from this question:

UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
uint32_t existingTraitsWithNewTrait = UIFontDescriptorTraitBold;
    fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithNewTrait];
UIFont *updatedFont = [UIFont fontWithDescriptor:fontDescriptor size:0.0];
NSDictionary *attribs =  @{NSFontAttributeName : updatedFont};
[mutableAttrString setAttributes:attribs range:result.range];

Using NSAttributedString for Multiple Font Styles in a Single Label , You could do it with multiple labels where the single bold word is on its own, but You can change things like color, font, size, kerning, etc all on specific Finally​, we're setting the text on a UILabel to the new attributed string. 16 NSAttributedString change style to bold without changing pointSize? Oct 19 '12. 15 NSLocaleCountryCode returns nil Mar 4 '13.

Make the above call from the table cell code but pass in the desired font size by getting the font size from the cell's textLabel.

Formatting strings with NSAttributedString, But sometimes we want more – we want to be able to add formatting like bold or italics can be all sorts of things: numbers, colors, fonts, paragraph styles, and more. use NSMutableAttributedString , which is an attributed string that you can modify: When you preview that you'll see the font size get larger with each word​  For anyone experiencing the same issue, I've found a (somewhat limiting) solution. Trying to change from the system font to another one did not solve the problem, but hardcoding it like below made it work more satisfying. The only problem is that

If you just want to Bold the second word in a string label, i.e. for a name label while using the Default iOS System Fonts, i.e. heading, or title etc

let s = "\(client.givenName) \(client.surname)" as NSString

let myAttribute = [ NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleTitle1) ]
let myString = NSMutableAttributedString(string: "\(s)", attributes: myAttribute )

let myRange = s.rangeOfString(client.surname)
let desc = UIFont.preferredFontForTextStyle(UIFontTextStyleTitle1).fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitBold)
let new = UIFont(descriptor: desc, size: 0.0)

myString.addAttribute(NSFontAttributeName, value: new, range: myRange)

nameLabel.attributedText = myString

NSAttributedString by example – Hacking with Swift, Let's start with a trivial attributed string: one that holds a regular string with no If you want to adjust paragraph-level settings - text alignment, indents, line For example, this creates and applies a paragraph style to make our text user wants to make part of it bold – rather than create the whole thing from  By default, the point size will be relative to the size of the screen and zoom command will not change the size of the point. If zooming in and zooming out changes the point size then reset its size with respect to the current drawing area by using RE command which regenerates the drawing objects.

Have you tried setting the size to 0.0?

How to change font size of attributed string wi |Apple Developer , I have a UITextView whcih holds an attributed string. How do I change just the font size of all the text without changing other attributes like Bold  Hello, Why does changing the Normal (Style) font in a (Word 2010) document cause several other Styles to also change? When using Styles in Word, I generally first open the Styles panel (standalone

Scaling Fonts Automatically, To add support for Dynamic Type in your app, you use text styles. This setting tells the text control to adjust the text size based on the Dynamic Type Call scaledFont(for:) , passing in a reference to the custom font that's at a point size suitable for use with large . It has no effect on custom fonts set in Interface Builder. How do you easily present strings with bold text and links while also making the content easily editable? By “easily editable”, I mean just changing the string without having to change your source code. This is what I needed to do recently. I needed to present exactly the style of strings that you see, containing bold text and clickable links.

Set UIButton Font Programmatically in Swift, To set a different font on UIButton programmatically you will need to create a new instance of UIFont You can also use a simple system font style to make your button title bold. For example, we can change button font size, foreground color​, background color and make text bold all in one attributed string. new Forms9Patch.Label { HtmlText = "plain <i>Bold+Italic</i> plain"} would give you a label where the text has been formatted bold italic in the middle of the string. Also, as an aside, it allows you to use custom fonts that are embedded resources in your PCL project without any platform specific work.

Working with Fonts and Attributed Strings in WatchKit, These options consist of text style fonts, system fonts and custom fonts. This chapter, however, will introduce the concepts of text styles and system fonts Okay No thanks options are provided on this screen to adjust the font size and use bold text. Attributed strings are represented using the NSAttributedString and  I am dinamically getting an HTML string from a Wordpress API and parsing it into an Attributed String to show it in my app. Since the string has its own styles, it shows different fonts and sizes, something that is affecting our design choices.

  • I tried that, but a fresh and clean initialized UITableViewCell returns pointSize 0.0f for the textLabel (it might be that the font or even the textLabel is empty)! Any idea on how to fix this?
  • If you implement the tableView:willDisplayCellAtIndexPath: delegate method, the font should be set and you can update your font there.
  • Any chance you could share a version of this in Obj-C?