NSString boundingRectWithSize slightly underestimates the correct height - why?
source link: https://www.codesd.com/item/nsstring-boundingrectwithsize-slightly-underestimates-the-correct-height-why.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
NSString boundingRectWithSize slightly underestimates the correct height - why?
I'm attempting to resize a text field / view automatically depending on its current width. In other words I want the width to stay constant but resize the height according to the text supplied to it.
It seems to be working but for some reason is aligning to the bottom of my window despite efforts to manually move it back. Can anybody see what I'm doing wrong here?
NSString *temp = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel felis nec massa ultricies blandit non id arcu. Sed enim est, facilisis a feugiat in, consectetur eget arcu. Aenean rutrum, lacus id euismod congue, nisl erat vulputate lectus, non faucibus tortor purus sed sem. Donec tempor dui egestas velit auctor vitae faucibus diam facilisis. Morbi convallis nulla quis est pulvinar quis ultricies sem sollicitudin.";
myText.stringValue = temp;
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:12], NSFontAttributeName,
[NSParagraphStyle defaultParagraphStyle], NSParagraphStyleAttributeName,
nil];
NSSize size = NSMakeSize(window.frame.size.width, 200);
myText.frame = [temp boundingRectWithSize:size options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin attributes:attributes];
EDIT: Even when manually moving the frame it's clear that the text is still getting cut off. The newly adjusted size is not quite there.
this is in reference to: NSString boundingRectWithSize slightly underestimating the correct width - why? and NSTextView or NSTextField automatically resize bounds / frame?
I had the same Problem. I found the following in the Documentation:
To correctly draw and size multi-line text, pass NSStringDrawingUsesLineFragmentOrigin in the options parameter.
This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must raise its value to the nearest higher integer using the ceil function.
So this solved it for me:
CGRect rect = [myLabel.text boundingRectWithSize:CGSizeMake(myLabel.frame.size.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName: myLabel.font}
context:nil];
rect.size.width = ceil(rect.size.width);
rect.size.height = ceil(rect.size.height);
Update (Swift 3)
As of now, the method is deprecated. The Swift 3 way to do this would be:
let size = myLabel.text!.size(
attributes: [NSFontAttributeName: myLabel.font]
)
let rect = CGSize(
width: ceil(size.width),
height: ceil(size.height)
)
See this answer for an Objective-C example.
Recommend
-
92
Q:你知道如何用line-height使多行文字垂直居中么? - 掘金 Q:你知道如何用line-height使多行文字垂直居中么? 2017年11月29日 03:00 ...
-
51
The United States has temporarily eased trade restrictions on China's Huawei to minimize disruption for its customers, a move the founder of the world's largest telecoms equipment maker said meant little because it was already prepared for U.S....
-
5
Return NSString from a C function advertisements In iOS development, is it possible to use NSString and return it from a function...
-
2
NSString sizeWithFont: constrainedToSize: calculate a wrong height advertisements I have a Subtitle style UITableViewCell which height changes dyna...
-
7
Objective-C: Add an NSString pattern to NSException advertisements I am working on some API (Crittercism) to report handled exceptions in the clien...
-
4
I wish the same value after the duplicate conversion of NSString iOS advertisements This question already has an answer here:
-
3
How to define label.text with NSString as soon as the new view is loaded, the NSString value is defined in the class method advertisements I w...
-
2
iOS开发Extra系列:NSString*** 浏览:497次 出处信息 在iOS系统,NSString可能是最常用的对象,很多用法...
-
6
'NSString' may not respond to the '-hexIntValue' warning, the function works well advertisements I'm taking a h...
-
0
Use copy property for NSString Sep 10, 2012 一个简短例子来说明一下为什么 NSString @property 最好用 copy 而不是 retain: #import <Foundation...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK