iPhone Development - Setting UIWebView font

后端 未结 9 2041
别那么骄傲
别那么骄傲 2020-11-28 04:00

I have to show rich text that i pull from server, so i\'m using UIWebView. Now the problem is that i have no control over the font which is used in UIWebView. How can i chan

相关标签:
9条回答
  • 2020-11-28 04:39

    System Font

    -apple-system on iOS 9 & Safari OS X 10

    font-family: -apple-system;
    font-family: '-apple-system','HelveticaNeue'; // iOS 8 compatible
    
    0 讨论(0)
  • 2020-11-28 04:39

    I made a method that wraps some text in a HTML body with the correct style for a given UIColor and UIFont. It is based on Mustafa answer.

    It is used like this:

    NSString * htmlString = [MyClass htmlFromBodyString:@"an <b>example</b>"
                                               textFont:[UIFont systemFontOfSize:10]
                                              textColor:[UIColor grayColor]];
    [webView loadHTMLString:htmlString baseURL:nil];
    
    0 讨论(0)
  • 2020-11-28 04:41

    I use the following CSS in a web view to do exactly what you are saying.

    body
    {
        font-family:helvetica;
    }
    

    But yours looks the like it should work as well. Strange. Only difference I can see is the font vs font-family, but that shouldn't make a difference.

    chris.

    0 讨论(0)
  • 2020-11-28 04:42

    another easy way that worked for me

        UIFont *font = [UIFont systemFontOfSize:15];
    
          NSString *htmlString = [NSString stringWithFormat:@"<span style=\"font-family:Helvetica; font-size: %i\">%@</span>",
                         (int) font.pointSize,
                         [bodyText stringWithNewLinesAsBRs]];
           [myWebView loadHTMLString:htmlString baseURL:nil];
    
    0 讨论(0)
  • 2020-11-28 04:46

    Here is my easy to use and easy to expand solution with more font settings:

                    myHTMLLabel = [[UIWebView alloc] initWithFrame:CGRectMake(myX,myY,myWidth,myHeight)];
    
                    myHTMLLabel.userInteractionEnabled=NO;
                    myHTMLLabel.scalesPageToFit=NO;
    
                    [myHTMLLabel setOpaque:NO];
                    myHTMLLabel.backgroundColor = myBackColor;
    
                    NSString *myHTMLText = [NSString stringWithFormat:@"<html>"
                                            "<head><style type='text/css'>"
                                            ".main_text {"
                                            "   display: block;"
                                            "   font-family:[fontName];"
                                            "   text-decoration:none;"
                                            "   font-size:[fontSize]px;"
                                            "   color:[fontColor];"
                                            "   line-height: [fontSize]px;"
                                            "   font-weight:normal;"
                                            "   text-align:[textAlign];"
                                            "}"
                                            "</style></head>"
                                            "<body> <SPAN class='main_text'>[text]</SPAN></body></html>"];
    
                    myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[text]" withString: myText];
                    myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontName]" withString: myFontName];
                    myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontSize]" withString: myFontSize];           
                    myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontColor]" withString: myFontColorHex];
                    myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[textAlign]" withString: myFontAlign];
    
    
                    NSLog(@"*** renderHTMLText --> myHTMLText: %@",myHTMLText);
    
                    [myHTMLLabel loadHTMLString:myHTMLText baseURL:nil];
    
    0 讨论(0)
  • 2020-11-28 04:50

    It worked when i modified the string like this. Your right chris, it shouldn't make any difference.

    NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
                                    "<head> \n"
                                    "<style type=\"text/css\"> \n"
                                    "body {font-family: \"%@\"; font-size: %@;}\n"
                                    "</style> \n"
                                    "</head> \n"
                                    "<body>%@</body> \n"
                                    "</html>", @"helvetica", [NSNumber numberWithInt:kFieldFontSize], content];
    
    0 讨论(0)
提交回复
热议问题