【问题标题】:How to detect emoji and change font size如何检测表情符号并更改字体大小
【发布时间】:2016-12-31 09:00:52
【问题描述】:

我的文本中包含表情符号,我们可以通过对字符串进行编码和解码来正确显示它,我需要实现的是增加文本中只有表情符号的字体大小,如下图所示,

我有一个想法来确定所有表情符号的范围,并提供NSAttributedString 并增加字体大小。现在我不知道如何检测给定字符串中的表情符号范围?

谢谢

【问题讨论】:

  • 您可以更改字符串中一组表情符号的属性。

标签: ios objective-c nsattributedstring emoji


【解决方案1】:

你可以像下面这样直接使用它或

if ([myString containsString:@"?"]) 
   {
        NSLog(@"one");
        //change the font size here.
   }
else
   {
        NSLog(@"fk");
       //change the font size here.
   }

或者你可以使用

[mystring is isEqualToString:"I believe ?"];

试试这些。希望这对你有帮助。

【讨论】:

  • 可以有多个emoji,不只是这个,甚至可以有新的emoji,相信你没看懂我的问题。
  • 作为一个想法,将所有possible emoji 放入一个数组中,然后检查您的字符串表情符号是否包含。
  • 对不起,我错误地编辑了这个答案,而不是我的。请像以前一样改变它!
【解决方案2】:

我做了一个演示,你可以从下面的字符串中检测表情符号,

  NSString *str = @"this is ? and test ?";

NSArray *arr = [str componentsSeparatedByString:@" "];

for (int i = 0; i < arr.count; i++) {

NSString *temp = [arr objectAtIndex:i];

if ( ![temp canBeConvertedToEncoding:NSASCIIStringEncoding]) {

    NSLog(@"%d",i);
    NSLog(@"%@",temp);  // temp is emoji. You can detect emoji here from your string now you can manage as per your need


}


}

【讨论】:

  • 以上代码如何增加emoji的FONT SIZE?
  • 谢谢,canBeConvertedToEncoding 是什么,你能提供完整的代码吗?
  • 不客气...:) canBeConvertedToEncoding 将返回 YES 如果字符串可以转换为指定的编码(即 NSASCIIStringEncoding ),否则它将返回 NO。 S0,当 emoji 出现时,它会返回 NO。
【解决方案3】:

我也做过类似的事情

    let string = "This is emoji Test"
    let attributedEmoji = NSMutableAttributedString(string: " \u{1F600}", attributes: [NSFontAttributeName:UIFont.systemFontOfSize(60)])

    let attribString = NSMutableAttributedString.init(string: string)
    attribString.appendAttributedString(attributedEmoji)

    lblEmoji.attributedText = attribString

您可以更改字体和字体大小以缩放表情符号。

  1. 将所有可能的表情符号(您的应用程序使用)放入一个数组中。
  2. 从数组中将表情符号搜索到字符串中。如果找到,应用属性化表情符号。
  3. 编写一个接受表情符号代码并返回属性表情符号文本的方法。

希望这些信息能以更好的方式帮助您。

https://github.com/woxtu/NSString-RemoveEmoji

Find out if Character in String is emoji?

【讨论】:

  • \u{1F600} -- 这是表情符号 unicode 。你可以找到 emoji 的 unicode unicode.org/emoji/charts/full-emoji-list.html
  • 我知道该怎么做,看我的问题。问题是你怎么能检测到\u{1F600}
  • 检查我编辑的答案,下方的两个 URL 可以解决您的问题
【解决方案4】:

感谢所有回答的人,但没有一个是完整的答案,尽管@Raj 的建议看 NSString-RemoveEmoji 帮助我实现了这个解决方案,在这里,它适用于任何类型的表情符号

-(NSMutableAttributedString *)getAttributedEmojiString:(NSString *)inputString{

    NSMutableArray *__block emojiRange=[[NSMutableArray alloc] init];
    [inputString enumerateSubstringsInRange:NSMakeRange(0, [inputString length])
                                    options:NSStringEnumerationByComposedCharacterSequences
                                 usingBlock: ^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) {
             if([substring isEmoji]){
                 [emojiRange addObject:@{@"startrange":@(substringRange.location),@"endrange":@(enclosingRange.length)}];
             }
     }];

    NSMutableAttributedString *mutString=[[NSMutableAttributedString alloc] initWithString:inputString];


    [mutString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16.0] range:NSMakeRange(0, mutString.length)];

    [emojiRange enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mutString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:35.0] range:NSMakeRange([obj[@"startrange"] floatValue], [obj[@"endrange"] floatValue])];
    }];

    return mutString;
}

说明

  1. 首先通过NSString-RemoveEmoji函数isEmoji找到字符串中所有表情符号的NSRange,并存储到数组中。
  2. 提供获取的范围以将更大的FONT SIZE应用于范围内的字符。
  3. 最后将生成的属性文本赋给标签。

    self.label.attributedText=[self getAttributedEmojiString:EmojiDecoded(originalText)];
    

我使用两个宏来编码和解码表情符号,因为我需要将这些值保存到服务器并通过 api 读取,下面是宏。

#define Encoded(val) [[val dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0]
#define Decoded(val) [[NSString alloc] initWithData:[[NSData alloc] initWithBase64EncodedString:val options:0] encoding:NSUTF8StringEncoding]

#define EmojiEncoded(val) [[NSString alloc] initWithData:[val dataUsingEncoding:NSNonLossyASCIIStringEncoding] encoding:NSUTF8StringEncoding]
#define EmojiDecoded(val) [[NSString alloc] initWithData:[val dataUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding]

希望它可以帮助任何正在寻找类似解决方案的人。

干杯,谢谢大家。

【讨论】:

  • 你是如何转换找到表情符号的方法,请分享
  • @MridulGupta 我不记得确切但我没有转换任何东西,我在答案中使用 NSString+RemoveEmoji 类别来确定字符串是否为表情符号,如果是,则保存范围字符串和增加字体大小。我的回答很完整,没有多余的。
  • 是否有 isEmoji 方法的 Objective-C 实现?
  • @mengxiangjian 只有ObjectiveC,你可以在函数中定义而不是#define
【解决方案5】:

这有点晚了,但可能对偶然发现此答案的其他人有用。秘诀是询问Core Text,它知道NSAttributedString 中的哪些字符是表情符号字符。

// Build the attributed string as needed
let ns = NSAttributedString(string: s)

// Now create a typesetter and render the line
let typesetter = CTTypesetterCreateWithAttributedString(nsa)
let line = CTTypesetterCreateLine(typesetter, CFRangeMake(0, nsa.length))

// Once you have a line you can enumerate the runs
guard let runs = CTLineGetGlyphRuns(line) as? [CTRun] else {
    throw NSError(domain: "CoreText", code: -1, userInfo: nil)
}

// Each run will have a font with specific attributes
print("There are \(runs.count) run(s) in \(ns.string)")
print()
for run in runs {
    let charRange = CTRunGetStringRange(run)
    let x: NSAttributedString = CFAttributedStringCreateWithSubstring(nil, nsa, charRange)

    print("    Chars: '\(x.string)'")

    let attributes: NSDictionary = CTRunGetAttributes(run)
    let font = attributes["NSFont"] as! CTFont

    let traits = CTFontGetSymbolicTraits(font)
    print("    Emoji == \(traits.contains(.traitColorGlyphs))")

    print()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-19
    • 2017-05-27
    • 2018-08-29
    • 2010-09-17
    • 2013-09-27
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多