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

Develop #20

Merged
merged 4 commits into from
May 26, 2015
Merged
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
4 changes: 2 additions & 2 deletions KILabel.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Pod::Spec.new do |s|

s.name = "KILabel"
s.version = "1.0.0"
s.version = "1.0.1"
s.summary = "Replacement for UILabel for iOS 7 and 8 that provides automatic detection of links such as URLs, twitter style usernames and hashtags."

s.description = <<-DESC
Expand All @@ -27,7 +27,7 @@ Pod::Spec.new do |s|

s.platform = :ios, "7.0"

s.source = { :git => "https://github.com/Krelborn/KILabel.git", :tag => "1.0.0" }
s.source = { :git => "https://github.com/Krelborn/KILabel.git", :tag => "1.0.1" }

s.source_files = "KILabel/Source/**/*.{h,m}"
public_header_files = "KILabel/Source/**/*.h"
Expand Down
52 changes: 26 additions & 26 deletions KILabel/Source/KILabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ - (NSDictionary *)linkAtPoint:(CGPoint)location
}

// Work out the offset of the text in the view
CGPoint textOffset;
NSRange glyphRange = [_layoutManager glyphRangeForTextContainer:_textContainer];
textOffset = [self calcTextOffsetForGlyphRange:glyphRange];
CGPoint textOffset = [self calcGlyphsPositionInView];

// Get the touch location and use text offset to convert to text cotainer coords
location.x -= textOffset.x;
Expand Down Expand Up @@ -543,23 +541,23 @@ - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)num
_textContainer.maximumNumberOfLines = numberOfLines;

// Measure the text with the new state
CGRect textBounds;
@try
{
NSRange glyphRange = [_layoutManager glyphRangeForTextContainer:_textContainer];
textBounds = [_layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:_textContainer];

// Position the bounds and round up the size for good measure
textBounds.origin = bounds.origin;
textBounds.size.width = ceilf(textBounds.size.width);
textBounds.size.height = ceilf(textBounds.size.height);
}
@finally
CGRect textBounds = [_layoutManager usedRectForTextContainer:_textContainer];

// Position the bounds and round up the size for good measure
textBounds.origin = bounds.origin;
textBounds.size.width = ceil(textBounds.size.width);
textBounds.size.height = ceil(textBounds.size.height);

if (textBounds.size.height < bounds.size.height)
{
// Restore the old container state before we exit under any circumstances
_textContainer.size = savedTextContainerSize;
_textContainer.maximumNumberOfLines = savedTextContainerNumberOfLines;
// Take verical alignment into account
CGFloat offsetY = (bounds.size.height - textBounds.size.height) / 2.0;
textBounds.origin.y += offsetY;
}

// Restore the old container state before we exit under any circumstances
_textContainer.size = savedTextContainerSize;
_textContainer.maximumNumberOfLines = savedTextContainerNumberOfLines;

return textBounds;
}
Expand All @@ -571,24 +569,26 @@ - (void)drawTextInRect:(CGRect)rect
// [super drawTextInRect:rect];

// Calculate the offset of the text in the view
CGPoint textOffset;
NSRange glyphRange = [_layoutManager glyphRangeForTextContainer:_textContainer];
textOffset = [self calcTextOffsetForGlyphRange:glyphRange];
CGPoint glyphsPosition = [self calcGlyphsPositionInView];

// Drawing code
[_layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:textOffset];
[_layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:textOffset];
[_layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:glyphsPosition];
[_layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:glyphsPosition];
}

// Returns the XY offset of the range of glyphs from the view's origin
- (CGPoint)calcTextOffsetForGlyphRange:(NSRange)glyphRange
- (CGPoint)calcGlyphsPositionInView
{
CGPoint textOffset = CGPointZero;

CGRect textBounds = [_layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:_textContainer];
CGFloat paddingHeight = (self.bounds.size.height - textBounds.size.height) / 2.0f;
if (paddingHeight > 0)
CGRect textBounds = [_layoutManager usedRectForTextContainer:_textContainer];
textBounds.size.width = ceil(textBounds.size.width);
textBounds.size.height = ceil(textBounds.size.height);

if (textBounds.size.height < self.bounds.size.height)
{
CGFloat paddingHeight = (self.bounds.size.height - textBounds.size.height) / 2.0;
textOffset.y = paddingHeight;
}

Expand Down
12 changes: 6 additions & 6 deletions KILabelDemo/KILabelDemo/KIViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ - (IBAction)toggleDetectURLs:(UISwitch *)sender
{
if (sender.isOn)
{
self.label.linkDetectionTypes |= KILinkTypeURL;
self.label.linkDetectionTypes |= KILinkTypeOptionURL;
}
else
{
self.label.linkDetectionTypes ^= KILinkTypeURL;
self.label.linkDetectionTypes ^= KILinkTypeOptionURL;
}
}

Expand All @@ -164,11 +164,11 @@ - (IBAction)toggleDetectUsernames:(UISwitch *)sender
{
if (sender.isOn)
{
self.label.linkDetectionTypes |= KILinkTypeUserHandle;
self.label.linkDetectionTypes |= KILinkTypeOptionUserHandle;
}
else
{
self.label.linkDetectionTypes ^= KILinkTypeUserHandle;
self.label.linkDetectionTypes ^= KILinkTypeOptionUserHandle;
}
}

Expand All @@ -181,11 +181,11 @@ - (IBAction)toggleDetectHashtags:(UISwitch *)sender
{
if (sender.isOn)
{
self.label.linkDetectionTypes |= KILinkTypeHashtag;
self.label.linkDetectionTypes |= KILinkTypeOptionHashtag;
}
else
{
self.label.linkDetectionTypes ^= KILinkTypeHashtag;
self.label.linkDetectionTypes ^= KILinkTypeOptionHashtag;
}
}

Expand Down