2017-02-27 2 views
0

dummyString ist mein HTML-Inhalt, der als Zeichenfolge gespeichert wird. Dann verwende ich,Schriftgröße der analysierten HTML-Zeichenfolge erhöhen - Objective C

dummyString = [NSString stringWithFormat:@"<span style=\"font-family: HelveticaNeue-Thin; font-size: 18\">%@</span>", dummyString]; 

, um die Attribute festzulegen (18 ist meine Standardschriftgröße).

Dann verwende ich folgende Codezeile, um HTML-Inhalt zu parsen, der in UIScrollView dargestellt werden soll.

NSMutableAttributedString *dummy = [[NSMutableAttributedString alloc] 
           initWithData: [dummyString dataUsingEncoding:NSUnicodeStringEncoding] 
           options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } 
           documentAttributes: nil 
           error: nil 
           ]; 

Wenn ich die Schriftgröße von Standard (18) auf eine andere Größe (etwa 36), die Größe des Textes nicht erhöht, anstatt den Abstand der Buchstaben zu. Addiert man die Screenshots:

font-size == 18

font-size == 36

Ich bin nicht in der Lage zu verstehen, wo ich falsch mache!

+0

dummyString = [dummyString stringByAppendingString: @ ""]; – iOS

+0

wo laden Sie HTML-String in der Webansicht oder Textansicht –

+0

Laden in TextView –

Antwort

0

Ich habe dies in swift geschrieben. Ich entferne die alte Schriftart und tende meine eigene in der ganzen Reihe.

let dummyString = NSString(format:"<span style=\"font-family: HelveticaNeue-Thin; font-size: 9\">hi this string</span>") 

    do{ 
     let attributedString = try NSMutableAttributedString.init(data:dummyString.data(using: String.Encoding.unicode.rawValue)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes:nil) 

     attributedString.beginEditing() 
     attributedString.enumerateAttribute(NSFontAttributeName, in:NSMakeRange(0,attributedString.length), options: NSAttributedString.EnumerationOptions(rawValue: 0), using: { (vaue, range, stop) in 
      attributedString .removeAttribute(NSFontAttributeName, range:range) 
      // you can use any font 
      let newFont = UIFont.boldSystemFont(ofSize:25) 
      attributedString.addAttribute(NSFontAttributeName, value: newFont, range:range) 
     }) 
     attributedString.endEditing() 
    textView.attributedText = attributedString 
    }catch{ 
    } 
} 

hoffe, das hilft :)

0
@implementation UILabel (HTML) 

- (void)jaq_setHTMLFromString:(NSString *)string { 

string = [string stringByAppendingString:[NSString stringWithFormat:@" <style>body{font-family: '%@'; font-size:%fpx;}</style>", 
              self.font.fontName, 
              self.font.pointSize]]; 
self.attributedText = [[NSAttributedString alloc] initWithData:[string dataUsingEncoding:NSUnicodeStringEncoding] 
                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 
                   NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
              documentAttributes:nil 
                 error:nil]; 
} 
+0

Gleiches Ergebnis! Keine Änderung –

Verwandte Themen