CVe*_*tex 5 iphone fonts objective-c uilabel
我正在尝试测量NSString的视觉大小,该大小考虑了我可以渲染的行数.但是,sizeWithFont没有考虑numberOfLines属性?因此,我的布局算法将所有内容都置于低于实际需要的位置.
_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];
Run Code Online (Sandbox Code Playgroud)
有谁知道如何使用iPhone SDK做到这一点?
Kev*_*lar 12
而不是CGFLOAT_MAX用于文本计算的最大高度,请尝试使用以下方法获取一行的大小:
[_price.text sizeWithFont:_price.font].height
Run Code Online (Sandbox Code Playgroud)
然后将其乘以您想要的最大行数,然后将其插入您自己约束的大小的高度.它可能看起来像这样:
_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];
Run Code Online (Sandbox Code Playgroud)
如果您将行数设置为0,请不要使用此项,因为在这种情况下您的最大高度将为0; 你应该使用CGFLOAT_MAX.
使用UILabel的sizeToFit而不是sizeWithFont:来布局多行UILabel,因为sizeWithFont:将截断字符串(请参阅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;
}
}
Run Code Online (Sandbox Code Playgroud)
使用带有您喜欢的文字和字体的标签来调用它:
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];
Run Code Online (Sandbox Code Playgroud)
startCharacterWrapAtSize参数允许您选择从给定字体大小开始使用characterWrap.这应该可以节省空间,wordWrap会使用非常小的字体.
编辑:错误修正
| 归档时间: |
|
| 查看次数: |
21510 次 |
| 最近记录: |