Skip to content

Commit

Permalink
Added attributed string methods to NSString
Browse files Browse the repository at this point in the history
  • Loading branch information
renssies committed Mar 3, 2015
1 parent 8770079 commit b3dc1e2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion AWKHelpers.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = "AWKHelpers"
s.version = "0.4.0"
s.version = "0.5"
s.summary = "A growing collection of UIKit and Foundation categories (helpers)"
s.description = <<-DESC
A growing collection of UIKit and Foundation categories (helpers) we use at Awkward
Expand Down
34 changes: 34 additions & 0 deletions Classes/AWKStringHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

enum {
NSTruncateStringPositionStart=0,
Expand Down Expand Up @@ -111,4 +112,37 @@ enum {
@return Returns a UTF-8 encoded string representation of current string
*/
- (NSString *)URLEncodedString;

#pragma mark Attributed Methods

/**
Creates a NSAttributedString from the string with the given line height
@param lineHeight The lineheight to assign to the whole string
@return Returns a NSAttributedString with the paragraphstyle and the required line height
*/
- (NSAttributedString *)attributedStringWithLineHeight:(CGFloat)lineHeight;

/**
Creates a NSAttributedString from the string with the given line height and alignment
@param lineHeight The lineheight to assign to the whole string
@param textAlignment The alignment to assign to the whole string
@param font The font to assign to the whole string
@return Returns a NSAttributedString with the paragraphstyle and the required line height and alignment
*/
- (NSAttributedString *)attributedStringWithLineHeight:(CGFloat)lineHeight textAlignement:(NSTextAlignment)textAlignment;

/**
Creates a NSAttributedString from the string with the given line height, alignment and font
@param lineHeight The lineheight to assign to the whole string
@param textAlignment The alignment to assign to the whole string
@return Returns a NSAttributedString with the paragraphstyle and the required line height, text alignment and font
*/
- (NSAttributedString *)attributedStringWithLineHeight:(CGFloat)lineHeight textAlignement:(NSTextAlignment)textAlignment font:(UIFont *)font;

@end
26 changes: 26 additions & 0 deletions Classes/AWKStringHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import "AWKStringHelper.h"
#import <CommonCrypto/CommonDigest.h>
#import "AWKDictionaryHelper.h"


@implementation NSString (AWKStringHelper)

Expand Down Expand Up @@ -157,4 +159,28 @@ - (NSString *)URLEncodedString {
return output;
}

#pragma mark Attributed Methods

- (NSAttributedString *)attributedStringWithLineHeight:(CGFloat)lineHeight {
return [self attributedStringWithLineHeight:lineHeight textAlignement:NSTextAlignmentLeft];

}

- (NSAttributedString *)attributedStringWithLineHeight:(CGFloat)lineHeight textAlignement:(NSTextAlignment)textAlignment {
return [self attributedStringWithLineHeight:lineHeight textAlignement:textAlignment font:nil];

}

- (NSAttributedString *)attributedStringWithLineHeight:(CGFloat)lineHeight textAlignement:(NSTextAlignment)textAlignment font:(UIFont *)font {
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObjectIfNonNil:font forKey:NSFontAttributeName];

NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setMinimumLineHeight:lineHeight];
[paragraphStyle setMaximumLineHeight:lineHeight];
[paragraphStyle setAlignment:textAlignment];
[attributes setObjectIfNonNil:paragraphStyle forKey:NSParagraphStyleAttributeName];
return [[NSAttributedString alloc] initWithString:self attributes:attributes];

}
@end

0 comments on commit b3dc1e2

Please sign in to comment.