Skip to content

Commit

Permalink
Wrapping for words that are too long
Browse files Browse the repository at this point in the history
Basic wrapping for words that are too long. Also updated demo to show
the behavior.
  • Loading branch information
maranas committed Apr 27, 2012
1 parent f1d0057 commit c0b312e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
27 changes: 26 additions & 1 deletion GLTapLabelDemo/GLTapLabel.m
Expand Up @@ -48,7 +48,32 @@ -(void)drawTextInRect:(CGRect)rect
NSString *read;
NSScanner *s = [NSScanner scannerWithString:self.text];
while ([s scanUpToCharactersFromSet:[NSCharacterSet symbolCharacterSet] intoString:&read]) {
NSArray *words = [read componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSArray *origWords = [read componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSMutableArray *words = [NSMutableArray array];
for (NSString* word in origWords)
{
// this is just to avoid ARC complications. We could just as easily make it __strong but then it won't work with non-ARC builds.
NSString* origWord = word;
NSString* newWord = word;
CGSize s = [newWord sizeWithFont:self.font];
NSString *cutWord = @"";
int c = [word length]-1;
while (s.width > rect.size.width) {
cutWord = [origWord substringFromIndex:c];
newWord = [origWord substringToIndex:c];
s = [newWord sizeWithFont:self.font];
c -= 1;
if (s.width <= rect.size.width)
{
[words addObject:newWord];
origWord = cutWord;
s = [origWord sizeWithFont:self.font];
c = [origWord length]-1;
}
}
[words addObject:origWord];
}

[words enumerateObjectsUsingBlock:^(NSString *word, NSUInteger idx, BOOL *stop) {
BOOL hot = [word hasPrefix:@"#"] || [word hasPrefix:@"@"];
UIFont *f= hot ? hotFont : self.font;
Expand Down
22 changes: 13 additions & 9 deletions GLTapLabelDemo/en.lproj/GLViewController.xib
@@ -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">1280</int>
<string key="IBDocument.SystemVersion">11C74</string>
<string key="IBDocument.InterfaceBuilderVersion">1938</string>
<string key="IBDocument.AppKitVersion">1138.23</string>
<string key="IBDocument.HIToolboxVersion">567.00</string>
<int key="IBDocument.SystemTarget">1296</int>
<string key="IBDocument.SystemVersion">11D50b</string>
<string key="IBDocument.InterfaceBuilderVersion">2182</string>
<string key="IBDocument.AppKitVersion">1138.32</string>
<string key="IBDocument.HIToolboxVersion">568.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="NS.object.0">933</string>
<string key="NS.object.0">1181</string>
</object>
<array key="IBDocument.IntegratedClassDependencies">
<string>IBProxyObject</string>
Expand Down Expand Up @@ -38,7 +38,7 @@
<object class="IBUILabel" id="699338738">
<reference key="NSNextResponder" ref="774585933"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{56, 165}, {208, 162}}</string>
<string key="NSFrame">{{56, 109}, {208, 210}}</string>
<reference key="NSSuperview" ref="774585933"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="74742103"/>
Expand All @@ -48,7 +48,7 @@
<int key="IBUIContentMode">7</int>
<bool key="IBUIMultipleTouchEnabled">YES</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<string key="IBUIText">hh #TAG1 #TAG2 normal πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜ test @user1 @user2 πŸ•˜πŸ•šπŸ‘‚πŸ‘‚πŸ‘€ πŸ‘»πŸ‘»πŸŽ…πŸŽ‘πŸ’£πŸ’£fjdhfjdhfπŸ”«πŸ’ŠπŸ’Š</string>
<string key="IBUIText">areallyreallyreallylongwordyupthisword'slongandmightexceedthreelinesbutnoworrieswe'llwrapit #TAG1 #TAG2 normal πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜πŸ•˜ test @user1 @user2 πŸ•˜πŸ•šπŸ‘‚πŸ‘‚πŸ‘€ πŸ‘»πŸ‘»πŸŽ…πŸŽ‘πŸ’£πŸ’£fjdhfjdhfπŸ”«πŸ’ŠπŸ’Š</string>
<object class="NSColor" key="IBUITextColor" id="547420789">
<int key="NSColorSpace">1</int>
<bytes key="NSRGB">MCAwIDAAA</bytes>
Expand Down Expand Up @@ -281,8 +281,12 @@
</object>
<int key="IBDocument.localizationMode">0</int>
<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"/>
</object>
<bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
<int key="IBDocument.defaultPropertyAccessControl">3</int>
<string key="IBCocoaTouchPluginVersion">933</string>
<string key="IBCocoaTouchPluginVersion">1181</string>
</data>
</archive>

0 comments on commit c0b312e

Please sign in to comment.