HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocoa – 使用CoreText与变音符号绑定的字符框大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用各种技术在MacOS X / iOS中获取角色的边界框.我现在正在展示我的所有尝试.到目前为止,如果我想获得像“Ä”这样的变音字符的大小,代码就会失败.

使用CTFontGetBoundingRectsForGlyphs

-(void) resizeLayer1:(CATextLayer *)l toString:(NSString *)String
{
    // need to set CGFont explicitly to convert font property to a CGFontRef
    CGFontRef layerFont = CGFontCreateWithFontName((CFStringRef)@"Helvetica");
    l.font = layerFont;

    String = l.String;
    NSUInteger len = [String length];

    // get characters from NSString
    UniChar *characters = (UniChar *)malloc(sizeof(UniChar)*len);
    CFStringGetCharacters((__bridge CFStringRef)l.String,CFRangeMake(0,[l.String length]),characters);

    // Get CTFontRef from CGFontRef
    CTFontRef coreTextFont = CTFontCreateWithGraphicsFont(layerFont,l.fontSize,NULL,null);

    // allocate glyphs and bounding Box arrays for holding the result
    // assuming that each character is only one glyph,which is wrong
    CGGlyph *glyphs = (CGGlyph *)malloc(sizeof(CGGlyph)*len);
    CTFontGetGlyphsForCharacters(coreTextFont,characters,glyphs,len);

    // get bounding Boxes for glyphs
    CGRect *bb = (CGRect *)malloc(sizeof(CGRect)*len);
    CTFontGetBoundingRectsForGlyphs(coreTextFont,kCTFontDefaultOrientation,bb,len);
    CFRelease(coreTextFont);

    l.position = CGPointMake(200.f,100.f);

    l.bounds = bb[0];

    l.BACkgroundColor = CGColorCreateGenericRGB(0.f,.5f,.9f,1.f);

    free(characters);
    free(glyphs);
    free(bb);
}

结果它有点工作,但有一些填充正在进行,因为字形由CATextLayer呈现

使用CTFramesetterSuggestFrameSizeWithConsTraints

-(void) resizeLayer2:(CATextLayer *)l toString:(NSString *)String
{
    // need to set CGFont explicitly to convert font property to a CGFontRef
    CGFontRef layerFont = CGFontCreateWithFontName((CFStringRef)@"Helvetica");
    l.font = layerFont;

    String = l.String;

    NSUInteger len = [String length];

    // get characters from NSString
    UniChar *characters = (UniChar *)malloc(sizeof(UniChar)*len);
    CFStringGetCharacters((__bridge CFStringRef)l.String,null);

    CFMutableAttributedStringRef attrStr = CFAttributedStringCreateMutable(kcfAllocatorDefault,0);
    CFAttributedStringreplaceString(attrStr,0),(__bridge CFStringRef)String);
    CTTextAlignment alignment = kCTJustifiedTextAlignment;
    CTParagraphStyleSetTing _setTings[] = { {kCTParagraphStyleSpecifierAlignment,sizeof(alignment),&alignment} };
    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(_setTings,sizeof(_setTings) / sizeof(_setTings[0]));
    CFAttributedStringSetAttribute(attrStr,CFAttributedStringGetLength(attrStr)),kCTParagraphStyleAttributename,paragraphStylE);
    CFAttributedStringSetAttribute(attrStr,kCTFontAttributename,coreTextFont);
    CFRelease(paragraphStylE);

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrStr);
    CFRelease(attrStr);

    CFRange range;
    CGFloat maxWidth  = CGFLOAT_MAX;
    CGFloat maxHeight = 10000.f;
    CGSize consTraint = CGSizeMake(maxWidth,maxHeight);

    //  checking frame sizes
    CGSize coreTextSize = CTFramesetterSuggestFrameSizeWithConsTraints(framesetter,len),nil,consTraint,&rangE); 

    // allocate glyphs and bounding Box arrays for holding the result
    // assuming that each character is only one glyph,len);
    CFRelease(coreTextFont);

    bb[0].origin = l.bounds.origin;
    coreTextSize.width = ceilf(coreTextSize.width);
    coreTextSize.height = ceilf(coreTextSize.height);
    bb[0].size = coreTextSize;


    l.position = CGPointMake(200.f,100.f);


    // after setTing the bounds the layer gets transparent
    l.bounds = bb[0];
    l.opaque = YES;
    return;    

    l.BACkgroundColor = CGColorCreateGenericRGB(0.f,1.f);

    free(characters);
    free(glyphs);
    free(bb);
}

结果字符串的边界框是正确的.变音符号存在问题:Ä缺少其点,因为边界框不够高.

使用CTLineGetTypographicBounds

-(void) resizeLayer3:(CATextLayer *)l toString:(NSString *)String
{
    // need to set CGFont explicitly to convert font property to a CGFontRef
    CGFontRef layerFont = CGFontCreateWithFontName((CFStringRef)@"Helvetica");
    l.font = layerFont;

    String = l.String;


    NSUInteger len = [String length];

    // get characters from NSString
    UniChar *characters = (UniChar *)malloc(sizeof(UniChar)*len);
    CFStringGetCharacters((__bridge CFStringRef)l.String,(__bridge CFStringRef)String);
    CFAttributedStringSetAttribute(attrStr,coreTextFont);

    CTLineRef line = CTLineCreateWithAttributedString(attrStr);
    CGFloat ascent;
    CGFloat descent;
    CGFloat width = CTLineGetTypographicBounds(line,&ascent,&descent,null);
    CGFloat height = ascent+descent;
    CGSize coreTextSize = CGSizeMake(width,height);


    // get bounding Boxes for glyphs
    CGRect *bb = (CGRect *)malloc(sizeof(CGRect)*len);
    CFRelease(coreTextFont);

    bb[0].origin = CGPointZero;
    coreTextSize.width = ceilf(coreTextSize.width);
    coreTextSize.height = ceilf(coreTextSize.height);
    bb[0].size = coreTextSize;


    l.position = CGPointMake(200.f,1.f);

    free(characters);
    free(bb);
}

结果字符串的边界框是正确的.变音符号存在问题:Ä缺少其点,因为边界框不够高.

使用CTFontGetBoundingRectsForGlyphs

-(void) resizeLayer4:(CATextLayer *)l toString:(NSString *)String
{
    // need to set CGFont explicitly to convert font property to a CGFontRef
    CGFontRef layerFont = CGFontCreateWithFontName((CFStringRef)@"Helvetica");
    l.font = layerFont;

    String = l.String;

    NSUInteger len = [String length];

    // get characters from NSString
    UniChar *characters = (UniChar *)malloc(sizeof(UniChar)*len);
    CFStringGetCharacters((__bridge CFStringRef)l.String,len);

    CGPathRef glyPHPath = CTFontCreatePathForGlyph(coreTextFont,glyphs[1],null);
    CGRect rect = CGPathGetBoundingBox(glyPHPath);

    // get bounding Boxes for glyphs
    CGRect *bb = (CGRect *)malloc(sizeof(CGRect)*len);
    CTFontGetBoundingRectsForGlyphs(coreTextFont,100.f);

    l.bounds = rect;

    l.BACkgroundColor = CGColorCreateGenericRGB(0.f,1.f);

    free(characters);
    free(glyphs);
    free(bb);
}

结果字符串的边界框是正确的.由于变音符号本身是一个字形,而字符是另一个字形意味着Ä由两个字形组成,因此变音符号存在问题.怎么可以使用它?

还有其他可能性我忽略了并值得尝试吗?

编辑

CTLineGetTypographicBounds的第三个选项似乎有效.然而,我正在运行一个不同的问题,即CATextLayer中的第一行缺少其变音符号.第一行不知何故不够高.这意味着我在这里咆哮错误的树.

@H_404_32@

解决方法

在第一个示例中,您似乎忽略了这样一个事实,即字形的边界矩形很可能是负y源.返回的rect通常将y = 0视为文本的基线.因此,您可以在bounds rect中设置偏移量,这也可能是图层在文本中具有偏移量的原因. (没试过但是这么认为)

如果您对特定文本的边界不感兴趣,但选择包含各种文本的高度,您可能还想使用CTFontGetBoundingBox.

@H_404_32@ @H_404_32@
本图文内容来源于网友网络收集整理提供,作为学习参使用,版权属于原作者。

大佬总结

以上是大佬教程为你收集整理的cocoa – 使用CoreText与变音符号绑定的字符框全部内容,希望文章能够帮你解决cocoa – 使用CoreText与变音符号绑定的字符框所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。