iOS sets the font and size of html rich text

Just add font style
NSString *preString = [NSString stringWithFormat:@“<span style="color:#%@; font-size:%@; font-family: PingFangSC-Regular, PingFang SC”>”, defaultColor , [NSString stringWithFormat:@“%f”,14 * rectScale()]];
NSString *suffixString = @“”;

directly on the code


+(NSAttributedString *)returnFontColorAttributeString:(NSString *)string WithLineHeight:(CGFloat) lineHeight Font:(UIFont *)font Color:(NSString *)defaultColor {
    if (isBlankString(string)) {
        return nil;
    }
    NSString *preString = [NSString stringWithFormat:@"<span style=\"color:#%@; font-size:%@; font-family: PingFangSC-Regular, PingFang SC\">",defaultColor, [NSString stringWithFormat:@"%f",14 * rectScale()]];
    NSString *suffixString = @"</span>";
    NSString *newString = [[preString stringByAppendingString:string] stringByAppendingString:suffixString];
    
    NSDictionary *dic = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[newString dataUsingEncoding:NSUnicodeStringEncoding] options:dic documentAttributes:nil error:nil];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.maximumLineHeight = lineHeight;
    paragraphStyle.minimumLineHeight = lineHeight;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.paragraphSpacing = 1;
    
   // paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                paragraphStyle,NSParagraphStyleAttributeName,
//                                font,NSFontAttributeName,
                                @0.0f,NSBaselineOffsetAttributeName,
                                nil];
    [attrStr addAttributes:attributes range:NSMakeRange(0, attrStr.length)];
    return attrStr;
}


Guess you like

Origin blog.csdn.net/LIUXIAOXIAOBO/article/details/130497146