iOS 7 sizeWithAttributes:replacesizeWithFont:constrainedToSize

如何从新的iOS 7方法sizeWithAttributes返回多行文本CGSize?

我希望这产生与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] }]; 

此方法只产生一行文本的高度。

那么你可以试试这个:

 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]; 

这是我做到的:

  // 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]; 

这是我的方法来处理这两种情况,进入一个NSString类别。

 - (CGSize) sizeWithFontOrAttributes:(UIFont *) font { if (IS_IOS7) { NSDictionary *fontWithAttributes = @{NSFontAttributeName:font}; return [self sizeWithAttributes:fontWithAttributes]; } else { return [self sizeWithFont:font]; } } 

对于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 () ); 

Swift 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) 

如果您有标签集的文本,字体,数字宽度和宽度,则此方法将返回标签的大小:

 myLabel.numberOfLines = 0; CGSize size = [myLabel sizeThatFits:CGSizeMake(myLabel.frame.size.width, CGFLOAT_MAX)];` 

或者,如果您正在查看UITextView ,则始终可以使用NSLayoutManager方法:

 CGSize textSize = [textView.layoutManager usedRectForTextContainer:textView.textContainer].size; 

您还可以通过以下方式查找给定字体的行高:

 UIFont *font; CGFloat lineHeight = font.lineHeight; 

[作为新用户,我不能发表评论@testing的答案,但要使他的答案(对于xamarin.ios)更有用]

我们可以返回一个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 ); 
 CGSize stringsize = [lbl.text sizeWithAttributes: @{NSFontAttributeName:[UIFont fontWithName:FontProximaNovaRegular size:12.0]}]; CGSize adjustedSize = CGSizeMake(ceilf(stringsize.width), ceilf(stringsize.height)); 

使用ceilf方法进行合理pipe理