1

I am building my first WatchKit App and am having troubles with NSAttributedString formatting as it does not seem to work as I'd expect ;)

This is my code:

UIFont *textFont = [UIFont fontWithName:@"Menlo" size:30];
UIFont *hlFont = [UIFont fontWithName:@"Menlo-Bold" size:30];

NSMutableAttributedString *information = [[NSMutableAttributedString alloc]initWithString:@"ADDED AN ENTRY OF " attributes:@{NSFontAttributeName : textFont}];
NSString *amountString = [NSString stringWithFormat:@"%.2f",(-1)*handler.amount.floatValue];
NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
NSAttributedString *amount = [[NSAttributedString alloc]initWithString:amountString attributes:@{NSFontAttributeName : hlFont, NSUnderlineStyleAttributeName : underline }];

NSAttributedString *to = [[NSMutableAttributedString alloc]initWithString:@" TO " attributes:@{NSFontAttributeName : textFont}];

NSString *categoryString = handler.category;
NSAttributedString *category = [[NSAttributedString alloc]initWithString:categoryString attributes:@{NSFontAttributeName : hlFont, NSUnderlineStyleAttributeName : underline }];

[information appendAttributedString:amount];
[information appendAttributedString:to];
[information appendAttributedString:category];

[_informationLabel setAttributedText:information];

and this the result:

enter image description here

Expectation

10.00 and Stuff should be underlined and in boldface.

Is there something fundamentally different to how attributed strings work on the watch than on iOS? What am I missing?

read through this: https://developer.apple.com/library/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/TextandLabels.html

4

2 回答 2

2

Solved it, the problem were the fonts @"Menlo".

By using

UIFont *textFont = [UIFont systemFontOfSize:20];
UIFont *hlFont = [UIFont systemFontOfSize:20];

the formatting with underlines works fine.

于 2015-04-23T09:16:59.763 回答
-1

I don't believe you can programmatically set a label to be bold, italic, underlined....this has to be done through the actual interface in the storyboard.

According to this link

The attributes you can configure are

  1. text
  2. text color
  3. font
  4. min scale
  5. alignment
  6. lines

a potential workaround is to incorporate multiple labels, and set the ones you need to the right format (bold, underlined)

于 2015-04-22T16:37:27.597 回答