【问题标题】:Strange behaviour of UILabel.attributedText combined with Emoji fontUILabel.attributedText 与 Emoji 字体相结合的奇怪行为
【发布时间】:2014-11-06 14:53:26
【问题描述】:

我发现 UILabel 的一些奇怪行为带有属性文本和表情符号图标。不知道如何解决。

这是我每次在文本字段上更改文本时都在执行的代码。

NSLog(@"bef %@", self.textView.attributedText);
self.textView.attributedText = [[NSMutableAttributedString alloc] initWithString:self.textView.text];
NSLog(@"after %@", self.textView.attributedText);

输出是:

bef qwe{
    NSFont = "<UICTFont: 0x7fc88b0e23d0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSOriginalFont = "<UICTFont: 0x7fc88b0e23d0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}????{
    NSFont = "<UICTFont: 0x7fc88a4d6aa0> font-family: \"Apple Color Emoji\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSOriginalFont = "<UICTFont: 0x7fc88b0e23d0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}1{
    NSFont = "<UICTFont: 0x7fc88b0e23d0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}
2014-11-06 15:49:04.256 Inbot Dev[31162:506070] after qwe{
    NSFont = "<UICTFont: 0x7fc88b0e23d0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSOriginalFont = "<UICTFont: 0x7fc88b0e23d0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}????1{
    NSFont = "<UICTFont: 0x7fc88a4d6aa0> font-family: \"Apple Color Emoji\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSOriginalFont = "<UICTFont: 0x7fc88b0e23d0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}

实际问题是 Emoji 后的“1”进入 Emoji 块,但它不应该。 我正在对 NSMutableAttributedString 进行一些自定义装饰,这只是我缩小范围的一个问题示例。

更有趣的是,只有数字才会发生,如果我在 Emoji 之后加上字母就可以了...

似乎在设置属性文本时,它正在为 Emoji 做一些额外的事情。以前有人遇到过这个问题吗?

【问题讨论】:

    标签: ios objective-c uilabel nsattributedstring emoji


    【解决方案1】:

    实际上问题是通过在创建 NSMutableAttributedString 时为整个文本设置默认字体来解决的。

    似乎只有在使用 Helvetica 后备时才会出现问题

    我正在做的是:

    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.alignment = style == kDecorateStringStyleDark ? NSTextAlignmentLeft : NSTextAlignmentRight;
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:sourceString attributes:@{ NSParagraphStyleAttributeName : paragraphStyle }];
    //this part below fixed the problem (by default it was using Helvetica)
    [attString addAttribute:NSFontAttributeName value:FONT(FONT_MEDIUM, 14.0f) range:NSMakeRange(0, sourceString.length)];
    //after that you can decorate parts of text as you want to
    

    【讨论】:

    • 你能举个例子说明你的意思是“设置默认字体”吗?我有同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2021-07-02
    • 1970-01-01
    相关资源
    最近更新 更多