Amdx Rux
Amdx Rux

Reputation: 105

NSMutableAttributedString with different fonts

I have a problem with the combination of two strings:

let finalMutableString = NSMutableAttributedString()
let attributedDot = NSAttributedString(string: " ●", attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 7)])
let firstPartString = NSAttributedString(string: "Sample text", attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 17)])
finalMutableString.append(attributedDot)
finalMutableString.append(firstPartString)
label.attributedText = finalMutableString

And whole text has font size 7.0 not only attributedDot. Why is this how it behaves? Text's should have different sizes

Upvotes: 5

Views: 1673

Answers (2)

yasin89
yasin89

Reputation: 173

There is a easy way for Objective-C

NSMutableAttributedString *yourAttributedString = [[NSMutableAttributedString alloc] initWithString:@"Your string text"];

[yourAttributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Proxima Nova" size:18.0f] range:NSMakeRange(0,16)];

range should be length of your string

Upvotes: 1

Yury Imashev
Yury Imashev

Reputation: 2128

I guess that your code work, but you think that it doesn't because font sizes look pretty much equally.

Here is what I see with your code

enter image description here

And that's what I see when I change size to 2 and 37

enter image description here

And that's your original sizes (7 and 17), but for both strings, I've set the same text.

enter image description here

Upvotes: 2

Related Questions