我发现了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";
}
实际问题是&#34; 1&#34;表情符号进入表情符号块后,但它不应该。 我在NSMutableAttributedString上做了一些自定义装饰,这只是我缩小问题的一个例子。
更有意思的是,只有在数字之后才会出现数字,如果我在表情符号之后放了字母......
似乎在设置attributionText时,它正在为表情符号做一些额外的事情。以前有人遇到过这个问题吗?
答案 0 :(得分:0)
实际上,在创建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