2013-11-01 14 views
7

属性付き文字列の高さを取得しようとしています。これは期待通りに動作しています。emojisで属性付きの文字列 - 適切な高さ

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

...文字列にemojisが含まれていない場合。 emojisでは、文字列はこのサイズのラベルの一番下で切り取られます。私は何か特別なことがありますか?

+0

は、あなたがこの問題を解決できましたか?私はまったく同じ問題を抱えています。 – damirstuhec

答えて

3

私はこれが古い投稿だと知っていますが、誰かがまだそれが役に立つと思うかもしれません。あなたはコアテキストに落として、あなたのために頑張ってください!

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; 
} 
+0

これは私を助けました。ありがとう。 – user1105951

+0

@Oliver Atkinson:私はうまく働いていましたが、テキストにスペースがたくさんあると正しく動作しません。 – bittu

+0

@bittuどのフォントを使用していますか?システムでサポートされているフォントではOKですが、通常はカスタムフォントを使用するときに問題が発生しました –

0
+(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; 

} 
関連する問題