iOS NSAttributedString simple encapsulation is compatible with displaying HTML content centered underline

iOS NSAttributedString simple encapsulation is compatible with displaying HTML content centered underline

question

There are many places in the project that use rich text, HTML text, etc., such as underline styles, different font sizes, etc. Currently, NSAttributedString is used to achieve the effect.

Some of the pits, after stepping on them, found a solution, so I made the following simple package.
Solve the problem:

1. Centered font format: left by default, some need to be centered
2. Compatible display of HTML content: Some rich text comes with HTML content, which cannot destroy its actual content, such as font color, etc.
3. Underline:

solve

specific code

/**
 content:NSString 显示内容
 fontSize:int 字体大小
 color:UIColor 字体颜色
 needLine:BOOL 是否需要下划线
 isCenter:BOOL 是否居中
 witdh: 是html且包含图片时,图片显示最大宽度
 */
+(NSAttributedString *)fetchHtmlStringByContent:(NSString *)content fontSize:(int)fontSize color:(UIColor *)color needUserLine:(BOOL)needLine isCenter:(BOOL)isCenter htmlImageMaxWitdh:(CGFloat)witdh{
    
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    if(isCenter){
    
    
        paragraphStyle.alignment = NSTextAlignmentCenter;
    }
    
    NSMutableDictionary * attris = [NSMutableDictionary dictionaryWithDictionary:@{
    
    NSFontAttributeName:[UIFont boldSystemFontOfSize:fontSize],NSForegroundColorAttributeName:color,NSParagraphStyleAttributeName:paragraphStyle}];
    if(needLine){
    
    
        [attris setObject:[NSNumber numberWithInteger:NSUnderlineStyleSingle] forKey:NSUnderlineStyleAttributeName];
    }
    if([content containsString:@"<"] && [content containsString:@">"] && [content containsString:@"</"]){
    
    //html
        [attris setObject:NSHTMLTextDocumentType forKey:NSDocumentTypeDocumentAttribute];
        if(witdh>0){
    
    
            content = [NSString stringWithFormat:@"<head><style>img{width:auto;height:auto;max-width:%f}</style></head>%@",witdh,content];
        }
        NSMutableAttributedString * attribtStr = [[NSMutableAttributedString alloc] initWithData:[content dataUsingEncoding:NSUnicodeStringEncoding] options:attris documentAttributes:nil error:nil];
        return attribtStr;
    }else{
    
    
        NSAttributedString * attribtStr = [[NSAttributedString alloc]initWithString:content attributes:attris];
        return attribtStr;
    }
}

other problems

When using tableview to display NSAttributedString, the height of the row will be dynamically calculated. At this time, the calculation of the content before conversion will be different from the width and height of the actual displayed content.

The specific reason is: there will be some content characters (such as HTML tags) in the content. After being converted and displayed by NSAttributedString, the tags will disappear, but these characters will be calculated when using the content to calculate the width and height.

Solution: Do not use content calculation, but use the obtained NSAttributedString result, such as attributedString, and use it, attributedString.string calculation.

attributedString.string is the string actually displayed on the interface

contact author

Looking forward to your likes and attention! In case of doubt, contact the author.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_39404995/article/details/113183255