Skip to content

Commit

Permalink
Merge pull request #18 from Remki/AddedAttributedTexts
Browse files Browse the repository at this point in the history
Added support for attributedText.
  • Loading branch information
segiddins committed Oct 5, 2015
2 parents e50155b + 9c903a2 commit e347735
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ UIFont *myFont = [UIFont dynamicFontWithName:@"Courier" baseSize:16.0f];
NSInteger textDelta = [[UIApplication sharedApplication] preferredFontSizeDelta];
```

## AttributedText Support
## `NSAttributedString` Support

SSDynamicText supports attributed text, all you have to do is set your attributed text to new property dynamicAttributedText.

Expand Down
9 changes: 8 additions & 1 deletion SSDynamicText/NSAttributedString+SSTextSize.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// NSAttributedString+SSTextSize.h
// Pods
// SSDynamicText
//
// Created by Remigiusz Herba on 28/08/15.
//
Expand All @@ -10,6 +10,13 @@

@interface NSAttributedString (SSTextSize)

/**
* Creates NSAttributedString with each font size changed by delta
* @param delta The size by which you want to change the font.
* A negative number will decrease font size.
* A positive number will increase font size.
* @return new NSAttributedString object with font size changed by delta.
*/
- (NSAttributedString *)ss_attributedStringWithAdjustedFontSizeWithDelta:(NSInteger)delta;

@end
2 changes: 1 addition & 1 deletion SSDynamicText/NSAttributedString+SSTextSize.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// NSAttributedString+SSTextSize.m
// Pods
// SSDynamicText
//
// Created by Remigiusz Herba on 28/08/15.
//
Expand Down
16 changes: 10 additions & 6 deletions SSDynamicText/SSDynamicAttributedTextSizable.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SSDynamicAttributedTextSizable.h
// Pods
// SSDynamicText
//
// Created by Remigiusz Herba on 15/09/15.
//
Expand All @@ -10,12 +10,16 @@

@protocol SSDynamicAttributedTextSizable <NSObject>

/*
TextView and TextField sometimes calls setAttributedText even when we work with normal Text.
Framework is using it under the hood sometimes after layouts or even setText calls it. Because of that we cannot override
default attributeText setter to change font, sometimes it change font at random.
/**
* TextView and TextField sometimes calls setAttributedText even when we work with normal Text.
* Framework is using it under the hood sometimes after layouts or even setText calls it. Because of that we cannot override
* default attributeText setter to change font, sometimes it change font at random.
*
* This is used to set attributedText which will be dynamicaly changed with font size changes.
* Updating this will change attributedText to dynamicAttributedText + font sizes changed with delta.
* @return original dynamicAttributedText value. To check current attributedText font sizes use @property attributedText
*
*/

@property (nonatomic, copy) NSAttributedString *dynamicAttributedText;

@end
4 changes: 2 additions & 2 deletions SSDynamicText/SSDynamicButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#import <UIKit/UIKit.h>

/*
While creating this button in xib, don't forget to change it type to Custom !!
/**
* While creating this button in xib, don't forget to change it type to Custom !!
*/
@interface SSDynamicButton : UIButton

Expand Down
1 change: 1 addition & 0 deletions SSDynamicText/SSDynamicButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
- (void)awakeFromNib {
[super awakeFromNib];

NSAssert(self.buttonType == UIButtonTypeCustom, @"Change SSDynamicButton.buttonType to UIButtonTypeCustom in your nib");
NSString *fontName;
CGFloat baseSize = 0;

Expand Down
6 changes: 3 additions & 3 deletions SSDynamicText/SSDynamicLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (void)setFont:(UIFont *)font {

- (instancetype)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self startObservingTextSizeChanged];
[self startObservingTextSizeChanges];
}

return self;
Expand All @@ -47,7 +47,7 @@ - (void)awakeFromNib {
[super awakeFromNib];

[self setupBaseFontBasedOnCurrentFont];
[self startObservingTextSizeChanged];
[self startObservingTextSizeChanges];
}

+ (instancetype)labelWithFont:(NSString *)fontName baseSize:(CGFloat)size {
Expand Down Expand Up @@ -100,7 +100,7 @@ - (void)setupBaseFontBasedOnCurrentFont {
size:baseSize]);
}

- (void)startObservingTextSizeChanged {
- (void)startObservingTextSizeChanges {
[self ss_startObservingTextSizeChangesWithBlock:self.textSizeChanger.changeHandler];
}

Expand Down
6 changes: 3 additions & 3 deletions SSDynamicText/SSDynamicTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (void)setFont:(UIFont *)font {

- (instancetype)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self startObservingTextSizeChanged];
[self startObservingTextSizeChanges];
}

return self;
Expand All @@ -47,7 +47,7 @@ - (void)awakeFromNib {
[super awakeFromNib];

[self setupBaseFontBasedOnCurrentFont];
[self startObservingTextSizeChanged];
[self startObservingTextSizeChanges];
}

+ (instancetype)textFieldWithFont:(NSString *)fontName baseSize:(CGFloat)size {
Expand Down Expand Up @@ -100,7 +100,7 @@ - (void)setupBaseFontBasedOnCurrentFont {
size:baseSize]);
}

- (void)startObservingTextSizeChanged {
- (void)startObservingTextSizeChanges {
[self ss_startObservingTextSizeChangesWithBlock:self.textSizeChanger.changeHandler];
}

Expand Down
18 changes: 17 additions & 1 deletion SSDynamicText/SSDynamicTextSizeChanger.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SSDynamicTextSizeChanger.h
// Pods
// SSDynamicText
//
// Created by Remigiusz Herba on 15/09/15.
//
Expand All @@ -12,10 +12,26 @@

@interface SSDynamicTextSizeChanger : NSObject <SSDynamicAttributedTextSizable>

/**
* The default font descriptor used by view.
* Its size is adjusted up (or down) based on the user's preferred text size.
* Updating this will change the view's font.
*/
@property (nonatomic, strong) UIFontDescriptor *defaultFontDescriptor;

/**
* The default block called by view when font size change.
*/
@property (nonatomic, readonly) SSTextSizeChangedBlock changeHandler;

/**
* Block which is called when SSDynamicTextSizeChanger want to change font, view should configure this block.
*/
@property (nonatomic, copy) void(^fontChangeBlock)(UIFont *font);

/**
* Block which is called when SSDynamicTextSizeChanger want to change attributedText, view should configure this block.
*/
@property (nonatomic, copy) void(^attributedTextChangeBlock)(NSAttributedString *attributedString);

@end
2 changes: 1 addition & 1 deletion SSDynamicText/SSDynamicTextSizeChanger.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SSDynamicTextSizeChanger.m
// Pods
// SSDynamicText
//
// Created by Remigiusz Herba on 15/09/15.
//
Expand Down
6 changes: 3 additions & 3 deletions SSDynamicText/SSDynamicTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (void)setFont:(UIFont *)font {

- (instancetype)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self startObservingTextSizeChanged];
[self startObservingTextSizeChanges];
}

return self;
Expand All @@ -47,7 +47,7 @@ - (void)awakeFromNib {
[super awakeFromNib];

[self setupBaseFontBasedOnCurrentFont];
[self startObservingTextSizeChanged];
[self startObservingTextSizeChanges];
}

+ (instancetype)textViewWithFont:(NSString *)fontName baseSize:(CGFloat)size {
Expand Down Expand Up @@ -100,7 +100,7 @@ - (void)setupBaseFontBasedOnCurrentFont {
size:baseSize]);
}

- (void)startObservingTextSizeChanged {
- (void)startObservingTextSizeChanges {
[self ss_startObservingTextSizeChangesWithBlock:self.textSizeChanger.changeHandler];
}

Expand Down

0 comments on commit e347735

Please sign in to comment.