19

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!

Michael Ochs
  • 2,846
  • 3
  • 27
  • 33

6 Answers6

7

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.withSymbolicTraits(.traitBold)
        //Setting size to '0.0' will preserve the textSize
        self.font = UIFont(descriptor: desc, size: 0.0)
    }

}
finebel
  • 2,227
  • 1
  • 9
  • 20
Sakiboy
  • 7,252
  • 7
  • 52
  • 69
7

You can bold a string without changing its other attributes by setting the StrokeWidth attribute with a negative value.

Objective-C:

[name setAttributes:@{NSStrokeWidthAttributeName : @-3.0} range:NSRangeFromString(name.string)];

Swift:

name.setAttributes([.strokeWidth: NSNumber(value: -3.0)], range: NSRangeFromString(name.string))
Yonat
  • 4,382
  • 2
  • 28
  • 37
  • But is this what Cocoa defines as "bold" when you chose "bold" on some text in TextEdit for instance? will it look the same? and what's does the -3.0 value mean? is it "smearing" the text horizontally over 3 points? – Motti Shneor Aug 15 '21 at 15:55
  • It looks the same to me… The text is sharp and not smeared - try and see for yourself. – Yonat Aug 15 '21 at 15:57
4

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];
Community
  • 1
  • 1
Roman B.
  • 3,598
  • 1
  • 25
  • 21
  • in MacOS, the preferredFontDescriptorWithTextStyle is only available in MacOS 11 and later - so we can't rely on this for apps that need support MacOS 10.13 and on. What is the alternative? those "system styles" are quite old now in the Interface-Builder --- how can one access them programmatically without the new APIs ? – Motti Shneor Aug 17 '21 at 07:55
2

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 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? – Michael Ochs Oct 21 '12 at 09:22
  • 3
    If you implement the `tableView:willDisplayCellAtIndexPath:` delegate method, the font should be set and you can update your font there. – rmaddy Oct 21 '12 at 15:30
2

Have you tried setting the size to 0.0?

NYC Tech Engineer
  • 1,845
  • 2
  • 22
  • 23
2

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
DogCoffee
  • 19,820
  • 10
  • 87
  • 120