Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting iOS 7 #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>8A17BD83-864D-4CFD-B4D5-575522C87497</string>
<key>IDESourceControlProjectName</key>
<string>GLTapLabelDemo</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>B9A878C4-EB96-4B33-A17C-AE108FBB9986</key>
<string>ssh://github.com/ibrahimkteish/GLTapLabelDemo.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>GLTapLabelDemo.xcodeproj/project.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>B9A878C4-EB96-4B33-A17C-AE108FBB9986</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>ssh://github.com/ibrahimkteish/GLTapLabelDemo.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>B9A878C4-EB96-4B33-A17C-AE108FBB9986</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>B9A878C4-EB96-4B33-A17C-AE108FBB9986</string>
<key>IDESourceControlWCCName</key>
<string>GLTapLabelDemo</string>
</dict>
</array>
</dict>
</plist>
103 changes: 90 additions & 13 deletions GLTapLabelDemo/GLTapLabel.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*
* Copyright (c) 2011 German Laullon
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -20,6 +20,8 @@
* THE SOFTWARE.
*/

#define is_iOS_7 [[UIDevice currentDevice].systemVersion hasPrefix:@"7"]

#import "GLTapLabel.h"

@implementation GLTapLabel
Expand All @@ -32,7 +34,7 @@ -(void)drawTextInRect:(CGRect)rect
UIColor *origColor = [self textColor];
[origColor set];
if(!hotFont){
hotFont = [UIFont fontWithName:[NSString stringWithFormat:@"%@-Bold",self.font.fontName] size:self.font.pointSize];
hotFont = [UIFont fontWithName:[NSString stringWithFormat:@"Helvetica-Bold"] size:self.font.pointSize];
}

if(!hotZones){
Expand All @@ -43,7 +45,21 @@ -(void)drawTextInRect:(CGRect)rect
[hotWords removeAllObjects];
}

CGSize space = [@" " sizeWithFont:self.font constrainedToSize:rect.size lineBreakMode:self.lineBreakMode];
CGSize space = CGSizeZero;

if (is_iOS_7)
{
space = [@" " boundingRectWithSize:rect.size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:self.font}
context:nil].size;
}
else
{
space = [@" " sizeWithFont:self.font constrainedToSize:rect.size lineBreakMode:self.lineBreakMode];
}


__block CGPoint drawPoint = CGPointMake(0,0);
NSString *read;
NSScanner *s = [NSScanner scannerWithString:self.text];
Expand All @@ -55,19 +71,42 @@ -(void)drawTextInRect:(CGRect)rect
// 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];
CGSize s = CGSizeZero;
if (is_iOS_7)
{
s = [newWord sizeWithAttributes:@{NSFontAttributeName:self.font}];
}
else
{
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];
if (is_iOS_7)
{
s = [newWord sizeWithAttributes:@{NSFontAttributeName:self.font}];
}
else
{
s = [newWord sizeWithFont:self.font];
}

c -= 1;
if (s.width <= rect.size.width)
{
[words addObject:newWord];
origWord = cutWord;
s = [origWord sizeWithFont:self.font];
if (is_iOS_7)
{
s = [newWord sizeWithAttributes:@{NSFontAttributeName:self.font}];
}
else
{
s = [newWord sizeWithFont:self.font];
}
c = [origWord length]-1;
}
}
Expand All @@ -77,7 +116,15 @@ -(void)drawTextInRect:(CGRect)rect
[words enumerateObjectsUsingBlock:^(NSString *word, NSUInteger idx, BOOL *stop) {
BOOL hot = [word hasPrefix:@"#"] || [word hasPrefix:@"@"];
UIFont *f= hot ? hotFont : self.font;
CGSize s = [word sizeWithFont:f];
CGSize s = CGSizeZero;
if (is_iOS_7)
{
s = [word sizeWithAttributes:@{NSFontAttributeName:f}];
}
else
{
s = [word sizeWithFont:f];
}
if(drawPoint.x + s.width > rect.size.width) {
drawPoint = CGPointMake(0, drawPoint.y + s.height);
}
Expand All @@ -86,7 +133,16 @@ -(void)drawTextInRect:(CGRect)rect
[hotWords addObject:word];
[linkColor set];
}
[word drawAtPoint:drawPoint withFont:f];
if (is_iOS_7)
{
[word drawAtPoint:drawPoint withAttributes:@{NSFontAttributeName:f}];
}
else
{
[word drawAtPoint:drawPoint withFont:f];

}

[origColor set];

drawPoint = CGPointMake(drawPoint.x + s.width + space.width, drawPoint.y);
Expand All @@ -96,11 +152,31 @@ -(void)drawTextInRect:(CGRect)rect
for(int idx=0;idx<read.length;idx=idx+2)
{
NSString *word=[read substringWithRange:NSMakeRange(idx, 2)];
CGSize s = [word sizeWithFont:self.font];
CGSize s = CGSizeZero;
if (is_iOS_7)
{
s = [word sizeWithAttributes:@{NSFontAttributeName:self.font}];
}
else
{
s = [word sizeWithFont:self.font];
}
if(drawPoint.x + s.width > rect.size.width) {
drawPoint = CGPointMake(0, drawPoint.y + s.height);
}
[word drawAtPoint:drawPoint withFont:self.font];

if(drawPoint.x + s.width > rect.size.width) {
drawPoint = CGPointMake(0, drawPoint.y + s.height);
}
if (is_iOS_7)
{
[word drawAtPoint:drawPoint withAttributes:@{NSFontAttributeName:self.font}];
}
else
{
[word drawAtPoint:drawPoint withFont:self.font];

}
drawPoint = CGPointMake(drawPoint.x + s.width, drawPoint.y);
}
}
Expand All @@ -110,7 +186,7 @@ -(void)drawTextInRect:(CGRect)rect
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = event.allTouches.anyObject;
CGPoint point = [touch locationInView:self];
CGPoint point = [touch locationInView:self];
[hotZones enumerateObjectsUsingBlock:^(NSValue *obj, NSUInteger idx, BOOL *stop) {
CGRect hotzone = [obj CGRectValue];
if (CGRectContainsPoint(hotzone, point)) {
Expand All @@ -123,3 +199,4 @@ -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
}

@end