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
Export Compliance in iOS App Submission
"Interfaceorientation" Is Deprecated in iOS 8, How to Change This Method Objective C
Calculating Tiles to Display in a Maprect When "Over-Zoomed" Beyond the Overlay Tile Set
Convert JSON String to JSON Object in Swift 4
Libmobilegestalt Mobilegestaltsupport.M:153 Mobilegestalt.C:550 Xcode Console
"'Cdvplugin.H' File Not Found" in Cordova as Component (Cleaver)
Touchesbegan, Touchesended, Touchesmoved for Moving Uiview
What Does It Mean to "Weak-Link" a Framework
Accessorybuttontappedforrowwithindexpath: Not Getting Called
Expanding and Collapsing Table View Cells in iOS
How to Add a Toolbar to the Bottom of a Uitableviewcontroller in Storyboards
Opengl Es 2.0 Object Picking on iOS
How to Convert an iPhone Osstatus Code to Something Useful
Modulename-Swift.H File Not Found in Xcode8
Is There Function to Convert Uicolor to Hue Saturation Brightness