diff --git a/AWKHelpers.podspec b/AWKHelpers.podspec index d135647..b72d6f2 100644 --- a/AWKHelpers.podspec +++ b/AWKHelpers.podspec @@ -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 diff --git a/Classes/AWKStringHelper.h b/Classes/AWKStringHelper.h index d05496c..88d4334 100644 --- a/Classes/AWKStringHelper.h +++ b/Classes/AWKStringHelper.h @@ -7,6 +7,7 @@ // #import +#import enum { NSTruncateStringPositionStart=0, @@ -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 diff --git a/Classes/AWKStringHelper.m b/Classes/AWKStringHelper.m index 2a2c70f..377e6b7 100644 --- a/Classes/AWKStringHelper.m +++ b/Classes/AWKStringHelper.m @@ -8,6 +8,8 @@ #import "AWKStringHelper.h" #import +#import "AWKDictionaryHelper.h" + @implementation NSString (AWKStringHelper) @@ -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