我有包含表情符号的文本,我们可以通过编码和解码字符串来正确显示它,我需要实现的是仅增加表情符号的字体大小文字就像在下面的图片,如何检测表情符号和更改字体大小
我有一个想法,以确定所有的表情符号的范围,供应NSAttributedString
增加字体大小。现在我不知道如何检测给定字符串中的emojis范围?
感谢
我有包含表情符号的文本,我们可以通过编码和解码字符串来正确显示它,我需要实现的是仅增加表情符号的字体大小文字就像在下面的图片,如何检测表情符号和更改字体大小
我有一个想法,以确定所有的表情符号的范围,供应NSAttributedString
增加字体大小。现在我不知道如何检测给定字符串中的emojis范围?
感谢
感谢所有谁回答,但没有一个完整的答案,虽然@拉吉的建议,看看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;
}
说明
isEmoji
,并存储在数组中。最后将生成的属性文本分配给标签。
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]
希望它可以帮助的人谁是寻找类似的解决方案。
干杯,并感谢所有。
你是如何转换查找emojis方法,请分享 –
@MridulGupta我不记得确切但我没有转换任何东西,我用NSString + RemoveEmoji类我的答案是确定字符串是否是表情符号,如果是则保存字符串的范围并增加字体大小。我的答案是完整的,没有什么额外的。 – iphonic
你可以用它直接像下面或
if ([myString containsString:@""])
{
NSLog(@"one");
//change the font size here.
}
else
{
NSLog(@"fk");
//change the font size here.
}
,或者您可以使用
[mystring is isEqualToString:"I believe "];
尝试这些。希望这会对你有所帮助。
我已经做一个演示,你可以像下面的字符串检测的表情符号,
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
}
}
我也做了同样喜欢
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
您可以更改字体和字体大小缩放表情符号。
希望这个信息会帮助你更好的方式。
\ u {1F600} - 这是表情符unicode。你可以找到表情符号的http://unicode.org/emoji/charts/full-emoji-list.html –
我知道这个怎么做,看我的问题。问题是如何检测'\ u {1F600}'? – iphonic
检查我编辑的答案和两个URL在较低的一面是解决您的问题 –
您可以更改字符串中一组表情符的属性。 – holex