Created
October 9, 2017 14:05
-
-
Save phynet/8265057d3a036aaabb66ac3eb5cc6d07 to your computer and use it in GitHub Desktop.
Class to create insets in UILabel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RLInsetsLabel.h" | |
#import "UIView+Utils.h" | |
@implementation RLInsetsLabel | |
- (void)awakeFromNib { | |
[super awakeFromNib]; | |
self.edgeInsets = UIEdgeInsetsMake(self.topEdge, self.leftEdge, self.bottomEdge, self.rightEdge); | |
} | |
- (id)initWithFrame:(CGRect)frame { | |
self = [super initWithFrame:frame]; | |
if (self) | |
{ | |
self.edgeInsets = UIEdgeInsetsMake(self.topEdge, self.leftEdge, self.bottomEdge, self.rightEdge); | |
} | |
return self; | |
} | |
- (void)drawTextInRect:(CGRect)rect { | |
self.edgeInsets = UIEdgeInsetsMake(self.topEdge, self.leftEdge, self.bottomEdge, self.rightEdge); | |
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)]; | |
} | |
- (CGSize)intrinsicContentSize { | |
CGSize size = [super intrinsicContentSize]; | |
size.width += self.edgeInsets.left + self.edgeInsets.right; | |
size.height += self.edgeInsets.top + self.edgeInsets.bottom; | |
NSLog(@"Size is %@",NSStringFromCGSize(size)); | |
return size; | |
} | |
- (CGFloat)estimatedHeight { | |
NSDictionary *attributes = @{NSFontAttributeName:self.font}; | |
CGRect rect = [self.text boundingRectWithSize:CGSizeMake(self.width,CGFLOAT_MAX) | |
options:NSStringDrawingUsesLineFragmentOrigin | |
attributes:attributes | |
context:nil]; | |
return rect.size.height + self.edgeInsets.top + self.edgeInsets.bottom; | |
} | |
@end | |
#import "UIView+Utils.h" | |
@implementation UIView (Utils) | |
//Creates and returns a clear view that can be used for tableview backgrounds. | |
+ (UIView *)clearBackgroundView { | |
UIView *view = [[UIView alloc] init]; | |
view.backgroundColor = [UIColor clearColor]; | |
return view; | |
} | |
//Shortcut to get the Y coordinate of the frame | |
- (CGFloat)y { | |
return CGRectGetMinY(self.frame); | |
} | |
//Shortcut to get the X coordinate of the frame | |
- (CGFloat)x { | |
return CGRectGetMinX(self.frame); | |
} | |
//Maximum coordinate Y of the frame | |
- (CGFloat)maxY { | |
return CGRectGetMaxY(self.frame); | |
} | |
//Maximum coordinate X of the frame | |
- (CGFloat)maxX { | |
return CGRectGetMaxX(self.frame); | |
} | |
//Shortcut to get the height of the frame | |
- (CGFloat)height { | |
return CGRectGetHeight(self.frame); | |
} | |
//Shortcut to get the width of the frame | |
- (CGFloat)width { | |
return CGRectGetWidth(self.frame); | |
} | |
- (void)setX:(CGFloat)x { | |
[self setOriginX:x]; | |
} | |
- (void)setY:(CGFloat)y { | |
[self setOriginY:y]; | |
} | |
// Give this view a new origin | |
- (void)setOrigin:(CGPoint)newOrigin { | |
CGRect newFrame = self.frame; | |
newFrame.origin = newOrigin; | |
self.frame = newFrame; | |
} | |
// Change the x position of this view | |
- (void)setOriginX:(CGFloat)newX { | |
CGRect newFrame = self.frame; | |
newFrame.origin.x = newX; | |
self.frame = newFrame; | |
} | |
// Change the y position of this view | |
- (void)setOriginY:(CGFloat)newY { | |
CGRect newFrame = self.frame; | |
newFrame.origin.y = newY; | |
self.frame = newFrame; | |
} | |
// Give this view new dimensions | |
- (void)setSize:(CGSize)newSize { | |
CGRect newFrame = self.frame; | |
newFrame.size = newSize; | |
self.frame = newFrame; | |
} | |
// Make this view fatter/thinner | |
- (void)setWidth:(CGFloat)newWidth { | |
CGRect newFrame = self.frame; | |
newFrame.size.width = newWidth; | |
self.frame = newFrame; | |
} | |
// Make this view taller/shorter | |
- (void)setHeight:(CGFloat)newHeight { | |
CGRect newFrame = self.frame; | |
newFrame.size.height = newHeight; | |
self.frame = newFrame; | |
} | |
- (void)removeAllSubviews { | |
for (UIView *view in self.subviews) { | |
[view removeFromSuperview]; | |
} | |
} | |
- (void)roundCornersWithRadius:(CGFloat)radius { | |
CALayer *layer = [self layer]; | |
[layer setCornerRadius:radius]; | |
layer.masksToBounds = YES; | |
} | |
- (void)addBorderWithColor:(UIColor *)color andWidth:(CGFloat)width { | |
[self.layer setBorderColor:[color CGColor]]; | |
[self.layer setBorderWidth:width]; | |
} | |
- (void)addBorderWithColor:(UIColor *)color andWidth:(CGFloat)borderWidth toEdges:(BorderEdges)edges { | |
if (edges & BorderEdgesTop) { | |
UIView *topBorder = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.width, borderWidth)]; | |
topBorder.backgroundColor = color; | |
topBorder.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; | |
[self addSubview:topBorder]; | |
} | |
if (edges & BorderEdgesBottom) { | |
UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0.f, self.height, self.width, borderWidth)]; | |
bottomBorder.backgroundColor = color; | |
bottomBorder.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; | |
[self addSubview:bottomBorder]; | |
} | |
if (edges & BorderEdgesLeft) { | |
UIView *leftBorder = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, borderWidth, self.height)]; | |
leftBorder.backgroundColor = color; | |
leftBorder.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin; | |
[self addSubview:leftBorder]; | |
} | |
if (edges & BorderEdgesRight) { | |
UIView *topBorder = [[UIView alloc] initWithFrame:CGRectMake(self.width, 0.f, borderWidth, self.height)]; | |
topBorder.backgroundColor = color; | |
topBorder.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin; | |
[self addSubview:topBorder]; | |
} | |
} | |
- (CGFloat)subviewContainingHeight { | |
CGFloat height = 0; | |
for (UIView *view in [self subviews]) { | |
height = MAX(view.frame.origin.y + view.frame.size.height, height); | |
} | |
return height; | |
} | |
- (void)addFlexibleWidthHeightMasks { | |
self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; | |
} | |
#pragma mark - Borders | |
- (UIView *)addTopBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth { | |
UIView *border = [UIView new]; | |
border.backgroundColor = color; | |
[border setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin]; | |
border.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth); | |
[self addSubview:border]; | |
return border; | |
} | |
- (UIView *)addBottomBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth { | |
UIView *border = [UIView new]; | |
border.backgroundColor = color; | |
[border setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin]; | |
border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth); | |
[self addSubview:border]; | |
return border; | |
} | |
- (UIView *)addLeftBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth { | |
UIView *border = [UIView new]; | |
border.backgroundColor = color; | |
border.frame = CGRectMake(0, 0, borderWidth, self.frame.size.height); | |
[border setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin]; | |
[self addSubview:border]; | |
return border; | |
} | |
- (UIView *)addRightBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth { | |
UIView *border = [UIView new]; | |
border.backgroundColor = color; | |
[border setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin]; | |
border.frame = CGRectMake(self.frame.size.width - borderWidth, 0, borderWidth, self.frame.size.height); | |
[self addSubview:border]; | |
return border; | |
} | |
#pragma mark - Snapshot | |
- (UIImage *)imageFromNotVisibleView { | |
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [[UIScreen mainScreen] scale]); | |
BOOL hidden = [self isHidden]; | |
[self setHidden:NO]; | |
[[self layer] renderInContext:UIGraphicsGetCurrentContext()]; | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
[self setHidden:hidden]; | |
return image; | |
} | |
- (UIImage *)imageScreenShot { | |
if (self.window) { | |
UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 1); | |
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES]; | |
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); | |
return screenshot; | |
} else { | |
return [self imageFromNotVisibleView]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment