ios字符串自适应rect

来源:互联网 发布:陈星网络情缘酷我音乐 编辑:程序博客网 时间:2024/06/05 06:38
-(void)reSetFontSizeToFitRect:(CGRect)rect Stingr:(NSString*)str{    NSMutableParagraphStyle* pharaStyle = [[NSMutableParagraphStyle alloc] init];    pharaStyle.lineBreakMode = NSLineBreakByTruncatingTail;    pharaStyle.alignment = NSTextAlignmentCenter;        NSInteger fontSize = 15;    for(int i = 0;i<20;i++){<span style="white-space:pre"></span><span style="color:#ff6666;">//比较的时候如果字体不相同,得出的结果是不正确的</span>        CGSize size = [str sizeWithAttributes:@{NSFontAttributeName:[<span style="color:#ff0000;">UIFont systemFontOfSize:fontSize]</span>}];        NSLog(@"size.width:%f  rect.size.width:%f",size.width,rect.size.width);        if(size.width<=rect.size.width){            UIFont *font = [UIFont systemFontOfSize:fontSize];            NSLog(@"%@",str);            [str drawInRect:rect withAttributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:pharaStyle}];            NSLog(@"fontSize %ld",fontSize);            break;        }                fontSize--;    }    }

0 0
原创粉丝点击