Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When the convinient methods from apple dont work, this category provides a good aproximation in most cases.</p> <pre><code>@implementation NSAttributedString (PixLib) - (CGFloat)heightForWidth:(CGFloat)width { CGMutablePathRef path = CGPathCreateMutable(); CGPathAddRect(path, NULL, CGRectMake(0, 0, width, 99999)); CGFloat h = [self heightForPath:path]; CGPathRelease(path); return h; } - (CGFloat)heightForPath:(CGPathRef)path { CGFloat height = 0; CTFrameRef frame = [self cfframeForPath:path]; if (frame != NULL) { NSArray* lines = (__bridge NSArray*)CTFrameGetLines(frame); int l = [lines count]; if (l &gt; 1) { CGPoint origins[l]; CTFrameGetLineOrigins(frame, CFRangeMake(0, l), origins); CGFloat yFirst = origins[0].y; CGFloat yLast = origins[l-1].y; CGFloat ascent, descent, leading; CTLineGetTypographicBounds((__bridge CTLineRef)[lines objectAtIndex:l-1], &amp;ascent, &amp;descent, &amp;leading); height = ceilf((ascent+descent+leading)*1.3) + yFirst-yLast; } else { if (l==1) { CGFloat ascent, descent, leading; CTLineGetTypographicBounds((__bridge CTLineRef)[lines objectAtIndex:0], &amp;ascent, &amp;descent, &amp;leading); height = ceilf(ascent+descent+leading)*1.3; } } CFRelease(frame); } return height; } - (CTFrameRef)cfframeForPath:(CGPathRef)p { // hack to avoid bugs width different behavior in iOS &lt;4.3 and &gt;4.3 CGMutablePathRef path = CGPathCreateMutable(); CGRect r = CGPathGetBoundingBox(p); CGAffineTransform t = CGAffineTransformIdentity; t = CGAffineTransformTranslate(t, r.origin.x, r.origin.y); t = CGAffineTransformScale(t, 1, -1); t = CGAffineTransformTranslate(t, r.origin.x, - ( r.origin.y + r.size.height )); CGPathAddPath(path, &amp;t, p); CGPathMoveToPoint(path, NULL, 0, 0); CGPathCloseSubpath(path); // hack end CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self); CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); CFRelease(framesetter); CGPathRelease(path); return frame; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload