UILabel의 글꼴 크기를 동적으로 변경
나는 현재 UILabel
:
factLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 100)];
factLabel.text = @"some text some text some text some text";
factLabel.backgroundColor = [UIColor clearColor];
factLabel.lineBreakMode = UILineBreakModeWordWrap;
factLabel.numberOfLines = 10;
[self.view addSubview:factLabel];
iOS 응용 프로그램의 수명 동안 factLabel
다양한 가치를 얻습니다. 문장이 여러 개이고 단어가 5 ~ 6 개만있는 경우도 있습니다.
UILabel
텍스트가 항상 정의한 범위에 맞도록 글꼴 크기가 변경되도록 어떻게 설정할 수 있습니까?
한 줄:
factLabel.numberOfLines = 1;
factLabel.minimumFontSize = 8;
factLabel.adjustsFontSizeToFitWidth = YES;
위의 코드는 텍스트의 글꼴 크기를 8
레이블에 맞게 (예를 들어) 텍스트 크기 에 맞게 조정합니다. numberOfLines = 1
필수입니다.
여러 줄 :
내용 numberOfLines > 1
을 마지막 텍스트의 크기를 파악하는 방법이 있는 NSString의 sizeWithFont ... UIKit 첨가 방식, 예를 들어 :
CGSize lLabelSize = [yourText sizeWithFont:factLabel.font
forWidth:factLabel.frame.size.width
lineBreakMode:factLabel.lineBreakMode];
그 후 lLabelSize
, 예를 들어, (단, 레이블의 높이 만 변경한다고 가정)을 사용하여 레이블의 크기를 조정할 수 있습니다 .
factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, factLabel.frame.size.width, lLabelSize.height);
iOS6
한 줄:
iOS6부터는 minimumFontSize
더 이상 사용되지 않습니다. 라인
factLabel.minimumFontSize = 8.;
다음으로 변경할 수 있습니다.
factLabel.minimumScaleFactor = 8./factLabel.font.pointSize;
IOS 7
여러 줄 :
iOS7부터는 sizeWithFont
더 이상 사용되지 않습니다. 여러 줄 경우 :
factLabel.numberOfLines = 0;
factLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize maximumLabelSize = CGSizeMake(factLabel.frame.size.width, CGFLOAT_MAX);
CGSize expectSize = [factLabel sizeThatFits:maximumLabelSize];
factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, expectSize.width, expectSize.height);
minimumFontSize
iOS 6에서는 더 이상 사용되지 않습니다 minimumScaleFactor
.
yourLabel.adjustsFontSizeToFitWidth=YES;
yourLabel.minimumScaleFactor=0.5;
레이블 및 텍스트 너비에 따라 글꼴 크기를 처리합니다.
@Eyal Ben Dov의 답변에 따라 카테고리를 만들어 다른 앱에서 유연하게 사용할 수 있습니다.
Obs .: iOS 7과 호환되도록 코드를 업데이트했습니다
헤더 파일
#import <UIKit/UIKit.h>
@interface UILabel (DynamicFontSize)
-(void) adjustFontSizeToFillItsContents;
@end
구현 파일
#import "UILabel+DynamicFontSize.h"
@implementation UILabel (DynamicFontSize)
#define CATEGORY_DYNAMIC_FONT_SIZE_MAXIMUM_VALUE 35
#define CATEGORY_DYNAMIC_FONT_SIZE_MINIMUM_VALUE 3
-(void) adjustFontSizeToFillItsContents
{
NSString* text = self.text;
for (int i = CATEGORY_DYNAMIC_FONT_SIZE_MAXIMUM_VALUE; i>CATEGORY_DYNAMIC_FONT_SIZE_MINIMUM_VALUE; i--) {
UIFont *font = [UIFont fontWithName:self.font.fontName size:(CGFloat)i];
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font}];
CGRect rectSize = [attributedText boundingRectWithSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (rectSize.size.height <= self.frame.size.height) {
self.font = [UIFont fontWithName:self.font.fontName size:(CGFloat)i];
break;
}
}
}
@end
-용법
#import "UILabel+DynamicFontSize.h"
[myUILabel adjustFontSizeToFillItsContents];
건배
한 줄 -두 가지 방법이 있습니다. 간단하게 변경할 수 있습니다.
1- 실용적으로 (Swift 3)
다음 코드를 추가하십시오.
yourLabel.numberOfLines = 1;
yourLabel.minimumScaleFactor = 0.7;
yourLabel.adjustsFontSizeToFitWidth = true;
2-UILabel 속성 관리자 사용
i- Select your label- Set number of lines 1.
ii- Autoshrink- Select Minimum Font Scale from drop down
iii- Set Minimum Font Scale value as you wish , I have set 0.7 as in below image. (default is 0.5)
2015 년입니다. 여러 줄에서 작동하도록 Swift를 사용하여 최신 버전의 iOS 및 XCode에 대해 수행하는 방법을 설명하는 블로그 게시물을 찾아야했습니다.
- "자동 축소"를 "최소 글꼴 크기"로 설정하십시오.
- 글꼴을 원하는 가장 큰 글꼴 크기로 설정하십시오 (20을 선택했습니다)
- "줄 바꿈"을 "워드 랩"에서 "꼬리 자르기"로 변경하십시오.
출처 : http://beckyhansmeyer.com/2015/04/09/autoshrinking-text-in-a-multiline-uilabel/
스위프트 버전 :
textLabel.adjustsFontSizeToFitWidth = true
textLabel.minimumScaleFactor = 0.5
UILabel의 Swift 확장 기능은 다음과 같습니다. 이진 검색 알고리즘을 실행하여 레이블 경계의 너비와 높이를 기준으로 글꼴 크기를 조정합니다. iOS 9 및 자동 레이아웃과 작동하도록 테스트되었습니다.
사용법 :<label>
글꼴 크기 조정이 필요한 사전 정의 된 UILabel은 어디에 있습니까?
<label>.fitFontForSize()
기본적으로이 기능은 5pt 및 300pt 글꼴 크기 범위 내에서 검색하고 해당 범위 내에서 텍스트를 "완벽하게"맞도록 글꼴을 설정합니다 (1.0pt 내 정확함). 예를 들어 다음과 같은 방법으로 1pt 와 레이블의 현재 글꼴 크기 를 0.1pts 내 에서 정확하게 검색하도록 매개 변수를 정의 할 수 있습니다 .
<label>.fitFontForSize(1.0, maxFontSize: <label>.font.pointSize, accuracy:0.1)
다음 코드를 파일에 복사 / 붙여 넣기
extension UILabel {
func fitFontForSize(var minFontSize : CGFloat = 5.0, var maxFontSize : CGFloat = 300.0, accuracy : CGFloat = 1.0) {
assert(maxFontSize > minFontSize)
layoutIfNeeded() // Can be removed at your own discretion
let constrainedSize = bounds.size
while maxFontSize - minFontSize > accuracy {
let midFontSize : CGFloat = ((minFontSize + maxFontSize) / 2)
font = font.fontWithSize(midFontSize)
sizeToFit()
let checkSize : CGSize = bounds.size
if checkSize.height < constrainedSize.height && checkSize.width < constrainedSize.width {
minFontSize = midFontSize
} else {
maxFontSize = midFontSize
}
}
font = font.fontWithSize(minFontSize)
sizeToFit()
layoutIfNeeded() // Can be removed at your own discretion
}
}
참고 : 각 layoutIfNeeded()
통화는 자신의 재량에 따라 제거 할 수 있습니다
조금 정교하지는 않지만 작동해야합니다. 예를 들어 최대 글꼴 크기는 28 인 uilabel을 120x120으로 제한하고 싶다고 말할 수 있습니다.
magicLabel.numberOfLines = 0;
magicLabel.lineBreakMode = NSLineBreakByWordWrapping;
...
magicLabel.text = text;
for (int i = 28; i>3; i--) {
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:(CGFloat)i] constrainedToSize:CGSizeMake(120.0f, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
if (size.height < 120) {
magicLabel.font = [UIFont systemFontOfSize:(CGFloat)i];
break;
}
}
sizeToFit 메시지를 UITextView로 보내십시오. 텍스트에 맞게 자체 높이를 조정합니다. 자체 너비 나 원점을 변경하지 않습니다.
[textViewA1 sizeToFit];
스위프트 2.0 버전 :
private func adapteSizeLabel(label: UILabel, sizeMax: CGFloat) {
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
let maximumLabelSize = CGSizeMake(label.frame.size.width, sizeMax);
let expectSize = label.sizeThatFits(maximumLabelSize)
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, expectSize.width, expectSize.height)
}
이 솔루션은 여러 줄에 적용됩니다.
몇 가지 기사를 따르고 자동으로 텍스트 크기를 조정하고 주어진 레이블 크기에 가장 잘 맞게 줄 수를 조정하는 기능이 필요한 후에 함수를 직접 작성했습니다. (즉, 짧은 줄은 한 줄에 잘 맞고 많은 양의 레이블 프레임을 사용하는 반면, 긴 줄은 2 또는 3 줄로 자동 분할되어 그에 따라 크기를 조정합니다)
자유롭게 재사용하고 필요에 따라 조정하십시오. viewDidLayoutSubviews
초기 레이블 프레임이 설정되도록 완료 한 후에 호출해야합니다 .
+ (void)setFontForLabel:(UILabel *)label withMaximumFontSize:(float)maxFontSize andMaximumLines:(int)maxLines {
int numLines = 1;
float fontSize = maxFontSize;
CGSize textSize; // The size of the text
CGSize frameSize; // The size of the frame of the label
CGSize unrestrictedFrameSize; // The size the text would be if it were not restricted by the label height
CGRect originalLabelFrame = label.frame;
frameSize = label.frame.size;
textSize = [label.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize: fontSize]}];
// Work out the number of lines that will need to fit the text in snug
while (((textSize.width / numLines) / (textSize.height * numLines) > frameSize.width / frameSize.height) && (numLines < maxLines)) {
numLines++;
}
label.numberOfLines = numLines;
// Get the current text size
label.font = [UIFont systemFontOfSize:fontSize];
textSize = [label.text boundingRectWithSize:CGSizeMake(frameSize.width, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName : label.font}
context:nil].size;
// Adjust the frame size so that it can fit text on more lines
// so that we do not end up with truncated text
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, label.frame.size.width);
// Get the size of the text as it would fit into the extended label size
unrestrictedFrameSize = [label textRectForBounds:CGRectMake(0, 0, label.bounds.size.width, CGFLOAT_MAX) limitedToNumberOfLines:numLines].size;
// Keep reducing the font size until it fits
while (textSize.width > unrestrictedFrameSize.width || textSize.height > frameSize.height) {
fontSize--;
label.font = [UIFont systemFontOfSize:fontSize];
textSize = [label.text boundingRectWithSize:CGSizeMake(frameSize.width, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName : label.font}
context:nil].size;
unrestrictedFrameSize = [label textRectForBounds:CGRectMake(0, 0, label.bounds.size.width, CGFLOAT_MAX) limitedToNumberOfLines:numLines].size;
}
// Set the label frame size back to original
label.frame = originalLabelFrame;
}
다음은 애니메이션 글꼴 크기 변경을 구현하는 UILabel 서브 클래스의 채우기 코드입니다.
@interface SNTextLayer : CATextLayer
@end
@implementation SNTextLayer
- (void)drawInContext:(CGContextRef)ctx {
// We override this to make text appear at the same vertical positon as in UILabel
// (otherwise it's shifted tdown)
CGFloat height = self.bounds.size.height;
float fontSize = self.fontSize;
// May need to adjust this somewhat if it's not aligned perfectly in your implementation
float yDiff = (height-fontSize)/2 - fontSize/10;
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, 0.0, yDiff);
[super drawInContext:ctx];
CGContextRestoreGState(ctx);
}
@end
@interface SNAnimatableLabel ()
@property CATextLayer* textLayer;
@end
@interface SNAnimatableLabel : UILabel
- (void)animateFontToSize:(CGFloat)fontSize withDuration:(double)duration;
@end
@implementation SNAnimatableLabel
- (void)awakeFromNib {
[super awakeFromNib];
_textLayer = [SNTextLayer new];
_textLayer.backgroundColor = self.backgroundColor.CGColor;
_textLayer.foregroundColor = self.textColor.CGColor;
_textLayer.font = CGFontCreateWithFontName((CFStringRef)self.font.fontName);
_textLayer.frame = self.bounds;
_textLayer.string = self.text;
_textLayer.fontSize = self.font.pointSize;
_textLayer.contentsScale = [UIScreen mainScreen].scale;
[_textLayer setPosition: CGPointMake(CGRectGetMidX(_textLayer.frame), CGRectGetMidY(_textLayer.frame))];
[_textLayer setAnchorPoint: CGPointMake(0.5, 0.5)];
[_textLayer setAlignmentMode: kCAAlignmentCenter];
self.textColor = self.backgroundColor;
// Blend text with background, so that it doens't interfere with textlayer text
[self.layer addSublayer:_textLayer];
self.layer.masksToBounds = NO;
}
- (void)setText:(NSString *)text {
_textLayer.string = text;
super.text = text;
}
- (void)layoutSubviews {
[super layoutSubviews];
// Need to enlarge the frame, otherwise the text may get clipped for bigger font sizes
_textLayer.frame = CGRectInset(self.bounds, -5, -5);
}
- (void)animateFontToSize:(CGFloat)fontSize withDuration:(double)duration {
[CATransaction begin];
[CATransaction setAnimationDuration:duration];
_textLayer.fontSize = fontSize;
[CATransaction commit];
}
참고 URL : https://stackoverflow.com/questions/4865458/dynamically-changing-font-size-of-uilabel
'IT story' 카테고리의 다른 글
원격 시스템에서 Ansible 작업을 사용하여 파일을 이동 / 이름 바꾸기하는 방법 (0) | 2020.05.19 |
---|---|
PHP stdClass에서 배열로 (0) | 2020.05.19 |
React의 이벤트 객체에서 사용자 정의 속성에 액세스하는 방법은 무엇입니까? (0) | 2020.05.19 |
프로그래밍 방식으로 터치 이벤트를 UIButton에 가짜로 만드는 방법은 무엇입니까? (0) | 2020.05.19 |
정렬 된 목록은 CSS로 1.1, 1.2, 1.3 (1, 2, 3 대신 ...)과 같은 결과를 생성 할 수 있습니까? (0) | 2020.05.19 |