iOS小demo之获取文字高度

来源:互联网 发布:python splinter 编辑:程序博客网 时间:2024/05/16 14:12

//本帖为自用贴,不喜勿喷


-(float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width

{

    

    UILabel*contentLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,0,

                                                                     width,0)];

    [contentLabel setText:value];

    contentLabel.font = [UIFontsystemFontOfSize:fontSize];

    contentLabel.numberOfLines=0;

    contentLabel.textColor = [UIColorcolorWithRed:27/255.0green:27/255.0blue:27/255.0alpha:1];

    // _contentLabel.backgroundColor=[UIColor greenColor];

    

    NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc] initWithString:value];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc] init];

    

    //[paragraphStyle setLineSpacing:5];//调整行间距

    

    [attributedStringaddAttribute:NSParagraphStyleAttributeNamevalue:paragraphStyle range:NSMakeRange(0, [valuelength])];

    contentLabel.attributedText = attributedString;

    [contentLabel sizeToFit];

    

    NSLog(@"textview:%f++",contentLabel.frame.size.height);

    

    return contentLabel.frame.size.height;

}


0 0