【问题标题】:Calculate Height from NSAttributedString with NSParagraphStyle使用 NSParagraphStyle 从 NSAttributedString 计算高度
【发布时间】:2013-06-25 03:45:15
【问题描述】:

我想用NSParagraphStyle 属性计算NSAttributedString 的高度。

我认为创建行间距较大的 UILabel 会很容易,但我无法为UITableViewCell 计算正确的高度。

我尝试用boundingRectWithSize:options: 计算它,但它根本不起作用……

【问题讨论】:

  • size 属性返回的值不是正确的吗?
  • 不,尺寸正在返回一个可笑的宽度。
  • 你在属性字典中设置字体了吗?
  • 这就是诀窍!天哪,为什么这甚至没有记录?
  • 如果您认为字体定义了字符的绘制方式,这很简单。

标签: ios objective-c height core-text nsattributedstring


【解决方案1】:

当苹果的便捷方法不起作用时,这个类别在大多数情况下提供了一个很好的近似值。

@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 > 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], &ascent, &descent, &leading);

            height = ceilf((ascent+descent+leading)*1.3) + yFirst-yLast;
        } else {
            if (l==1) {
                CGFloat ascent, descent, leading;
                CTLineGetTypographicBounds((__bridge CTLineRef)[lines objectAtIndex:0], &ascent, &descent, &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 <4.3 and >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, &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

【讨论】:

  • 它不适用于行距。我在 NSAttributedLabel 中添加了一个 NSParagrahStyle,但它不计算行距……
【解决方案2】:

我使用NSLayoutManager's usedRectForTextContainer: 与一个与 UITableView 断开连接的 TextView 堆栈。我回复了similar Stack Overflow question and explained how 来实现它。

【讨论】:

    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2021-07-03
    • 1970-01-01
    相关资源
    最近更新 更多