Several methods of dynamically adjusting the height of uilabel in IOS

Time:2020-11-25

brief introduction

The uilabel class implements a read-only text view. You can use this class to draw one or more lines of static text, for example, you may use other parts of the user interface identified. The uilabel class supports both simple and complex styles of label text, and can also control the appearance, such as whether the label uses a shadow or attracts a big highlight.

In IOS programs, what you can see and what you can touch are all subclasses of uiview. Uilabel is a label view class used to display text information, which is a subclass of uiview.

The following is the official website of uilabel: https://developer.apple.com/reference/uikit/uilabel

introduction

Sometimes we need to adjust the height of uilabel dynamically according to the font format (including font size and row spacing) in uilabel.

The author provides two methods to get the true height of uilabel according to the specific font, line spacing and width of uilabel.

There are three steps to dynamically adjust the height of the label

  • Gets the attributedstring with a specific format.
  • Get the new height according to attributedstring.
  • Reset the height of the label.

#Sample demo

The author compares whether there is dynamic height adjustment by displaying labels with the same content at the same time.

##Implementation steps:

####1. Get attributedstring with specific format

/**
 *Gets the property string in a specific format
 *
 *@ param string original string
 *@ param font
 *@ param spacing
 *
 *@ return a property string in a specific format
 */
- (NSAttributedString *)attributedStringWithString: (NSString *)string andFont: (UIFont *)font andLineSpacing:(CGFloat)spacing
{
 NSMutableAttributedString *contentString = [[NSMutableAttributedString alloc] initWithString:string];
 [contentString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, contentString.length)];
 NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
 [paragraphStyle setAlignment:NSTextAlignmentLeft];
 [paragraphStyle setLineSpacing:spacing];
 [contentString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contentString length])]; 
 return [contentString copy];
}

####2. Get the height according to attributedstring

/**
 *Gets the new height calculated from the property string of a specific format
 *
 *@ param string a property string of a specific format
 *The widest width of @ param maxwidth uilabe (line break point)
 *
 *@ return the new height calculated based on a property string of a specific format
 */
- (CGSize)sizeOfAttributedString: (NSAttributedString *)string withMaxWidth: (CGFloat)maxWidth
{
  CGRect rect = [string boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
  return rect.size;
}

####3. Reset the height of the label


NSAttributedString *string = [self attributedStringWithString:self.contentString andFont:[UIFont systemFontOfSize:15.0] andLineSpacing:2.0];
CGSize size = [self sizeOfAttributedString:string withMaxWidth:maxWidth];
self.resizeLabel.frame = CGRectMake((self.view.bounds.size.width - maxWidth)/2, 400, maxWidth, size.height);
self.resizeLabel.attributedText = string;

For comparison, a uilabel with the same format content is displayed


self.orignalLabel.attributedText = [self attributedStringWithString:self.contentString andFont:[UIFont systemFontOfSize:15.0] andLineSpacing:2.0 ];

summary

The above is the whole content of this article, I hope that the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message and exchange, thank you for your support to developeppaer.