Hoe retourneer je een tekst-CGS-grootte met meerdere regels van de nieuwe iOS 7-methode sizeWithAttributes?
Ik wil dat dit dezelfde resultaten oplevert als 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] }];
Deze methode produceert alleen de hoogte voor een enkele regel tekst.
Antwoord 1, autoriteit 100%
nou, je kunt dit proberen:
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];
Antwoord 2, autoriteit 8%
Dit is hoe ik het deed:
// 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];
Antwoord 3
Hier is mijn methode om met beide situaties om te gaan, valt in de categorie NSString
.
- (CGSize) sizeWithFontOrAttributes:(UIFont *) font {
if (IS_IOS7) {
NSDictionary *fontWithAttributes = @{NSFontAttributeName:font};
return [self sizeWithAttributes:fontWithAttributes];
} else {
return [self sizeWithFont:font];
}
}
Antwoord 4
Voor 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 ()
);
Antwoord 5
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)
Swift 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)
Antwoord 6
Als u de tekst, het lettertype, de numberOfLines en de breedte van uw label hebt ingesteld, retourneert deze methode de grootte van uw label:
myLabel.numberOfLines = 0;
CGSize size = [myLabel sizeThatFits:CGSizeMake(myLabel.frame.size.width, CGFLOAT_MAX)];`
Antwoord 7
Als alternatief, als u naar UITextView
kijkt, kunt u altijd de NSLayoutManager
-methode gebruiken:
CGSize textSize = [textView.layoutManager usedRectForTextContainer:textView.textContainer].size;
Je kunt de regelhoogte voor een bepaald lettertype ook vinden door:
UIFont *font;
CGFloat lineHeight = font.lineHeight;
Antwoord 8
[Als nieuwe gebruiker kan ik geen commentaar plaatsen op het antwoord van @testing, maar om zijn antwoord (voor xamarin.ios) nuttiger te maken]
We kunnen een CGRect retourneren en alleen de hoogteparameter gebruiken voor het gui-item dat we targeten, UIButton enz. Alle parameters die we nodig hebben, zoals hieronder, doorgeven
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
);
Antwoord 9
CGSize stringsize = [lbl.text sizeWithAttributes:
@{NSFontAttributeName:[UIFont fontWithName:FontProximaNovaRegular size:12.0]}];
CGSize adjustedSize = CGSizeMake(ceilf(stringsize.width), ceilf(stringsize.height));
gebruik de ceilf
methode om goed te beheren