IOS7sizeWithAttritribute: sizeWithFont 的替代品: strainedToSize

如何从新的 iOS7方法 sizeWithAttritribute 返回多行文本 CGSize?

我希望这会产生与 sizeWithFont: strainedToSize 相同的结果。

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

此方法只生成单行文本的高度。

89199 次浏览

你可以试试这个:

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

[作为一个新用户,我不能在@test 的回答上发表评论,但是可以让他的回答(对 xamarin.ios)更有用]

我们可以返回一个 CGRect,并且只使用针对 UIButton 等的 gui 项的 height 参数。传入我们需要的任何参数,如下所示

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

作为一种替代方法,如果你正在查看 UITextView,你总是可以使用 NSLayoutManager方法:

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

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

UIFont *font;
CGFloat lineHeight = font.lineHeight;

如果您有文本、字体、 numberOfLines 和标签的宽度设置,此方法返回标签的大小:

myLabel.numberOfLines = 0;


CGSize size = [myLabel sizeThatFits:CGSizeMake(myLabel.frame.size.width, CGFLOAT_MAX)];`
CGSize stringsize = [lbl.text sizeWithAttributes:
@{NSFontAttributeName:[UIFont fontWithName:FontProximaNovaRegular size:12.0]}];




CGSize adjustedSize = CGSizeMake(ceilf(stringsize.width), ceilf(stringsize.height));

使用 ceilf方法进行正确的管理

迅捷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)

斯威夫特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)