Skip to content

Commit

Permalink
Added some basic HTML and markup tag support to make the creation of …
Browse files Browse the repository at this point in the history
…an NSAttributedString easier

(request #88)
  • Loading branch information
AliSoftware committed Sep 26, 2012
1 parent bb5abd0 commit e97f511
Show file tree
Hide file tree
Showing 10 changed files with 526 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@interface BasicDemoViewController : UIViewController <OHAttributedLabelDelegate>

@property(nonatomic, retain) IBOutlet OHAttributedLabel* demoLabel;
@property(retain, nonatomic) IBOutlet OHAttributedLabel* htmlLabel;
@property(retain, nonatomic) IBOutlet OHAttributedLabel* basicMarkupLabel;

-(IBAction)fillDemoLabel;
-(IBAction)changeHAlignment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
#import "BasicDemoViewController.h"
#import "NSAttributedString+Attributes.h"
#import "UIAlertView+Commodity.h"
#import "OHASTagParserHTML.h"
#import "OHASTagParserBasicMarkup.h"

@interface BasicDemoViewController ()
@property(nonatomic, retain) NSMutableSet* visitedLinks;
@end

@implementation BasicDemoViewController
@synthesize demoLabel = _demoLabel;
@synthesize htmlLabel = _htmlLabel;
@synthesize basicMarkupLabel = _basicMarkupLabel;
@synthesize visitedLinks = _visitedLinks;

/////////////////////////////////////////////////////////////////////////////
Expand All @@ -38,19 +42,25 @@ -(void)dealloc
{
[_visitedLinks release];
[_demoLabel release];
[_htmlLabel release];
[_basicMarkupLabel release];
[super dealloc];
}
#endif

-(void)viewDidLoad
{
[self fillDemoLabel];
self.htmlLabel.attributedText = [OHASTagParserHTML attributedStringByReplacingTagsInAttributedString:self.htmlLabel.attributedText];
self.basicMarkupLabel.attributedText = [OHASTagParserBasicMarkup attributedStringByReplacingTagsInAttributedString:self.basicMarkupLabel.attributedText];
[super viewDidLoad];
}

-(void)viewDidUnload
{
[super viewDidUnload];
self.htmlLabel = nil;
self.basicMarkupLabel = nil;
self.demoLabel = nil;
}

Expand All @@ -68,6 +78,7 @@ -(IBAction)fillDemoLabel

/**(1)** Build the NSAttributedString *******/
NSMutableAttributedString* attrStr = [self.demoLabel.attributedText mutableCopy];

[attrStr setTextAlignment:kCTCenterTextAlignment lineBreakMode:kCTLineBreakByWordWrapping];
// and only change the color of "Hello"
[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(26,5)];
Expand Down
164 changes: 133 additions & 31 deletions AttributedLabel Example/Classes/BasicDemo/BasicDemoViewController.xib
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="8.00">
<data>
<int key="IBDocument.SystemTarget">1296</int>
<string key="IBDocument.SystemVersion">12B19</string>
<string key="IBDocument.InterfaceBuilderVersion">2549</string>
<string key="IBDocument.AppKitVersion">1187</string>
<string key="IBDocument.HIToolboxVersion">624.00</string>
<int key="IBDocument.SystemTarget">1536</int>
<string key="IBDocument.SystemVersion">12C54</string>
<string key="IBDocument.InterfaceBuilderVersion">2840</string>
<string key="IBDocument.AppKitVersion">1187.34</string>
<string key="IBDocument.HIToolboxVersion">625.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="NS.object.0">1498</string>
<string key="NS.object.0">1926</string>
</object>
<array key="IBDocument.IntegratedClassDependencies">
<string>IBProxyObject</string>
Expand Down Expand Up @@ -39,7 +39,7 @@
<object class="IBUILabel" id="317098620">
<reference key="NSNextResponder" ref="818567079"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{10, 54}, {300, 130}}</string>
<string key="NSFrame">{{10, 20}, {300, 130}}</string>
<reference key="NSSuperview" ref="818567079"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="418453974"/>
Expand All @@ -58,6 +58,7 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<object class="NSColor" key="IBUITextColor" id="613463035">
<int key="NSColorSpace">1</int>
<bytes key="NSRGB">MCAwIDAAA</bytes>
<string key="IBUIColorCocoaTouchKeyPath">darkTextColor</string>
</object>
<object class="NSColor" key="IBUIHighlightedColor" id="688241914">
<int key="NSColorSpace">3</int>
Expand All @@ -79,11 +80,12 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<int key="NSfFlags">16</int>
</object>
<bool key="IBUIAdjustsFontSizeToFit">NO</bool>
<double key="preferredMaxLayoutWidth">300</double>
</object>
<object class="IBUIButton" id="418453974">
<reference key="NSNextResponder" ref="818567079"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{10, 214}, {64, 37}}</string>
<string key="NSFrame">{{10, 158}, {64, 37}}</string>
<reference key="NSSuperview" ref="818567079"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="140541167"/>
Expand Down Expand Up @@ -117,7 +119,7 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<object class="IBUIButton" id="140541167">
<reference key="NSNextResponder" ref="818567079"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{82, 214}, {75, 37}}</string>
<string key="NSFrame">{{82, 158}, {75, 37}}</string>
<reference key="NSSuperview" ref="818567079"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="436418902"/>
Expand All @@ -139,10 +141,10 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<object class="IBUIButton" id="867269979">
<reference key="NSNextResponder" ref="818567079"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{248, 214}, {62, 37}}</string>
<string key="NSFrame">{{248, 158}, {62, 37}}</string>
<reference key="NSSuperview" ref="818567079"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="179511706"/>
<reference key="NSNextKeyView" ref="473412375"/>
<bool key="IBUIOpaque">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<int key="IBUIContentHorizontalAlignment">0</int>
Expand All @@ -161,7 +163,7 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<object class="IBUIButton" id="436418902">
<reference key="NSNextResponder" ref="818567079"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{165, 214}, {75, 37}}</string>
<string key="NSFrame">{{165, 158}, {75, 37}}</string>
<reference key="NSSuperview" ref="818567079"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="867269979"/>
Expand All @@ -183,7 +185,7 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<object class="IBUILabel" id="179511706">
<reference key="NSNextResponder" ref="818567079"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{10, 290}, {300, 45}}</string>
<string key="NSFrame">{{10, 320}, {300, 45}}</string>
<reference key="NSSuperview" ref="818567079"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="432049123"/>
Expand All @@ -209,11 +211,12 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<double key="NSSize">12</double>
<int key="NSfFlags">16</int>
</object>
<double key="preferredMaxLayoutWidth">300</double>
</object>
<object class="IBUIButton" id="432049123">
<reference key="NSNextResponder" ref="818567079"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{10, 335}, {300, 27}}</string>
<string key="NSFrame">{{10, 365}, {300, 27}}</string>
<reference key="NSSuperview" ref="818567079"/>
<reference key="NSWindow"/>
<bool key="IBUIOpaque">NO</bool>
Expand All @@ -234,12 +237,71 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<int key="traits">0</int>
<double key="pointSize">12</double>
</object>
<object class="NSFont" key="IBUIFont">
<object class="NSFont" key="IBUIFont" id="830814871">
<string key="NSName">Helvetica</string>
<double key="NSSize">12</double>
<int key="NSfFlags">16</int>
</object>
</object>
<object class="IBUILabel" id="473412375">
<reference key="NSNextResponder" ref="818567079"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{10, 202}, {300, 72}}</string>
<reference key="NSSuperview" ref="818567079"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="686639102"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<object class="NSColor" key="IBUIBackgroundColor">
<int key="NSColorSpace">1</int>
<bytes key="NSRGB">MSAxIDAuODAzNTg0OTI5NAA</bytes>
</object>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
<int key="IBUIContentMode">7</int>
<bool key="IBUIUserInteractionEnabled">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<string key="IBUIText">Some &lt;b&gt;basic&lt;/b&gt; &lt;font name="Courier" size="14"&gt;HTML&lt;/font&gt; support is provided for &lt;u&gt;convenience&lt;/u&gt; too. &lt;font color="red"&gt;You can add your own parsers easily&lt;/font&gt; by subclassing &lt;font name="Courier" size="14"&gt;OHASTagParserBase&lt;/font&gt; if needed.</string>
<reference key="IBUITextColor" ref="613463035"/>
<nil key="IBUIHighlightedColor"/>
<int key="IBUIBaselineAdjustment">0</int>
<int key="IBUINumberOfLines">0</int>
<int key="IBUILineBreakMode">0</int>
<object class="IBUIFontDescription" key="IBUIFontDescription" id="93224958">
<int key="type">1</int>
<double key="pointSize">12</double>
</object>
<reference key="IBUIFont" ref="830814871"/>
<bool key="IBUIAdjustsFontSizeToFit">NO</bool>
<double key="preferredMaxLayoutWidth">300</double>
</object>
<object class="IBUILabel" id="686639102">
<reference key="NSNextResponder" ref="818567079"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{10, 282}, {300, 41}}</string>
<reference key="NSSuperview" ref="818567079"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="179511706"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<object class="NSColor" key="IBUIBackgroundColor">
<int key="NSColorSpace">1</int>
<bytes key="NSRGB">MSAxIDAuOAA</bytes>
</object>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
<int key="IBUIContentMode">7</int>
<bool key="IBUIUserInteractionEnabled">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<string key="IBUIText">Some *basic* `Markup` is _also_ provided. {red|cool, isn't it?} {#80c|Enjoy}!</string>
<reference key="IBUITextColor" ref="613463035"/>
<nil key="IBUIHighlightedColor"/>
<int key="IBUIBaselineAdjustment">0</int>
<int key="IBUINumberOfLines">0</int>
<int key="IBUILineBreakMode">0</int>
<reference key="IBUIFontDescription" ref="93224958"/>
<reference key="IBUIFont" ref="830814871"/>
<bool key="IBUIAdjustsFontSizeToFit">NO</bool>
<double key="preferredMaxLayoutWidth">300</double>
</object>
</array>
<string key="NSFrame">{{0, 20}, {320, 411}}</string>
<reference key="NSSuperview"/>
Expand Down Expand Up @@ -276,6 +338,22 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
</object>
<int key="connectionID">21</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">htmlLabel</string>
<reference key="source" ref="372490531"/>
<reference key="destination" ref="473412375"/>
</object>
<int key="connectionID">23</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">basicMarkupLabel</string>
<reference key="source" ref="372490531"/>
<reference key="destination" ref="686639102"/>
</object>
<int key="connectionID">25</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchEventConnection" key="connection">
<string key="label">resetVisitedLinks</string>
Expand Down Expand Up @@ -353,13 +431,15 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<int key="objectID">3</int>
<reference key="object" ref="818567079"/>
<array class="NSMutableArray" key="children">
<reference ref="179511706"/>
<reference ref="432049123"/>
<reference ref="867269979"/>
<reference ref="140541167"/>
<reference ref="418453974"/>
<reference ref="317098620"/>
<reference ref="418453974"/>
<reference ref="140541167"/>
<reference ref="867269979"/>
<reference ref="436418902"/>
<reference ref="179511706"/>
<reference ref="432049123"/>
<reference ref="473412375"/>
<reference ref="686639102"/>
</array>
<reference key="parent" ref="0"/>
</object>
Expand Down Expand Up @@ -398,6 +478,16 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<reference key="object" ref="317098620"/>
<reference key="parent" ref="818567079"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">22</int>
<reference key="object" ref="473412375"/>
<reference key="parent" ref="818567079"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">24</int>
<reference key="object" ref="686639102"/>
<reference key="parent" ref="818567079"/>
</object>
</array>
</object>
<dictionary class="NSMutableDictionary" key="flattenedProperties">
Expand All @@ -407,6 +497,10 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<string key="-2.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="10.CustomClassName">OHAttributedLabel</string>
<string key="10.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="22.CustomClassName">OHAttributedLabel</string>
<string key="22.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="24.CustomClassName">OHAttributedLabel</string>
<string key="24.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="3.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="4.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="5.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
Expand All @@ -419,7 +513,7 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<nil key="activeLocalization"/>
<dictionary class="NSMutableDictionary" key="localizations"/>
<nil key="sourceID"/>
<int key="maxID">21</int>
<int key="maxID">25</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<array class="NSMutableArray" key="referencedPartialClassDescriptions">
Expand Down Expand Up @@ -455,17 +549,25 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<string key="candidateClassName">id</string>
</object>
</dictionary>
<object class="NSMutableDictionary" key="outlets">
<string key="NS.key.0">demoLabel</string>
<string key="NS.object.0">OHAttributedLabel</string>
</object>
<object class="NSMutableDictionary" key="toOneOutletInfosByName">
<string key="NS.key.0">demoLabel</string>
<object class="IBToOneOutletInfo" key="NS.object.0">
<dictionary class="NSMutableDictionary" key="outlets">
<string key="basicMarkupLabel">OHAttributedLabel</string>
<string key="demoLabel">OHAttributedLabel</string>
<string key="htmlLabel">OHAttributedLabel</string>
</dictionary>
<dictionary class="NSMutableDictionary" key="toOneOutletInfosByName">
<object class="IBToOneOutletInfo" key="basicMarkupLabel">
<string key="name">basicMarkupLabel</string>
<string key="candidateClassName">OHAttributedLabel</string>
</object>
<object class="IBToOneOutletInfo" key="demoLabel">
<string key="name">demoLabel</string>
<string key="candidateClassName">OHAttributedLabel</string>
</object>
</object>
<object class="IBToOneOutletInfo" key="htmlLabel">
<string key="name">htmlLabel</string>
<string key="candidateClassName">OHAttributedLabel</string>
</object>
</dictionary>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">./Classes/BasicDemoViewController.h</string>
Expand Down Expand Up @@ -496,10 +598,10 @@ cm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0IHN1bmRheS4</string>
<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">
<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS</string>
<real value="1296" key="NS.object.0"/>
<real value="1536" key="NS.object.0"/>
</object>
<bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
<int key="IBDocument.defaultPropertyAccessControl">3</int>
<string key="IBCocoaTouchPluginVersion">1498</string>
<string key="IBCocoaTouchPluginVersion">1926</string>
</data>
</archive>
Loading

0 comments on commit e97f511

Please sign in to comment.