Skip to content

Commit 386d56d

Browse files
author
James Tang
committed
Render HTML using UILabel and UITextView
0 parents  commit 386d56d

17 files changed

Lines changed: 1023 additions & 0 deletions

File tree

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Created by http://www.gitignore.io
2+
3+
### Xcode ###
4+
build
5+
*.xcodeproj/*
6+
!*.xcodeproj/project.pbxproj
7+
!*.xcworkspace/contents.xcworkspacedata
8+
9+
10+
### Objective-C ###
11+
# OS X
12+
.DS_Store
13+
14+
# Xcode
15+
build/
16+
*.pbxuser
17+
!default.pbxuser
18+
*.mode1v3
19+
!default.mode1v3
20+
*.mode2v3
21+
!default.mode2v3
22+
*.perspectivev3
23+
!default.perspectivev3
24+
xcuserdata
25+
*.xccheckout
26+
profile
27+
*.moved-aside
28+
DerivedData
29+
*.hmap
30+
*.ipa
31+
32+
# CocoaPods
33+
Pods
34+

htmlString.xcodeproj/project.pbxproj

Lines changed: 495 additions & 0 deletions
Large diffs are not rendered by default.

htmlString/AppDelegate.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// AppDelegate.h
3+
// htmlString
4+
//
5+
// Created by James Tang on 13/3/14.
6+
// Copyright (c) 2014 James Tang. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface AppDelegate : UIResponder <UIApplicationDelegate>
12+
13+
@property (strong, nonatomic) UIWindow *window;
14+
15+
@end

htmlString/AppDelegate.m

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// AppDelegate.m
3+
// htmlString
4+
//
5+
// Created by James Tang on 13/3/14.
6+
// Copyright (c) 2014 James Tang. All rights reserved.
7+
//
8+
9+
#import "AppDelegate.h"
10+
11+
@implementation AppDelegate
12+
13+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
14+
{
15+
// Override point for customization after application launch.
16+
return YES;
17+
}
18+
19+
- (void)applicationWillResignActive:(UIApplication *)application
20+
{
21+
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
22+
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
23+
}
24+
25+
- (void)applicationDidEnterBackground:(UIApplication *)application
26+
{
27+
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
28+
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
29+
}
30+
31+
- (void)applicationWillEnterForeground:(UIApplication *)application
32+
{
33+
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
34+
}
35+
36+
- (void)applicationDidBecomeActive:(UIApplication *)application
37+
{
38+
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
39+
}
40+
41+
- (void)applicationWillTerminate:(UIApplication *)application
42+
{
43+
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
44+
}
45+
46+
@end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5023" systemVersion="13A603" targetRuntime="iOS.CocoaTouch.iPad" propertyAccessControl="none" useAutolayout="YES" initialViewController="BYZ-38-t0r">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
5+
</dependencies>
6+
<scenes>
7+
<!--class Prefix:identifier View Controller-->
8+
<scene sceneID="tne-QT-ifu">
9+
<objects>
10+
<viewController id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
11+
<layoutGuides>
12+
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
13+
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
14+
</layoutGuides>
15+
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
16+
<rect key="frame" x="0.0" y="0.0" width="768" height="1024"/>
17+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
18+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
19+
</view>
20+
</viewController>
21+
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
22+
</objects>
23+
</scene>
24+
</scenes>
25+
<simulatedMetricsContainer key="defaultSimulatedMetrics">
26+
<simulatedStatusBarMetrics key="statusBar"/>
27+
<simulatedOrientationMetrics key="orientation"/>
28+
<simulatedScreenMetrics key="destination"/>
29+
</simulatedMetricsContainer>
30+
</document>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVersion="13C64" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
5+
</dependencies>
6+
<scenes>
7+
<!--View Controller-->
8+
<scene sceneID="ufC-wZ-h7g">
9+
<objects>
10+
<viewController id="vXZ-lx-hvc" customClass="ViewController" sceneMemberID="viewController">
11+
<layoutGuides>
12+
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
13+
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
14+
</layoutGuides>
15+
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
16+
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
17+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
18+
<subviews>
19+
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="uQs-U7-fNw">
20+
<rect key="frame" x="0.0" y="20" width="320" height="174"/>
21+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
22+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
23+
<string key="text">&lt;head&gt;
24+
&lt;style&gt;
25+
a {
26+
text-decoration:none;
27+
}
28+
body {
29+
vertical-align: middle;
30+
margin-top:10px;
31+
}
32+
&lt;/style&gt;
33+
&lt;/head&gt;
34+
&lt;body&gt;
35+
&lt;a href="http://apple.com" style=""&gt;apple.com&lt;/a&gt;&lt;img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRjLq1-KfX1ucPze9pzdxkeC-Uz4L0V2TsyLmfqEaJ7Hldmy3ZM" style="width:30;" /&gt;
36+
&lt;/body&gt;
37+
&lt;/html&gt;</string>
38+
<fontDescription key="fontDescription" type="system" pointSize="14"/>
39+
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
40+
<connections>
41+
<outlet property="delegate" destination="vXZ-lx-hvc" id="97Z-IA-SYh"/>
42+
</connections>
43+
</textView>
44+
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" editable="NO" translatesAutoresizingMaskIntoConstraints="NO" id="2E1-zn-PeM">
45+
<rect key="frame" x="0.0" y="197" width="162" height="146"/>
46+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
47+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
48+
<string key="text">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>
49+
<fontDescription key="fontDescription" type="system" pointSize="14"/>
50+
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
51+
<dataDetectorType key="dataDetectorTypes" phoneNumber="YES" link="YES" address="YES" calendarEvent="YES"/>
52+
<connections>
53+
<outlet property="delegate" destination="vXZ-lx-hvc" id="fAd-xG-Okk"/>
54+
</connections>
55+
</textView>
56+
<label opaque="NO" clipsSubviews="YES" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="150" translatesAutoresizingMaskIntoConstraints="NO" id="3MK-Nt-jKt">
57+
<rect key="frame" x="170" y="202" width="150" height="21"/>
58+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
59+
<color key="backgroundColor" white="0.66666666666666663" alpha="0.20000000000000001" colorSpace="calibratedWhite"/>
60+
<constraints>
61+
<constraint firstAttribute="width" constant="150" id="Vad-GV-BLS"/>
62+
</constraints>
63+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
64+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
65+
<nil key="highlightedColor"/>
66+
</label>
67+
</subviews>
68+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
69+
<constraints>
70+
<constraint firstAttribute="trailing" secondItem="3MK-Nt-jKt" secondAttribute="trailing" id="DmF-dP-McG"/>
71+
<constraint firstItem="3MK-Nt-jKt" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="182" id="Wwq-MY-TfK"/>
72+
</constraints>
73+
</view>
74+
<connections>
75+
<outlet property="previewLabel" destination="3MK-Nt-jKt" id="hMK-Ps-BUU"/>
76+
<outlet property="previewTextView" destination="2E1-zn-PeM" id="leL-mP-i63"/>
77+
<outlet property="textView" destination="uQs-U7-fNw" id="O5F-EM-tDN"/>
78+
</connections>
79+
</viewController>
80+
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
81+
</objects>
82+
</scene>
83+
</scenes>
84+
<simulatedMetricsContainer key="defaultSimulatedMetrics">
85+
<simulatedStatusBarMetrics key="statusBar"/>
86+
<simulatedOrientationMetrics key="orientation"/>
87+
<simulatedScreenMetrics key="destination" type="retina4"/>
88+
</simulatedMetricsContainer>
89+
</document>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "29x29",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "40x40",
11+
"scale" : "2x"
12+
},
13+
{
14+
"idiom" : "iphone",
15+
"size" : "60x60",
16+
"scale" : "2x"
17+
},
18+
{
19+
"idiom" : "ipad",
20+
"size" : "29x29",
21+
"scale" : "1x"
22+
},
23+
{
24+
"idiom" : "ipad",
25+
"size" : "29x29",
26+
"scale" : "2x"
27+
},
28+
{
29+
"idiom" : "ipad",
30+
"size" : "40x40",
31+
"scale" : "1x"
32+
},
33+
{
34+
"idiom" : "ipad",
35+
"size" : "40x40",
36+
"scale" : "2x"
37+
},
38+
{
39+
"idiom" : "ipad",
40+
"size" : "76x76",
41+
"scale" : "1x"
42+
},
43+
{
44+
"idiom" : "ipad",
45+
"size" : "76x76",
46+
"scale" : "2x"
47+
}
48+
],
49+
"info" : {
50+
"version" : 1,
51+
"author" : "xcode"
52+
}
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"images" : [
3+
{
4+
"orientation" : "portrait",
5+
"idiom" : "iphone",
6+
"extent" : "full-screen",
7+
"minimum-system-version" : "7.0",
8+
"scale" : "2x"
9+
},
10+
{
11+
"orientation" : "portrait",
12+
"idiom" : "iphone",
13+
"subtype" : "retina4",
14+
"extent" : "full-screen",
15+
"minimum-system-version" : "7.0",
16+
"scale" : "2x"
17+
},
18+
{
19+
"orientation" : "portrait",
20+
"idiom" : "ipad",
21+
"extent" : "full-screen",
22+
"minimum-system-version" : "7.0",
23+
"scale" : "1x"
24+
},
25+
{
26+
"orientation" : "landscape",
27+
"idiom" : "ipad",
28+
"extent" : "full-screen",
29+
"minimum-system-version" : "7.0",
30+
"scale" : "1x"
31+
},
32+
{
33+
"orientation" : "portrait",
34+
"idiom" : "ipad",
35+
"extent" : "full-screen",
36+
"minimum-system-version" : "7.0",
37+
"scale" : "2x"
38+
},
39+
{
40+
"orientation" : "landscape",
41+
"idiom" : "ipad",
42+
"extent" : "full-screen",
43+
"minimum-system-version" : "7.0",
44+
"scale" : "2x"
45+
}
46+
],
47+
"info" : {
48+
"version" : 1,
49+
"author" : "xcode"
50+
}
51+
}

htmlString/ViewController.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// ViewController.h
3+
// htmlString
4+
//
5+
// Created by James Tang on 13/3/14.
6+
// Copyright (c) 2014 James Tang. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface ViewController : UIViewController
12+
@property (weak, nonatomic) IBOutlet UITextView *textView;
13+
@property (weak, nonatomic) IBOutlet UILabel *previewLabel;
14+
@property (weak, nonatomic) IBOutlet UITextView *previewTextView;
15+
16+
@end

htmlString/ViewController.m

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// ViewController.m
3+
// htmlString
4+
//
5+
// Created by James Tang on 13/3/14.
6+
// Copyright (c) 2014 James Tang. All rights reserved.
7+
//
8+
9+
#import "ViewController.h"
10+
11+
@interface ViewController () <UITextViewDelegate>
12+
13+
@end
14+
15+
@implementation ViewController
16+
17+
- (void)viewDidLoad
18+
{
19+
[super viewDidLoad];
20+
// Do any additional setup after loading the view, typically from a nib.
21+
[self reloadData];
22+
}
23+
24+
- (void)didReceiveMemoryWarning
25+
{
26+
[super didReceiveMemoryWarning];
27+
// Dispose of any resources that can be recreated.
28+
}
29+
30+
#pragma mark UITextViewDelegate
31+
- (void)textViewDidChange:(UITextView *)textView {
32+
33+
[self reloadData];
34+
}
35+
36+
- (void)reloadData {
37+
NSString *htmlString = self.textView.text;
38+
NSAttributedString *string = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
39+
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
40+
NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
41+
documentAttributes:nil error:nil];
42+
43+
self.previewLabel.text = nil;
44+
self.previewLabel.attributedText = string;
45+
46+
self.previewTextView.attributedText = string;
47+
48+
[self.previewLabel invalidateIntrinsicContentSize];
49+
}
50+
51+
@end

0 commit comments

Comments
 (0)