【发布时间】:2015-04-22 08:47:31
【问题描述】:
我正在构建我的第一个 WatchKit 应用程序,但在使用 NSAttributedString 格式时遇到了问题,因为它似乎不像我预期的那样工作;)
这是我的代码:
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];
结果如下:
期待
10.00 和 Stuff 应加下划线并以粗体显示。
属性字符串在手表上的工作方式与在 iOS 上的工作方式有什么根本不同吗?我错过了什么?
【问题讨论】:
标签: ios nsattributedstring watchkit apple-watch wkinterfacelabel