iPhone Uilabel SizewithFont :
-
05-07-2019 - |
문제
렌더링 할 수있는 라인의 수를 고려한 NSString의 시각적 크기를 측정하려고합니다. 그러나 SizewithFont는 부동산에 대한 계정 번호를 고려하지 않습니까? 따라서 내 레이아웃 알고리즘은 모든 것을 실제로 필요한 것보다 낮게 배치합니다.
_price = [[UILabel alloc] init];
_price.text = myPriceValue;
_price.lineBreakMode = UILineBreakModeWordWrap;
_price.numberOfLines = 3;
_price.backgroundColor = [UIColor clearColor];
_price.textColor = TTSTYLEVAR(colorPrice);
/// the follow code ignores numberOfLines and just tells me the size of the whole block.
// I'd like it to be aware of numberOfLines
//
CGSize priceSize = [_price.text sizeWithFont:_price.font
constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
iPhone SDK를 사용 하여이 작업을 수행하는 방법을 아는 사람이 있습니까?
해결책
텍스트 계산의 최대 높이에 대한 cgfloat_max 대신 한 줄의 크기를 얻으십시오.
[_price.text sizeWithFont:_price.font].height
그런 다음 원하는 최대 라인을 곱한 다음 자신이 제한하는 크기의 높이에 꽂습니다. 아마도 다음과 같이 보일 것입니다.
_price = [[UILabel alloc] init];
_price.text = myPriceValue;
_price.lineBreakMode = UILineBreakModeWordWrap;
_price.numberOfLines = 3;
_price.backgroundColor = [UIColor clearColor];
_price.textColor = TTSTYLEVAR(colorPrice);
CGFloat lineHeight = [_price.text sizeWithFont:_price.font].height;
CGSize priceSize = [_price.text sizeWithFont:_price.font
constrainedToSize:CGSizeMake(maxWidth, lineHeight * _price.numberOfLines)
lineBreakMode:UILineBreakModeWordWrap];
이 경우 최대 높이가 0이므로 라인 수를 0으로 설정 한 경우에 사용하지 마십시오. 그런 다음 cgfloat_max를 사용해야합니다.
다른 팁
SizewithFont : SizewithFont : 끈을 자르기 때문에 SizewithFont 대신 Uilabel의 시즈 세트를 사용하십시오 (Apple Docs 참조). 다음 코드는 텍스트가 지정된 크기에 맞을 때까지 레이블의 글꼴 크기를 줄입니다. 지정된 높이에 맞는 즉시 여러 줄의 텍스트가 사용됩니다.
-(void)setFontSizeOfMultiLineLabel: (UILabel*)label
toFitSize: (CGSize) size
forMaxFontSize: (CGFloat) maxFontSize
andMinFontSize: (CGFloat) minFontSize
startCharacterWrapAtSize: (CGFloat)characterWrapSize{
CGRect constraintSize = CGRectMake(0, 0, size.width, 0);
label.frame = constraintSize;
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0; // allow any number of lines
for (int i = maxFontSize; i > minFontSize; i--) {
if((i < characterWrapSize) && (label.lineBreakMode == UILineBreakModeWordWrap)){
// start over again with lineBreakeMode set to character wrap
i = maxFontSize;
label.lineBreakMode = UILineBreakModeCharacterWrap;
}
label.font = [label.font fontWithSize:i];
[label sizeToFit];
if(label.frame.size.height < size.height){
break;
}
label.frame = constraintSize;
}
}
좋아하는 텍스트와 글꼴이있는 레이블로 이것을 호출하십시오.
UILabel *label = [[UILabel alloc] initWithFrame: CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = text;
label.font = [UIFont fontWithName: @"Helvetica" size: 16];
[self setFontSizeOfMultiLineLabel: label toFitSize: CGSizeMake(200, 44) forMaxFontSize: 16 andMinFontSize: 8 startCharacterWrapAtSize: 11];
STARTCHARACTERWRAPATSIZE 매개 변수를 사용하면 Giving Font 크기에서 시작하여 캐릭터 트랩을 사용할 수 있습니다. 이것은 WordWrap이 실제로 작은 글꼴을 사용하는 경우 공간을 절약해야합니다.
편집 : 버그 픽스
한 번의 전화로 시도하는 대신 이와 같은 일을하십시오 (의사를 용서해보십시오. 늦었습니다) :
NSString *s = _price.text;
UIFont *font = _price.font;
CGFloat fontSize = font.pointSize;
while (TRUE)
{
CGSize priceSize = [s sizeWithFont: font constrainedToSize:
CGSizeMake(maxWidth, fontSize) lineBreakMode: UILineBreakModeWordWrap];
if ( /* priceSize is satisfactory */ )
{
break; // Make sure this exits, eventually!!!
}
fontSize -= 1.0; // or a smaller decrement if you like
font = // new, smaller font
}
정답은 물론 NumberoFlines를 0으로 설정해야하므로 프레임 워크가 필요한 많은 줄로 결과를 계산할 수 있습니다. 또한보십시오 이 질문.
물론 호출되거나 통과되지 않은 것은 그 정보가 없기 때문에 그것을 고려하지 않습니다. 당신은 줄, 크기 및 글꼴로 엄격하게 작업하고 있습니다. 라인 수가있는 레이블입니다.
당신의 문제가 정확히 무엇인지 잘 모르겠습니다. 너무 키가 크거나 너무 짧은 크기를 얻고 있습니까? 결과의 높이를 글꼴의 높이로 나누어 텍스트 라인의 수를 찾을 수 있습니다.이 높이는 오스카더 + 내림차장의 값입니다.