0

我建立我的第一个WatchKit应用程序时遇到的麻烦与NSAttributedString格式,因为它似乎不工作,我会期望;)WatchKit,AttributedString格式不工作

这是我的代码:

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]; 

和这个结果:

enter image description here

期望

10.00Stuff应以下划线和粗体显示。

与iOS上的属性字符串如何工作有什么根本不同?我错过了什么?

通过这个阅读:https://developer.apple.com/library/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/TextandLabels.html

回答

1

解决了这个问题,这个问题是字体@"Menlo"

通过使用

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

格式化用下划线工作正常。

+0

*将在24小时内接受 –

+0

不知道为什么我们无法在苹果手表中使用字体“Menlo”或其他字体? –

0

我不相信你可以编程设置标签要大胆,斜体,下划线....这必须通过在故事板实际的接口来完成。

根据this link

的属性可以配置是

  1. 文本
  2. 文本颜色
  3. 字体
  4. 分钟规模
  5. 对准
  6. 线

潜在的解决方法是将多个标签,并设置您需要正确的格式的那些(粗体,下划线)

+0

Nsattributedstring确实如此:格式化标签中字符串的各个部分。 –