博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设置行间距,自适应文字大小
阅读量:7167 次
发布时间:2019-06-29

本文共 1391 字,大约阅读时间需要 4 分钟。

 
/*
*
 *  设置富文本(大小)
 *
 *  @param neirong   字符串
 *  @param lineSpace 行间距
 *
 *  @return NSMutableAttributedString
 
*/
+(NSMutableAttributedString *)attributedString:(NSString *)neirong space:(NSInteger)lineSpace font:(NSInteger)font;
/*
*
 *  计算富文本大小
 *
 *  @param width     宽度
 *  @param neirong   字符串
 *  @param lineSpace 行距
 *
 *  @return CGRect
 
*/
+ (CGRect)boundingRectWithSize:(NSInteger)width attributedString:(NSString *)neirong space:(NSInteger)lineSpace font:(NSInteger)font;

 

+(NSMutableAttributedString *)attributedString:(NSString *)neirong space:(NSInteger)lineSpace font:(NSInteger)font{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:neirong];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
//
调整行间距
    [attributedString setAttributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:[UIFont systemFontOfSize:font],} range:NSMakeRange(
0, [neirong length])];
    
return attributedString;
}
+(CGRect)boundingRectWithSize:(NSInteger)width attributedString:(NSString *)neirong space:(NSInteger)lineSpace font:(NSInteger)font{
    CGRect rect = [[Common attributedString:neirong space:lineSpace font:font] boundingRectWithSize:CGSizeMake(width, 
2000) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    
return rect;
}

效果图:

 

转载于:https://www.cnblogs.com/hxwj/p/4582643.html

你可能感兴趣的文章