0

How would I go about setting different font properties for the degree symbol in this string?

https://i.stack.imgur.com/GaDu6.png

I am currently setting the value like this:

currentTemp.text = [NSString stringWithFormat:@"%d°", [result[@"main"][@"temp"] intValue]];

And setting my font this way:

currentTemp.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:80];

Thanks for your help.

warrantsuspect
  • 221
  • 4
  • 10

1 Answers1

0

Check out NSAttributedString class: https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSAttributedString_Class/Reference/Reference.html

This is exactly what you need.

NSMutableAttributedString *temp = [[NSMutableAttributedString alloc] initWithString:@"20 C"];
[temp addAttribute:NSFontAttributeName
              value:[UIFont systemFontOfSize:20.0]
              range:NSMakeRange(0, 2)];
[temp addAttribute:NSFontAttributeName
              value:[UIFont systemFontOfSize:18.0]
              range:NSMakeRange(3, 1)];
sha
  • 17,824
  • 5
  • 63
  • 98
  • Your answer seems incomplete. Did you forget the `°` sign? – Zev Eisenberg May 29 '14 at 02:28
  • Really? :) I'm providing a way to learn and experience new things, not the copy-paste answer to simple task. – sha May 29 '14 at 02:32
  • I appreciate the sentiment, but some people may learn better by modifying a working example than by trying to make an incomplete example work. In this case, it seems weird that the answer is so close to solving the problem, and is just off by a couple of characters. Weird enough that I thought you had made a typo. – Zev Eisenberg May 29 '14 at 02:37
  • Any reason why systemFontofSize works but when I try to do [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:80] it fails – warrantsuspect May 29 '14 at 03:13
  • How does it fail exactly? Should not be any difference – sha May 29 '14 at 03:17
  • ` NSMutableAttributedString *temp = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d°", [result[@"main"][@"temp"] intValue]]]; [temp addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:40.0] range:NSMakeRange(0, 2)]; [temp addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:90.0] range:NSMakeRange(3, 0)]; currentTemp.attributedText = temp;` – warrantsuspect May 29 '14 at 03:24
  • That gives me weird results. My main text still looks larger and the other symbol is not reacting to font size 90 – warrantsuspect May 29 '14 at 03:25
  • Second range should be 2,1 not 3,0 I think. Range with length 0 means you're not doing anything. Range is empty. – sha May 29 '14 at 03:27
  • Great. I'm dumb. How can I adjust placement for the character? – warrantsuspect May 29 '14 at 03:31
  • I'm not sure you can do this. You would need to split the text into different pieces if you need that level of control. – sha May 29 '14 at 03:34