Permalink
Browse files

Added possibility to add custom regular expressions.

Signed-off-by: Jan-Rixt Van Hoye <jixt@burntide.eu>
  • Loading branch information...
1 parent 5b77c1a commit c51d27dc253d145bb1dad560de7349f5085bd24d @jixt committed Sep 8, 2011
View
7 AttributedLabel Example/Classes/AttributedLabel_ExampleAppDelegate.m
@@ -113,7 +113,12 @@ -(IBAction)fillLabel2 {
label2.textAlignment = UITextAlignmentCenter;
// "Hello World!" will be displayed in the label, justified, "Hello" in red and " World!" in gray.
label2.automaticallyAddLinksForType = NSTextCheckingTypeDate|NSTextCheckingTypeAddress|NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber;
-
+ // Regular expressions
+ NSError *error = nil;
+ NSRegularExpression *fromExpression = [[[NSRegularExpression alloc] initWithPattern:@"(@[a-zA-Z0-9_]+)" options:NSRegularExpressionCaseInsensitive error:&error] autorelease];
+ NSRegularExpression *tagExpression = [[[NSRegularExpression alloc] initWithPattern:@"(#[a-zA-Z0-9_-]+)" options:NSRegularExpressionCaseInsensitive error:&error] autorelease];
+ [label2 addCustomRegularExpression:fromExpression inRange:NSMakeRange(0, [label2.text length])];
+ [label2 addCustomRegularExpression:tagExpression inRange:NSMakeRange(0, [label2.text length])];
[attrStr release];
}
View
14 AttributedLabel Example/MainWindow.xib
@@ -2,13 +2,13 @@
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="7.10">
<data>
<int key="IBDocument.SystemTarget">1056</int>
- <string key="IBDocument.SystemVersion">10K549</string>
- <string key="IBDocument.InterfaceBuilderVersion">823</string>
+ <string key="IBDocument.SystemVersion">10K540</string>
+ <string key="IBDocument.InterfaceBuilderVersion">851</string>
<string key="IBDocument.AppKitVersion">1038.36</string>
<string key="IBDocument.HIToolboxVersion">461.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
- <string key="NS.object.0">132</string>
+ <string key="NS.object.0">141</string>
</object>
<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -92,9 +92,9 @@
<int key="IBUIContentMode">7</int>
<bool key="IBUIUserInteractionEnabled">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
- <string type="base64-UTF8" key="IBUIText">VmlzaXQgaHR0cDovL3d3dy5mb29kcmVwb3J0ZXIubmV0IHRvIHBvc3QgeW91ciBmb29kIQoKSSBzaG91
-bGQgY2FsbCBBcHBsZSBpbiBDdXBlcnRpbm8sIENhbGlmb3JuaWEgKCsxIDQwOC05OTYtMTAxMCkgbmV4
-dCBzdW5kYXkuA</string>
+ <string type="base64-UTF8" key="IBUIText">VmlzaXQgaHR0cDovL3d3dy5mb29kcmVwb3J0ZXIubmV0IHRvIHBvc3QgeW91ciBmb29kIQpJIHNob3Vs
+ZCBjYWxsIEFwcGxlIGluIEN1cGVydGlubywgQ2FsaWZvcm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0
+IHN1bmRheS4gCkBmb29kcmVwb3J0ZXIgI2Zvb2RyZXBvcnRlcg</string>
<object class="NSFont" key="IBUIFont" id="605141269">
<string key="NSName">Helvetica</string>
<double key="NSSize">14</double>
@@ -1035,6 +1035,6 @@ dCBzdW5kYXkuA</string>
<bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
<string key="IBDocument.LastKnownRelativeProjectPath">AttributedLabel Example.xcodeproj</string>
<int key="IBDocument.defaultPropertyAccessControl">3</int>
- <string key="IBCocoaTouchPluginVersion">132</string>
+ <string key="IBCocoaTouchPluginVersion">141</string>
</data>
</archive>
View
4 OHAttributedLabel.h
@@ -61,6 +61,7 @@ CTLineBreakMode CTLineBreakModeFromUILineBreakMode(UILineBreakMode lineBreakMode
CTFrameRef textFrame;
CGRect drawingRect;
NSMutableArray* customLinks;
+ NSMutableArray* customRegularExpressions;
NSTextCheckingResult* activeLink;
}
@@ -73,8 +74,11 @@ CTLineBreakMode CTLineBreakModeFromUILineBreakMode(UILineBreakMode lineBreakMode
@property(nonatomic, retain) UIColor* linkColor; //!< Defaults to [UIColor blueColor]. See also OHAttributedLabelDelegate
@property(nonatomic, retain) UIColor* highlightedLinkColor; //[UIColor colorWithWhite:0.2 alpha:0.5]
@property(nonatomic, assign) BOOL underlineLinks; //!< Defaults to YES. See also OHAttributedLabelDelegate
+
-(void)addCustomLink:(NSURL*)linkUrl inRange:(NSRange)range;
+-(void)addCustomRegularExpression:(NSRegularExpression *)expression inRange:(NSRange)range;
-(void)removeAllCustomLinks;
+-(void)removeAllRegularExpressions;
@property(nonatomic, assign) BOOL onlyCatchTouchesOnLinks; //!< If YES, pointInside will only return YES if the touch is on a link. If NO, pointInside will always return YES (Defaults to NO)
@property(nonatomic, assign) IBOutlet id<OHAttributedLabelDelegate> delegate;
View
50 OHAttributedLabel.m
@@ -165,6 +165,7 @@ @implementation OHAttributedLabel
- (void)commonInit
{
customLinks = [[NSMutableArray alloc] init];
+ customRegularExpressions = [[NSMutableArray alloc] init];
self.linkColor = [UIColor blueColor];
self.highlightedLinkColor = [UIColor colorWithWhite:0.4 alpha:0.3];
self.underlineLinks = YES;
@@ -202,6 +203,7 @@ -(void)dealloc
[self resetTextFrame];
[customLinks release];
+ [customRegularExpressions release];
self.linkColor = nil;
self.highlightedLinkColor = nil;
[activeLink release];
@@ -221,10 +223,21 @@ -(void)addCustomLink:(NSURL*)linkUrl inRange:(NSRange)range {
[customLinks addObject:link];
[self setNeedsDisplay];
}
+-(void)addCustomRegularExpression:(NSRegularExpression *)expression inRange:(NSRange)range{
+ [expression enumerateMatchesInString:self.text options:0 range:NSMakeRange(0, [self.text length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
+ [customRegularExpressions addObject:match];
+ }];
+
+ [self setNeedsDisplay];
+}
-(void)removeAllCustomLinks {
[customLinks removeAllObjects];
[self setNeedsDisplay];
}
+-(void)removeAllRegularExpressions {
+ [customRegularExpressions removeAllObjects];
+ [self setNeedsDisplay];
+}
-(NSMutableAttributedString*)attributedTextWithLinks {
NSMutableAttributedString* str = [self.attributedText mutableCopy];
@@ -247,6 +260,7 @@ -(NSMutableAttributedString*)attributedTextWithLinks {
[str setTextUnderlineStyle:uStyle range:[result range]];
}];
}
+ // Custom links
[customLinks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
NSTextCheckingResult* result = (NSTextCheckingResult*)obj;
@@ -270,6 +284,30 @@ -(NSMutableAttributedString*)attributedTextWithLinks {
}
}
}];
+ // Custom regular expressions
+ [customRegularExpressions enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
+ {
+ NSTextCheckingResult* result = (NSTextCheckingResult*)obj;
+
+ int32_t uStyle = self.underlineLinks ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone;
+ UIColor* thisLinkColor = (self.delegate && [self.delegate respondsToSelector:@selector(colorForLink:underlineStyle:)])
+ ? [self.delegate colorForLink:result underlineStyle:&uStyle] : self.linkColor;
+
+ @try {
+ if (thisLinkColor)
+ [str setTextColor:thisLinkColor range:[result range]];
+ if (uStyle>0)
+ [str setTextUnderlineStyle:uStyle range:[result range]];
+ }
+ @catch (NSException * e) {
+ // Protection against NSRangeException
+ if ([[e name] isEqualToString:NSRangeException]) {
+ NSLog(@"[OHAttributedLabel] exception: %@",e);
+ } else {
+ @throw;
+ }
+ }
+ }];
return [str autorelease];
}
@@ -291,7 +329,7 @@ -(NSTextCheckingResult*)linkAtCharacterIndex:(CFIndex)idx {
}];
if (foundResult) return foundResult;
}
-
+ // Custom links
[customLinks enumerateObjectsUsingBlock:^(id obj, NSUInteger aidx, BOOL *stop)
{
NSRange r = [(NSTextCheckingResult*)obj range];
@@ -300,6 +338,15 @@ -(NSTextCheckingResult*)linkAtCharacterIndex:(CFIndex)idx {
*stop = YES;
}
}];
+ // Custom regular expressions
+ [customRegularExpressions enumerateObjectsUsingBlock:^(id obj, NSUInteger aidx, BOOL *stop)
+ {
+ NSRange r = [(NSTextCheckingResult*)obj range];
+ if (NSLocationInRange(idx, r)) {
+ foundResult = [[obj retain] autorelease];
+ *stop = YES;
+ }
+ }];
return foundResult;
}
@@ -548,6 +595,7 @@ -(void)setAttributedText:(NSAttributedString*)attributedText {
[_attributedText release];
_attributedText = [attributedText mutableCopy];
[self removeAllCustomLinks];
+ [self removeAllRegularExpressions];
[self setNeedsDisplay];
}

0 comments on commit c51d27d

Please sign in to comment.