1. 程式人生 > >如何計算文字大小行間距的label的高度

如何計算文字大小行間距的label的高度

1,單純調節行間距的方法、能夠調整行間距,但是不能調整字間距
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 200)];
    [label setBackgroundColor:[UIColor blackColor]];
    [label setFont:[UIFont systemFontOfSize:16]];
    [label setTextColor:[UIColor whiteColor]];
    [label setNumberOfLines:0];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:LINESPACE];//調整行間距

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;

    [self.view addSubview:label];
    [label sizeToFit];



2、附件中有自定義的ZXHMultiLineLabel,以及使用如下:
    ZXHMultiLineLabel *readNewsLable =[[ZXHMultiLineLabel alloc] initWithFrame:CGRectZero];
    readNewsLable.textColor = MAIN_BASE_GRAY_COLOR;
    readNewsLable.lineBreakMode = NSLineBreakByWordWrapping;
    readNewsLable.backgroundColor = [UIColor yellowColor];

    readNewsLable.font = [UIFont fontWithName:contentFontName size:16];
    [readNewsLable setText:labelText];

    /*設定label的frame值*/
    [readNewsLable setFrame:CGRectMake(0, 20, 320, [readNewsLable getAttributedStringHeightWidthValue:320])];

    NSLog(@"Label高度是多少? %i", [readNewsLable getAttributedStringHeightWidthValue:320]);

    readNewsLable.numberOfLines = 0;
    [self.view addSubview:readNewsLable];


3、根據文字和字型,計算文字的特定高度SpecificWidth內的顯示高度
- (CGFloat) initAttributedString:(NSString *)normalString withFont:(NSString *)fontName withSpecificWidth:(CGFloat)inWidth
{
    long stringCharacterSpacing = 1.0f;//字間距
    CGFloat stringLinesSpacing = 4.4f; //行間距

    if(!normalString){
        return 0.0f;
    }

//去掉空行
NSString *myString = [normalString stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\n"];

//建立AttributeString
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:myString];

//設定字型及大小
    UIFont * font = [UIFont fontWithName:contentFontName size:16];
    CTFontRef helveticaBold = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, NULL);
    [attributedString addAttribute:(id)kCTFontAttributeName value:(__bridge id)helveticaBold range:NSMakeRange(0,[attributedString length])];

//設定字間距
//        long number = 1.5f;
//    long number = self.characterSpacing;
    long number = stringCharacterSpacing;

CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    [attributedString addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedString length])];
    CFRelease(num);
    /*
     if(self.characterSpacing)
     {
     long number = self.characterSpacing;
     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
     [attributedString addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(0,[attributedString length])];
     CFRelease(num);
     }
     */

//設定字型顏色
//        [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:(id)(self.textColor.CGColor) range:NSMakeRange(0,[attributedString length])];
    [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:(id)([UIColor grayColor].CGColor) range:NSMakeRange(0,[attributedString length])];

//建立文字對齊方式
CTTextAlignment alignment = kCTLeftTextAlignment;
    /*
    if(self.textAlignment == NSTextAlignmentCenter)
    {
        alignment = kCTCenterTextAlignment;
    }
    if(self.textAlignment == NSTextAlignmentRight)
    {
        alignment = kCTRightTextAlignment;
    }
    if(self.textAlignment == NSTextAlignmentLeft)
    {
        alignment = kCTTextAlignmentLeft;
    }
     */
//預設左對齊
    alignment = kCTTextAlignmentLeft;


CTParagraphStyleSetting alignmentStyle;

    alignmentStyle.spec = kCTParagraphStyleSpecifierAlignment;

    alignmentStyle.valueSize = sizeof(alignment);

    alignmentStyle.value = &alignment;

//設定文字行間距

    CGFloat lineSpace = stringLinesSpacing;

CTParagraphStyleSetting lineSpaceStyle;
    lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
    lineSpaceStyle.valueSize = sizeof(lineSpace);
    lineSpaceStyle.value =&lineSpace;

//設定文字段間距
    CGFloat paragraphSpacing = 15.0;
    CTParagraphStyleSetting paragraphSpaceStyle;
    paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
    paragraphSpaceStyle.valueSize = sizeof(CGFloat);
    paragraphSpaceStyle.value = ¶graphSpacing;

//建立設定陣列
    CTParagraphStyleSetting settings[ ] ={alignmentStyle,lineSpaceStyle,paragraphSpaceStyle};
    CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,3);

//給文字新增設定
    [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(__bridge id)style range:NSMakeRange(0 , [attributedString length])];
    CFRelease(helveticaBold);

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef) attributedString);   //string 為要計算高度的
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter,CFRangeMake(0, 0), NULL,CGSizeMake(inWidth, CGFLOAT_MAX), NULL);
    CFRelease(framesetter);
    return suggestedSize.height;
}