반응형
iOS 7 sizeWithAttributes : sizeWithFont : 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];
스위프트 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;
[As a new user, I cannot post a comment to @testing's answer, but to make his answer (for xamarin.ios) more useful]
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
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));
use ceilf
method to manage properlly
반응형
'programing tip' 카테고리의 다른 글
Xcode 7 베타 경고 : 인터페이스 방향 및 시작 스토리 보드 (0) | 2020.06.29 |
---|---|
계획된 "개인 보호"C # 액세스 수정 자의 의미는 무엇입니까? (0) | 2020.06.28 |
pandas를 사용하여 파이썬으로 Excel 파일 읽기 (0) | 2020.06.28 |
matplotlib의 날짜 눈금 및 회전 (0) | 2020.06.28 |
푸시 컨트롤러에서 RootViewController를 어떻게 얻습니까? (0) | 2020.06.28 |