새로운 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];
답변
스위프트 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)
답변
두 가지 상황을 모두 다루는 방법은 다음과 같습니다 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 ()
);
답변
텍스트, 글꼴, numberOfLines 및 레이블 너비를 설정 한 경우이 메소드는 레이블 크기를 반환합니다.
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;
