根据Html文本设置控件的尺寸

来源:互联网 发布:知乎av封面杀手 编辑:程序博客网 时间:2024/05/16 20:28

  //先把html文本转换成富文本内容

NSString * htmlString;

  NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc]

                                                  initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding]

                                                  options:@{

                                                            NSDocumentTypeDocumentAttribute :NSHTMLTextDocumentType

                                                            }

                                                  documentAttributes:nil

                                                  error:nil];

//再根据富文本内容计算尺寸

    NSDictionary * attributesDic = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:14],NSFontAttributeName,nil];

    NSRange  range =NSMakeRange(0, attributedString.length);

    [attributedString addAttributes:attributesDicrange:range];

    

    NSStringDrawingOptions options =NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading;

    CGRect rect = [attributedStringboundingRectWithSize:CGSizeMake(width,CGFLOAT_MAX)

                                                 options:options

                                                 context:nil];



0 0
原创粉丝点击