How to Calculate the Height of an Nsattributedstring with Given Width in iOS 6

How to calculate the height of an NSAttributedString with given width in iOS 6

Option 2 does work in iOS with the proper parameters.

NSAttributedString *attrStr = ... // your attributed string
CGFloat width = 300; // whatever your desired width is
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];

Without the proper values for the options parameter you will get the wrong height.

It is also required that attrStr contains a font attribute. Without a font, there is no way to properly calculate the size.

How to get height for NSAttributedString at a fixed width

The answer is to use

- (void)drawWithRect:(NSRect)rect options:(NSStringDrawingOptions)options
but the rect you pass in should have 0.0 in the dimension you want to be unlimited (which, er, makes perfect sense). Example here.

Calculate Height Of NSAttributedString Containing HTML

My solution did work, The problem was that I was incorrectly setting the size of scrollview, fixing the height of scroll view fixed my problem.

let size = CGSize(width: view.frame.width, height: 2000)
let boundingBox = contentView.attributedText.boundingRect(
with: size,
options: [.usesLineFragmentOrigin, .usesFontLeading, .usesDeviceMetrics],
context: nil
)
print(boundingBox.height)

NSAttributedString height limited by width and numberOfLines

Solution appears to be simplest. There are no need to get line origins and typographic bound. CTFramesetterSuggestFrameSizeWithConstraints with text specific text range will do all work

- (CGSize)fittingSizeWithSize:(CGSize)size numberOfLines:(NSInteger)numberOfLines {
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self);

if (!framesetter) {
return CGSizeZero;
}

if (numberOfLines == 0) {
CGSize textSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, size, NULL);

if (framesetter != NULL) {
CFRelease(framesetter);
}

return CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
} else {
CGPathRef path = CGPathCreateWithRect(CGRectMake(0, 0, size.width, CGFLOAT_MAX), NULL);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, self.length), path, NULL);
if (path != NULL) {
CFRelease(path);
}

NSArray *lines = (NSArray *)CTFrameGetLines(frame);
__block CFIndex len = 0;

[lines enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (numberOfLines > 0 && idx == numberOfLines) {
*stop = YES;
return;
}

CTLineRef line = (__bridge CTLineRef)obj;
CFRange range = CTLineGetStringRange(line);

len += range.length;
}];

CFRange strRange = CFRangeMake(0, len);
CGSize textSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, strRange, NULL, size, NULL);

if (framesetter != NULL) {
CFRelease(framesetter);
}

return CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
}
}

How to calculate height of nsattributed string with line spacing dynamically

You can just use UILabel's sizeThatFits. For example:

    let text = "This is\nSome\nGreat\nText"

let contentHeight = contentLabel.text! == "" ? 0 : contentLabel.getHeightWidthGivenWidthAndLineHeight(6, labelWidth: labelWidth)
//returns 73.2

But just setting

    contentLabel.attributedText = contentLabel.attributedString //attributedString is same as getHeightWidthGivenWidthAndLineHeight

let size = contentLabel.sizeThatFits(contentLabel.frame.size)
//returns (w 49.5,h 99.5)

Code for attributedString added to your extension, if you need to see that:

var attributedString:NSAttributedString?{
if let text = self.text{
let attributeString = NSMutableAttributedString(string: text)
let style = NSMutableParagraphStyle()

style.lineSpacing = 6
attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(0, text.characters.count))
return attributeString
}
return nil
}


Related Topics



Leave a reply



Submit