Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 带表情符号的属性字符串-正确高度_Ios_Nsstring_Uilabel_Nsattributedstring_Emoji - Fatal编程技术网

Ios 带表情符号的属性字符串-正确高度

Ios 带表情符号的属性字符串-正确高度,ios,nsstring,uilabel,nsattributedstring,emoji,Ios,Nsstring,Uilabel,Nsattributedstring,Emoji,我正在尝试获取属性字符串的高度。这在以下情况下工作正常: [attrString boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine) context:NULL] …除非字符串包含表情符号。使用emojis时,字符串在标

我正在尝试获取属性字符串的高度。这在以下情况下工作正常:

[attrString boundingRectWithSize:size
                         options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine)
                         context:NULL]

…除非字符串包含表情符号。使用emojis时,字符串在标签的底部被截断,标签的大小与此相同。有什么特别的事我需要做吗?

我知道这是一篇老文章,但有人可能仍然觉得它很有用,你可以转到核心文本,让它为你做艰苦的工作

static inline CGSize OKASizeWithAttributedString(NSAttributedString *attributedString, CGFloat width) {

  CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributedString);
  CGSize targetSize = CGSizeMake(width, CGFLOAT_MAX);
  CGSize size = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, (CFIndex)[attributedString length]), NULL, targetSize, NULL);
  CFRelease(framesetter);

  return size;
}

你能解决这个问题吗?我有完全相同的问题。如果您取消NSStringDrawingUserFontLeading选项,它可能工作正常,但我想知道NSStringDrawingUserFontLeading究竟是如何计算的height@Oliver阿特金森:我用过你的代码。它工作得很好,但是如果文本中有很多空格,它就不能正常工作。@bittu你用的是什么字体?有了系统支持的字体就可以了,我在使用自定义字体时通常会遇到问题。我在系统和自定义字体中都尝试了这段代码(Avenir next)font.issue同时出现。还有一件事..它对两种字体的小文本都有效,但如果文本太大,则不起作用。我在swift中尝试过相同的方法,但CTFramesetterSuggestFrameSizeWithConstraints()方法返回零大小,@OliverAtkinson可以告诉您它是swift版本。
+(CGFloat)heightStringWithEmojis:(NSString*)str fontType:(UIFont *)uiFont ForWidth:(CGFloat)width {

    // Get text
    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef) str );
    CFIndex stringLength = CFStringGetLength((CFStringRef) attrString);

    // Change font
    CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef) uiFont.fontName, uiFont.pointSize, NULL);
    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, stringLength), kCTFontAttributeName, ctFont);

    // Calc the size
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
    CFRange fitRange;
    CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(width, CGFLOAT_MAX), &fitRange);

    CFRelease(ctFont);
    CFRelease(framesetter);
    CFRelease(attrString);

    return frameSize.height;

}