Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Jul 9, 2013
0 parents commit aeb508a
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2010-2013 Sam Soffes, http://soff.es

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 AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 changes: 10 additions & 0 deletions Readme.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SAMLabel

A simple subclass of UILabel with vertical alignment and text insets.

SAMLabel is tested on iOS 6 and requires ARC. Released under the [MIT license](LICENSE).


## Installation

Simply add the files in the `SAMLabel.h` and `SAMLabel.m` to your project or add `SAMLabel` to your Podfile if you're using CocoaPods.
14 changes: 14 additions & 0 deletions SAMLabel.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Pod::Spec.new do |spec|
spec.name = 'SAMLabel'
spec.version = '0.1.0'
spec.authors = {'Sam Soffes' => 'sam@soff.es'}
spec.homepage = 'https://github.com/soffes/SAMLabel'
spec.summary = 'A simple subclass of UILabel with vertical alignment and text insets.'
spec.source = {:git => 'https://github.com/soffes/SAMLabel.git', :tag => "v#{spec.version}"}
spec.license = { :type => 'MIT', :file => 'LICENSE' }

spec.platform = :ios
spec.requires_arc = true
spec.frameworks = 'UIKit'
spec.source_files = 'SAMLabel'
end
44 changes: 44 additions & 0 deletions SAMLabel/SAMLabel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// SAMLabel.h
// SAMLabel
//
// Created by Sam Soffes on 7/12/10.
// Copyright 2010-2013 Sam Soffes. All rights reserved.
//

#import <UIKit/UIKit.h>

/**
The vertical alignment of text within a label.
*/
typedef enum {
/** Aligns the text vertically at the top in the label (the default). */
SAMLabelVerticalTextAlignmentTop = UIControlContentVerticalAlignmentTop,

/** Aligns the text vertically in the center of the label. */
SAMLabelVerticalTextAlignmentMiddle = UIControlContentVerticalAlignmentCenter,

/** Aligns the text vertically at the bottom in the label. */
SAMLabelVerticalTextAlignmentBottom = UIControlContentVerticalAlignmentBottom
} SAMLabelVerticalTextAlignment;

/**
Simple label subclass that adds the ability to align your text to the top or bottom.
*/
@interface SAMLabel : UILabel

/**
The vertical text alignment of the receiver.
The default is `SAMLabelVerticalTextAlignmentMiddle` to match `UILabel`.
*/
@property (nonatomic, assign) SAMLabelVerticalTextAlignment verticalTextAlignment;

/**
The edge insets of the text.
The default is `UIEdgeInsetsZero` so it behaves like `UILabel` by default.
*/
@property (nonatomic, assign) UIEdgeInsets textEdgeInsets;

@end
75 changes: 75 additions & 0 deletions SAMLabel/SAMLabel.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// SAMLabel.m
// SAMLabel
//
// Created by Sam Soffes on 7/12/10.
// Copyright 2010-2013 Sam Soffes. All rights reserved.
//

#import "SAMLabel.h"

@implementation SAMLabel

#pragma mark - Accessors

@synthesize verticalTextAlignment = _verticalTextAlignment;

- (void)setVerticalTextAlignment:(SAMLabelVerticalTextAlignment)verticalTextAlignment {
_verticalTextAlignment = verticalTextAlignment;

[self setNeedsLayout];
}


@synthesize textEdgeInsets = _textEdgeInsets;

- (void)setTextEdgeInsets:(UIEdgeInsets)textEdgeInsets {
_textEdgeInsets = textEdgeInsets;

[self setNeedsLayout];
}


#pragma mark - UIView

- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self initialize];
}
return self;
}


- (id)initWithFrame:(CGRect)aFrame {
if ((self = [super initWithFrame:aFrame])) {
[self initialize];
}
return self;
}


#pragma mark - UILabel

- (void)drawTextInRect:(CGRect)rect {
rect = UIEdgeInsetsInsetRect(rect, self.textEdgeInsets);

if (self.verticalTextAlignment == SAMLabelVerticalTextAlignmentTop) {
CGSize sizeThatFits = [self sizeThatFits:rect.size];
rect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, sizeThatFits.height);
} else if (self.verticalTextAlignment == SAMLabelVerticalTextAlignmentBottom) {
CGSize sizeThatFits = [self sizeThatFits:rect.size];
rect = CGRectMake(rect.origin.x, rect.origin.y + (rect.size.height - sizeThatFits.height), rect.size.width, sizeThatFits.height);
}

[super drawTextInRect:rect];
}


#pragma mark - Private

- (void)initialize {
self.verticalTextAlignment = SAMLabelVerticalTextAlignmentMiddle;
self.textEdgeInsets = UIEdgeInsetsZero;
}

@end

0 comments on commit aeb508a

Please sign in to comment.