Generate the page of HTML dynamically

Exercises 2 and 3 of the previous project ended with a String containing a page of HTML displayed in a UIWebView.

		let html: String = /* etc. */;
		webView.loadHTMLString(html, baseURL: nil);

The following are examples of HTML created dynamically.

Things to try

  1. Insert the following loops into the application(_:didFinishLaunchingWithOptions:) method of the application delegate of the previous project.
    		for familyName in UIFont.familyNames() {
    			print("\(familyName)");
    		}
    
    Marion
    Copperplate
    Heiti SC
    Iowan Old Style
    Courier New
    etc.
    

    See Closure Expressions.

    		var familyNames: [String] = UIFont.familyNames() as! [String];
    		familyNames.sort({$0 < $1});	//parentheses unnecessary
    
    		for familyName in familyNames {
    			print("\(familyName)");
    		}
    
    		for familyName in (UIFont.familyNames() as! [String]).sorted({$0 < $1}) {
    			print("\(familyName)");
    		}
    
    Academy Engraved LET
    Al Nile
    American Typewriter
    Apple Color Emoji
    Apple SD Gothic Neo
    

  2. Create the html string as follows.
    		var html: String = "";
    
    		for familyName in (UIFont.familyNames() as! [String]).sorted({$0 < $1}) {
    			html += "<BR><BR><SPAN STYLE = \"font-family: \(familyName);\">\(familyName)\n"
    				+ "<BR>Pack my box with five dozen liqueur jugs.</SPAN>\n";
    		}
    
    		html = "<HTML>\n"
    			+ "<HEAD>\n"
    
    			+ "<META "
    			+ "NAME = \"viewport\" "
    			+ "CONTENT = \"initial-scale = 1.0, width = device-width\">\n"
    		
    			+ "</HEAD>\n\n"
    			+ "<BODY>\(html)</BODY>\n"
    			+ "</HTML>\n";
    
    		print("\(html)");
    		webView.loadHTMLString(html, baseURL: nil);
    
    <HTML>
    <HEAD>
    <META NAME = "viewport" CONTENT = "initial-scale = 1.0, width = device-width">
    </HEAD>
    
    <BODY><BR><BR><SPAN STYLE = "font-family: Academy Engraved LET;">Academy Engraved LET
    <BR>Pack my box with five dozen liqueur jugs.</SPAN>
    <BR><BR><SPAN STYLE = "font-family: Al Nile;">Al Nile
    <BR>Pack my box with five dozen liqueur jugs.</SPAN>
    <BR><BR><SPAN STYLE = "font-family: American Typewriter;">American Typewriter
    etc.
    </BODY>
    </HTML>
    

  3. Loop through all the letters in a foreign alphabet. See the Unicode code charts. The format %04X prints an integer as a minimum of four hexadecimal digits, padded if necessary with leading zeroes. For Hebrew (LANG = "HE"), loop from 0x05D0 to 0x05EA inclusive. For Arabic (LANG = "AR"), loop from 0x0621 to 0x063A inclusive. For Russian (LANG = "RU"), loop from 0x0410 to 0x044F inclusive.
    		var html: String = "";
    
    		for var i = 0x05D0; i <= 0x05EA; ++i {
    			html += String(format: "&#x%04X;", i);
    		}
    
    		html = "<HTML>\n"
    			+ "<HEAD>\n"
    
    			+ "<META "
    			+ "NAME = \"viewport\" "
    			+ "CONTENT = \"initial-scale = 1.0, width = device-width\">\n"
    		
    			+ "</HEAD>\n\n"
    			+ "<BODY STYLE = \"text-align: right;\"><BR>\n"
    			+ "<SPAN LANG = \"HE\" STYLE = \"font-size: 200%;\">\(html)</SPAN>\n"
    			+ "</BODY>\n"
    			+ "</HTML>\n";
    
    		print("\(html)");
    		webView.loadHTMLString(html, baseURL: nil);
    
    <HTML>
    <HEAD>
    <META NAME = "viewport" CONTENT = "initial-scale = 1.0, width = device-width">
    </HEAD>
    
    <BODY STYLE = "text-align: right;"><BR>
    <SPAN LANG = "HE" STYLE = "font-size: 200%;">&#x05D0;&#x05D1;&#x05D2;&#x05D3;&#x05D4;&#x05D5;&#x05D6;&#x05D7;&#x05D8;&#x05D9;&#x05DA;&#x05DB;&#x05DC;&#x05DD;&#x05DE;&#x05DF;&#x05E0;&#x05E1;&#x05E2;&#x05E3;&#x05E4;&#x05E5;&#x05E6;&#x05E7;&#x05E8;&#x05E9;&#x05EA;</SPAN>
    </BODY>
    </HTML>