【问题标题】:WatchKit, AttributedString formatting not workingWatchKit,AttributedString 格式不起作用
【发布时间】: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.00Stuff 应加下划线并以粗体显示。

属性字符串在手表上的工作方式与在 iOS 上的工作方式有什么根本不同吗?我错过了什么?

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

【问题讨论】:

    标签: ios nsattributedstring watchkit apple-watch wkinterfacelabel


    【解决方案1】:

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

    通过使用

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

    带下划线的格式可以正常工作。

    【讨论】:

    • * 将在 24 小时内接受
    • 不知道为什么我们不能在Apple Watch中使用“Menlo”或其他字体?
    【解决方案2】:

    我不相信您可以以编程方式将标签设置为粗体、斜体、下划线......这必须通过情节提要中的实际界面来完成。

    根据this link

    你可以配置的属性是

    1. 文字
    2. 文字颜色
    3. 字体
    4. 分钟刻度
    5. 对齐

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

    【讨论】:

    • Nsattributedstring 正是这样做的:格式化标签中字符串的各个部分。
    猜你喜欢
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 2018-01-10
    • 1970-01-01
    相关资源
    最近更新 更多