objective-c iOS 7 sizeWithAttributes:替换 sizeWithFont:constrainedToSize
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19145078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 7 sizeWithAttributes: replacement for sizeWithFont:constrainedToSize
提问by morcutt
How do you return a multiline text CGSize from the new iOS 7 method sizeWithAttributes?
如何从新的 iOS 7 方法 sizeWithAttributes 返回多行文本 CGSize?
I would like this to produce the same results as sizeWithFont:constrainedToSize.
我希望它产生与 sizeWithFont:constrainedToSize 相同的结果。
NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eu urna quis lacus imperdiet scelerisque a nec neque. Mauris eget feugiat augue, vitae porttitor mi. Curabitur vitae sollicitudin augue. Donec id sapien eros. Proin consequat tellus in vehicula sagittis. Morbi sed felis a nibh hendrerit hendrerit. Lorem ipsum dolor sit."
CGSize textSize = [text sizeWithAttributes:@{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:16.0] }];
This method only produces the height for a single line of text.
此方法仅生成单行文本的高度。
回答by elio.d
well you can try this :
你可以试试这个:
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14]};
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
CGRect rect = [textToMeasure boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
回答by Gnawbone
This is how I did it:
我是这样做的:
// Get a font to draw it in
UIFont *font = [UIFont boldSystemFontOfSize: 28];
CGRect textRect;
NSDictionary *attributes = @{NSFontAttributeName: font};
// How big is this string when drawn in this font?
textRect.size = [text sizeWithAttributes:attributes];
// Draw the string
[text drawInRect:textRect withAttributes:attributes];
回答by Jelly
Swift 2.3:
斯威夫特 2.3:
let attributes = [NSFontAttributeName:UIFont(name: "HelveticaNeue", size: 14)]
let rect = NSString(string: textToMeasure).boundingRectWithSize(
CGSizeMake(width, CGFLOAT_MAX),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: attributes, context: nil)
Swift 4:
斯威夫特 4:
let attributes = [NSFontAttributeName:UIFont(name: "HelveticaNeue", size: 14)]
let rect = NSString(string: textToMeasure).boundingRect(
with: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude),
options: NSStringDrawingOptions.usesLineFragmentOrigin,
attributes: attributes as [NSAttributedString.Key : Any], context: nil)
回答by Dan Rosenstark
Here's my method to deal with both situations, goes in an NSStringcategory.
这是我处理这两种情况的方法,属于一个NSString类别。
- (CGSize) sizeWithFontOrAttributes:(UIFont *) font {
if (IS_IOS7) {
NSDictionary *fontWithAttributes = @{NSFontAttributeName:font};
return [self sizeWithAttributes:fontWithAttributes];
} else {
return [self sizeWithFont:font];
}
}
回答by testing
For Xamarin.iOS:
对于 Xamarin.iOS:
UIFont descriptionLabelFont = UIFont.SystemFontOfSize (11);
NSString textToMeasure = (NSString)DescriptionLabel.Text;
CGRect labelRect = textToMeasure.GetBoundingRect (
new CGSize(this.Frame.Width, nfloat.MaxValue),
NSStringDrawingOptions.UsesLineFragmentOrigin,
new UIStringAttributes () { Font = descriptionLabelFont },
new NSStringDrawingContext ()
);
回答by Marijn
If you have the text, font, numberOfLines and width of your label set, this method returns the size of your label:
如果您设置了标签的文本、字体、行数和宽度,则此方法将返回标签的大小:
myLabel.numberOfLines = 0;
CGSize size = [myLabel sizeThatFits:CGSizeMake(myLabel.frame.size.width, CGFLOAT_MAX)];`
回答by mikeho
As an alternative, if you're looking at UITextView, you can always use the NSLayoutManagermethod:
作为替代方案,如果您正在查看UITextView,则始终可以使用该NSLayoutManager方法:
CGSize textSize = [textView.layoutManager usedRectForTextContainer:textView.textContainer].size;
You can also find the line height for a given font by:
您还可以通过以下方式找到给定字体的行高:
UIFont *font;
CGFloat lineHeight = font.lineHeight;
回答by jenish
CGSize stringsize = [lbl.text sizeWithAttributes:
@{NSFontAttributeName:[UIFont fontWithName:FontProximaNovaRegular size:12.0]}];
CGSize adjustedSize = CGSizeMake(ceilf(stringsize.width), ceilf(stringsize.height));
use ceilfmethod to manage properlly
使用ceilf方法妥善管理
回答by Matt
[As a new user, I cannot post a comment to @testing's answer, but to make his answer (for xamarin.ios) more useful]
[作为一个新用户,我不能对@testing 的回答发表评论,但为了让他的回答(对于 xamarin.ios)更有用]
We can return a CGRect and only use the height parameter for the gui item we are targeting UIButton etc.Passing in any parameters we need as below
我们可以返回一个 CGRect 并且只使用我们针对 UIButton 等的 gui 项目的高度参数。传递我们需要的任何参数,如下所示
public CGRect GetRectForString(String strMeasure, int fontSize, nfloat guiItemWidth)
{
UIFont descriptionLabelFont = UIFont.SystemFontOfSize (fontSize);
NSString textToMeasure = (NSString)strMeasure;
CGRect labelRect = textToMeasure.GetBoundingRect (
new CGSize(guiItemWidth, nfloat.MaxValue),
NSStringDrawingOptions.UsesLineFragmentOrigin,
new UIStringAttributes () { Font = descriptionLabelFont },
new NSStringDrawingContext ()
);
return labelRect;
}
header_Revision.Frame = new CGRect (5
, verticalAdjust
, View.Frame.Width-10
, GetRectForString( header_Revision.Title(UIControlState.Normal)
, 18
, View.Frame.Width-10 ).Height
);

