IOS develops -UILabel, height computing (text, attributedText)

IOS development buddies, affirmation of the UILabel based control of this understanding is very deep, also some of the label text width height calculation, especially for the spacing of cases sometimes feel headache, everyone has their own method, some may be more similar, recent projects encountered the multi line with the spacing of more, this two days to a method previously used, in fact, is not so new, share out for everyone to see, I hope to help students in need.

OK, let me introduce some of the gadgets I use first:

#import@interface UILabel (YGZExt)

@property (nonatomic, readonly) CGFloat lineHeight; / text line height

@property (nonatomic, readonly) NSInteger textNumberOfLines; / / the actual text line number

@property (nonatomic, readonly) CGFloat textHeight; / / the actual text height

@property (nonatomic, readonly) CGFloat textWidth; / / the actual text width

– – (CGSize) textSizeForLimitedSize: (CGSize) size;

– (NSMutableAttributedString *) attributedStringWith: (NSString *), string, lineSpace: (CGFloat), lineSpace, limitWidth: (CGFloat), limitWidth;

@end

See below, concrete implementation:

#import “UILabel+YGZExt.h”

#import “UIView+YGZExt.h””

@implementation UILabel (YGZExt)

– – (CGFloat) lineHeight {

CGSize rowSize = “single” sizeWithAttributes:[NSDictionary [@ dictionaryWithObjectsAndKeys:self.font, NSFontAttributeName, nil]];

Return rowSize.height;

}

– – (NSInteger) textNumberOfLines {

CGSize, contentSize = [self.text, boundingRectWithSize:CGSizeMake (self.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin, attributes:@{NSFontAttributeName:self.font}, context:nil].size;

Return contentSize.height / self.lineHeight;

}

– – (CGFloat) textHeight {

CGSize, size = [self, textRectForBounds:CGRectMake (0, 0, self.width, 10000) limitedToNumberOfLines:self.numberOfLines].size;

Return size.height;

}

– – (CGFloat) textWidth {

CGSize, size = [self, textRectForBounds:CGRectMake (0, 0, self.width, 10000) limitedToNumberOfLines:self.numberOfLines].size;

Return size.width;

}

– – (CGSize) textSizeForLimitedSize: (CGSize) size {

CGRect, rect = CGRectMake (self.frame.origin.x, self.frame.origin.y, size.width, size.height);

Self.frame = rect;

CGFloat, height = self.textHeight, &gt, size.height, size.height: self.textHeight;

Return CGSizeMake (self.textWidth, height);

}

– (NSMutableAttributedString *) attributedStringWith: (NSString *), string, lineSpace: (CGFloat), lineSpace, limitWidth: (CGFloat), limitWidth {

If (string = = Nil) {

String = @””;

}

Self.width = limitWidth;

Self.text = string;

LineSpace = self.textNumberOfLines, > 1, lineSpace: 0;

NSMutableAttributedString *attributedString = [[NSMutableAttributedString, alloc], initWithString:string];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle, alloc], init];

ParagraphStyle.lineSpacing = lineSpace; / / adjust line spacing

NSRange range = NSMakeRange (0, [string, length]);

[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];

Return attributedString;

}

@end

How about using it? Please see the following test results:

IOS develops -UILabel, height computing (text, attributedText)
single line
IOS develops -UILabel, height computing (text, attributedText)
multiple lines

Next, look at the line spacing:

IOS develops -UILabel, height computing (text, attributedText)
single line
IOS develops -UILabel, height computing (text, attributedText)
multiple lines

Demo
on GitHub