我正在尝试将标签高度修改为动态,以取决于文本。
CGSize size = [Str sizeWithAttributes:
@{NSFontAttributeName: [UIFont fontWithName:@"OpenSans" size:15]}];
CGSize adjustedSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
NSLog(@"this is height of the label =%@",NSStringFromCGSize(adjustedSize));
这里我得到了宽度和高度,但宽度超过了屏幕尺寸宽度。
我想要的是标签宽度应该等于我的屏幕尺寸,高度取决于文字........
答案 0 :(得分:2)
设置
yourLabel.numberOfLines = 0;
[yourLabel sizeToFit];
为该标签指定特定宽度或前导和尾随空格,以使宽度保持固定,并根据文本长度增加高度。
您还可以通过xib文件设置numberOfLines。 1.单击xib中的标签。 2.在对齐选项卡下方的右侧面板上,您可以将numberOfLines从1设置为0.
答案 1 :(得分:1)
答案 2 :(得分:1)
我认为您的自定义字体会产生问题。您可以使用此代码计算动态大小
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
答案 3 :(得分:1)
您可以将标签高度修改为动态,以取决于具有以下代码的文本
NSString * str = [NSString stringWithFormat:@“Lorem Ipsum只是打印和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是业界标准的虚拟文本,当时一台未知的打印机采用了类型的厨房把它拼凑成一本样本书。“];
CGSize maximumLabelSize = CGSizeMake(self.view.frame.size.width - 20, FLT_MAX);
CGSize expectedLabelSize = [str sizeWithFont:[UIFont fontWithName:@"OpenSans" size:15] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
UILabel *Label = [[UILabel alloc] init];
[Label setTranslatesAutoresizingMaskIntoConstraints:NO];
//set new frame to label.
CGRect newFrame = Label.frame;
newFrame.origin.x = 10;
newFrame.origin.y = 20;
newFrame.size.width = expectedLabelSize.width - 20;
newFrame.size.height = expectedLabelSize.height;
Label.frame = newFrame;
Label.text = str;
Label.lineBreakMode = NSLineBreakByWordWrapping;
Label.numberOfLines = 20;
Label.font = [UIFont fontWithName:@"OpenSans" size:15];
Label.backgroundColor = [UIColor redColor];
[self.view addSubview:Label];
//width constraint
[Label addConstraint:[NSLayoutConstraint constraintWithItem:Label
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier:1
constant:expectedLabelSize.width - 20]];
//height constraint
[Label addConstraint:[NSLayoutConstraint constraintWithItem:Label
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier:1
constant: expectedLabelSize.height]];
//center constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:Label
attribute: NSLayoutAttributeCenterX
multiplier:1
constant:0]];
// Top constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:Label
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.topLayoutGuide
attribute: NSLayoutAttributeBottom
multiplier:1
constant:20]];
答案 4 :(得分:0)
1.来自Xib你需要设置否。该标签的第0行:
并使用此方法:
<?php echo esc_url( $product->add_to_cart_url() ); ?>
将此方法称为:
- (CGFloat)getHeightforController:(id)view{
UILabel *tempView =(UILabel *)view;
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
context.minimumScaleFactor = 0.8;
float width = tempView.frame.size.width;
width = width * ([[UIScreen mainScreen] bounds].size.width/320);
CGSize size=[tempView.text boundingRectWithSize:CGSizeMake(tempView.frame.size.width, 200)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName : tempView.font}
context:context].size;
return size.height;
}
确保在调用此方法之前为标签设置文本
答案 5 :(得分:0)
使用自动布局或这样做
yourLabel.numberOfLines = 0;
yourLabel.lineBreakMode = NSLineBreakByWordWrapping;
yourLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;