Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added possibility to add custom regular expressions.

Signed-off-by: Jan-Rixt Van Hoye <jixt@burntide.eu>
  • Loading branch information...
commit c51d27dc253d145bb1dad560de7349f5085bd24d 1 parent 5b77c1a
Jan-Rixt Van Hoye authored
7 AttributedLabel Example/Classes/AttributedLabel_ExampleAppDelegate.m
@@ -113,7 +113,12 @@ -(IBAction)fillLabel2 {
113 113 label2.textAlignment = UITextAlignmentCenter;
114 114 // "Hello World!" will be displayed in the label, justified, "Hello" in red and " World!" in gray.
115 115 label2.automaticallyAddLinksForType = NSTextCheckingTypeDate|NSTextCheckingTypeAddress|NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber;
116   -
  116 + // Regular expressions
  117 + NSError *error = nil;
  118 + NSRegularExpression *fromExpression = [[[NSRegularExpression alloc] initWithPattern:@"(@[a-zA-Z0-9_]+)" options:NSRegularExpressionCaseInsensitive error:&error] autorelease];
  119 + NSRegularExpression *tagExpression = [[[NSRegularExpression alloc] initWithPattern:@"(#[a-zA-Z0-9_-]+)" options:NSRegularExpressionCaseInsensitive error:&error] autorelease];
  120 + [label2 addCustomRegularExpression:fromExpression inRange:NSMakeRange(0, [label2.text length])];
  121 + [label2 addCustomRegularExpression:tagExpression inRange:NSMakeRange(0, [label2.text length])];
117 122 [attrStr release];
118 123 }
119 124
14 AttributedLabel Example/MainWindow.xib
@@ -2,13 +2,13 @@
2 2 <archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="7.10">
3 3 <data>
4 4 <int key="IBDocument.SystemTarget">1056</int>
5   - <string key="IBDocument.SystemVersion">10K549</string>
6   - <string key="IBDocument.InterfaceBuilderVersion">823</string>
  5 + <string key="IBDocument.SystemVersion">10K540</string>
  6 + <string key="IBDocument.InterfaceBuilderVersion">851</string>
7 7 <string key="IBDocument.AppKitVersion">1038.36</string>
8 8 <string key="IBDocument.HIToolboxVersion">461.00</string>
9 9 <object class="NSMutableDictionary" key="IBDocument.PluginVersions">
10 10 <string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
11   - <string key="NS.object.0">132</string>
  11 + <string key="NS.object.0">141</string>
12 12 </object>
13 13 <object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
14 14 <bool key="EncodedWithXMLCoder">YES</bool>
@@ -92,9 +92,9 @@
92 92 <int key="IBUIContentMode">7</int>
93 93 <bool key="IBUIUserInteractionEnabled">NO</bool>
94 94 <string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
95   - <string type="base64-UTF8" key="IBUIText">VmlzaXQgaHR0cDovL3d3dy5mb29kcmVwb3J0ZXIubmV0IHRvIHBvc3QgeW91ciBmb29kIQoKSSBzaG91
96   -bGQgY2FsbCBBcHBsZSBpbiBDdXBlcnRpbm8sIENhbGlmb3JuaWEgKCsxIDQwOC05OTYtMTAxMCkgbmV4
97   -dCBzdW5kYXkuA</string>
  95 + <string type="base64-UTF8" key="IBUIText">VmlzaXQgaHR0cDovL3d3dy5mb29kcmVwb3J0ZXIubmV0IHRvIHBvc3QgeW91ciBmb29kIQpJIHNob3Vs
  96 +ZCBjYWxsIEFwcGxlIGluIEN1cGVydGlubywgQ2FsaWZvcm5pYSAoKzEgNDA4LTk5Ni0xMDEwKSBuZXh0
  97 +IHN1bmRheS4gCkBmb29kcmVwb3J0ZXIgI2Zvb2RyZXBvcnRlcg</string>
98 98 <object class="NSFont" key="IBUIFont" id="605141269">
99 99 <string key="NSName">Helvetica</string>
100 100 <double key="NSSize">14</double>
@@ -1035,6 +1035,6 @@ dCBzdW5kYXkuA</string>
1035 1035 <bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
1036 1036 <string key="IBDocument.LastKnownRelativeProjectPath">AttributedLabel Example.xcodeproj</string>
1037 1037 <int key="IBDocument.defaultPropertyAccessControl">3</int>
1038   - <string key="IBCocoaTouchPluginVersion">132</string>
  1038 + <string key="IBCocoaTouchPluginVersion">141</string>
1039 1039 </data>
1040 1040 </archive>
4 OHAttributedLabel.h
@@ -61,6 +61,7 @@ CTLineBreakMode CTLineBreakModeFromUILineBreakMode(UILineBreakMode lineBreakMode
61 61 CTFrameRef textFrame;
62 62 CGRect drawingRect;
63 63 NSMutableArray* customLinks;
  64 + NSMutableArray* customRegularExpressions;
64 65 NSTextCheckingResult* activeLink;
65 66 }
66 67
@@ -73,8 +74,11 @@ CTLineBreakMode CTLineBreakModeFromUILineBreakMode(UILineBreakMode lineBreakMode
73 74 @property(nonatomic, retain) UIColor* linkColor; //!< Defaults to [UIColor blueColor]. See also OHAttributedLabelDelegate
74 75 @property(nonatomic, retain) UIColor* highlightedLinkColor; //[UIColor colorWithWhite:0.2 alpha:0.5]
75 76 @property(nonatomic, assign) BOOL underlineLinks; //!< Defaults to YES. See also OHAttributedLabelDelegate
  77 +
76 78 -(void)addCustomLink:(NSURL*)linkUrl inRange:(NSRange)range;
  79 +-(void)addCustomRegularExpression:(NSRegularExpression *)expression inRange:(NSRange)range;
77 80 -(void)removeAllCustomLinks;
  81 +-(void)removeAllRegularExpressions;
78 82
79 83 @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)
80 84 @property(nonatomic, assign) IBOutlet id<OHAttributedLabelDelegate> delegate;
50 OHAttributedLabel.m
@@ -165,6 +165,7 @@ @implementation OHAttributedLabel
165 165 - (void)commonInit
166 166 {
167 167 customLinks = [[NSMutableArray alloc] init];
  168 + customRegularExpressions = [[NSMutableArray alloc] init];
168 169 self.linkColor = [UIColor blueColor];
169 170 self.highlightedLinkColor = [UIColor colorWithWhite:0.4 alpha:0.3];
170 171 self.underlineLinks = YES;
@@ -202,6 +203,7 @@ -(void)dealloc
202 203 [self resetTextFrame];
203 204
204 205 [customLinks release];
  206 + [customRegularExpressions release];
205 207 self.linkColor = nil;
206 208 self.highlightedLinkColor = nil;
207 209 [activeLink release];
@@ -221,10 +223,21 @@ -(void)addCustomLink:(NSURL*)linkUrl inRange:(NSRange)range {
221 223 [customLinks addObject:link];
222 224 [self setNeedsDisplay];
223 225 }
  226 +-(void)addCustomRegularExpression:(NSRegularExpression *)expression inRange:(NSRange)range{
  227 + [expression enumerateMatchesInString:self.text options:0 range:NSMakeRange(0, [self.text length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
  228 + [customRegularExpressions addObject:match];
  229 + }];
  230 +
  231 + [self setNeedsDisplay];
  232 +}
224 233 -(void)removeAllCustomLinks {
225 234 [customLinks removeAllObjects];
226 235 [self setNeedsDisplay];
227 236 }
  237 +-(void)removeAllRegularExpressions {
  238 + [customRegularExpressions removeAllObjects];
  239 + [self setNeedsDisplay];
  240 +}
228 241
229 242 -(NSMutableAttributedString*)attributedTextWithLinks {
230 243 NSMutableAttributedString* str = [self.attributedText mutableCopy];
@@ -247,6 +260,7 @@ -(NSMutableAttributedString*)attributedTextWithLinks {
247 260 [str setTextUnderlineStyle:uStyle range:[result range]];
248 261 }];
249 262 }
  263 + // Custom links
250 264 [customLinks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
251 265 {
252 266 NSTextCheckingResult* result = (NSTextCheckingResult*)obj;
@@ -270,6 +284,30 @@ -(NSMutableAttributedString*)attributedTextWithLinks {
270 284 }
271 285 }
272 286 }];
  287 + // Custom regular expressions
  288 + [customRegularExpressions enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
  289 + {
  290 + NSTextCheckingResult* result = (NSTextCheckingResult*)obj;
  291 +
  292 + int32_t uStyle = self.underlineLinks ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone;
  293 + UIColor* thisLinkColor = (self.delegate && [self.delegate respondsToSelector:@selector(colorForLink:underlineStyle:)])
  294 + ? [self.delegate colorForLink:result underlineStyle:&uStyle] : self.linkColor;
  295 +
  296 + @try {
  297 + if (thisLinkColor)
  298 + [str setTextColor:thisLinkColor range:[result range]];
  299 + if (uStyle>0)
  300 + [str setTextUnderlineStyle:uStyle range:[result range]];
  301 + }
  302 + @catch (NSException * e) {
  303 + // Protection against NSRangeException
  304 + if ([[e name] isEqualToString:NSRangeException]) {
  305 + NSLog(@"[OHAttributedLabel] exception: %@",e);
  306 + } else {
  307 + @throw;
  308 + }
  309 + }
  310 + }];
273 311 return [str autorelease];
274 312 }
275 313
@@ -291,7 +329,7 @@ -(NSTextCheckingResult*)linkAtCharacterIndex:(CFIndex)idx {
291 329 }];
292 330 if (foundResult) return foundResult;
293 331 }
294   -
  332 + // Custom links
295 333 [customLinks enumerateObjectsUsingBlock:^(id obj, NSUInteger aidx, BOOL *stop)
296 334 {
297 335 NSRange r = [(NSTextCheckingResult*)obj range];
@@ -300,6 +338,15 @@ -(NSTextCheckingResult*)linkAtCharacterIndex:(CFIndex)idx {
300 338 *stop = YES;
301 339 }
302 340 }];
  341 + // Custom regular expressions
  342 + [customRegularExpressions enumerateObjectsUsingBlock:^(id obj, NSUInteger aidx, BOOL *stop)
  343 + {
  344 + NSRange r = [(NSTextCheckingResult*)obj range];
  345 + if (NSLocationInRange(idx, r)) {
  346 + foundResult = [[obj retain] autorelease];
  347 + *stop = YES;
  348 + }
  349 + }];
303 350 return foundResult;
304 351 }
305 352
@@ -548,6 +595,7 @@ -(void)setAttributedText:(NSAttributedString*)attributedText {
548 595 [_attributedText release];
549 596 _attributedText = [attributedText mutableCopy];
550 597 [self removeAllCustomLinks];
  598 + [self removeAllRegularExpressions];
551 599 [self setNeedsDisplay];
552 600 }
553 601

0 comments on commit c51d27d

Please sign in to comment.
Something went wrong with that request. Please try again.