UITextView内容自适应高度&行间距的调转

来源:互联网 发布:python爬虫58同城 编辑:程序博客网 时间:2024/04/29 20:11

UITextView *priceInfoTXTV=[[UITextViewalloc] initWithFrame:CGRectMake(10,5, PDEVICEWIDTH-20,50)];

    

    priceInfoTXTV.font=[UIFontsystemFontOfSize:14];

    priceInfoTXTV.editable=NO;

    

    priceInfoTXTV.text=@"测试内容,测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容。\n测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容。\n测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容》\n";

    //设置行间距

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc] init];

    paragraphStyle.lineSpacing =5;// 字体的行间距

    

    NSDictionary *attributes =@{

                                 NSFontAttributeName:[UIFontsystemFontOfSize:14],

                                 NSParagraphStyleAttributeName:paragraphStyle

                                 };

    priceInfoTXTV.attributedText = [[NSAttributedStringalloc] initWithString:priceInfoTXTV.textattributes:attributes];

    

     CGRect txtFrame = priceInfoTXTV.frame;

     float textViewContentHeight = txtFrame.size.height =[[NSStringstringWithFormat:@"%@\n ",priceInfoTXTV.text]

                                                  boundingRectWithSize:CGSizeMake(txtFrame.size.width,CGFLOAT_MAX)

                                                  options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading

                                                  attributes:attributes context:nil].size.height;

    

    priceInfoTXTV.frame=CGRectMake(priceInfoTXTV.frame.origin.x, priceInfoTXTV.frame.origin.y, priceInfoTXTV.frame.size.width, textViewContentHeight);

0 0
原创粉丝点击