Skip to content

Instantly share code, notes, and snippets.

@snown
Created October 1, 2014 19:20
Show Gist options
  • Save snown/029fda85319111e0d0c7 to your computer and use it in GitHub Desktop.
Save snown/029fda85319111e0d0c7 to your computer and use it in GitHub Desktop.
UITextView Category for initializing a text view with a specified UILabel object
//
// UITextView+MTRXUILabel.h
// Site Survey
//
// Created by Logan Holmes on 10/1/14.
// Copyright (c) 2014 Logan Holmes. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UITextView (MTRXUILabel)
+ (instancetype)textViewWithUILabel:(UILabel *)label;
@end
//
// UITextView+MTRXUILabel.m
//
// Created by Logan Holmes on 10/1/14.
// Copyright (c) 2014 Logan Holmes. All rights reserved.
//
#import "UITextView+MTRXUILabel.h"
#import <Masonry/Masonry.h>
@implementation UITextView (MTRXUILabel)
+ (instancetype)textViewWithUILabel:(UILabel *)label {
UITextView *myself = [(UITextView *)[self alloc] initWithFrame:CGRectZero];
if (label) {
// UILabel Attribues
// Accessing the Text Attributes
myself.text = label.text;
myself.attributedText = label.attributedText;
myself.font = label.font;
myself.textColor = label.textColor;
myself.textAlignment = label.textAlignment;
myself.textContainer.lineBreakMode = label.lineBreakMode;
myself.textContainer.maximumNumberOfLines = (NSUInteger)label.numberOfLines;
// Line up the text vertically
myself.textContainer.lineFragmentPadding = 0;
CGRect textRect = [label textRectForBounds:label.bounds limitedToNumberOfLines:label.numberOfLines];
myself.textContainerInset = UIEdgeInsetsMake((label.bounds.size.height / 2.0) - (textRect.size.height / 2.0), 0.0, 0.0, 0.0);
// UIView Attributes
// Configuring a View's Visual Appearance
myself.backgroundColor = label.backgroundColor;
myself.alpha = label.alpha;
myself.opaque = label.opaque;
myself.tintColor = label.tintColor;
myself.clipsToBounds = label.clipsToBounds;
myself.clearsContextBeforeDrawing = label.clearsContextBeforeDrawing;
}
return myself;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment