Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CreatureSurvive/3280b95e478bc6ce948ca96d362a160f to your computer and use it in GitHub Desktop.
Save CreatureSurvive/3280b95e478bc6ce948ca96d362a160f to your computer and use it in GitHub Desktop.
UILabel Util Categories for day-to-day use
//
// UILabel+UtilityAttributes.h
//
// Created by Paulo Almeida on 1/31/14.
//
#import <UIKit/UIKit.h>
@interface UILabel (UtilityAttributes)
- (void) boldSubstring: (NSString*) substring;
- (void) boldRange: (NSRange) range;
- (void) colorRange: (NSRange) range AndColor:(UIColor*) color;
- (void) colorSubstring: (NSString*) substring AndColor:(UIColor*) color;
- (void) increaseFontSizeRange: (NSRange) range AndSize:(CGFloat) size;
- (void) increaseFontSizeSubstring: (NSString*) substring AndSize:(CGFloat) size ;
- (void) decreaseFontSizeRange: (NSRange) range AndSize:(CGFloat) size;
- (void) decreaseFontSizeSubstring: (NSString*) substring AndSize:(CGFloat) size;
@end
//
// UILabel+UtilityAttributes.m
//
// Created by Paulo Almeida on 1/31/14.
//
#import "UILabel+UtilityAttributes.h"
@implementation UILabel (UtilityAttributes)
- (void) boldRange: (NSRange) range {
if (![self respondsToSelector:@selector(setAttributedText:)]) {
return;
}
NSMutableAttributedString *attributedText= nil;
if(self.attributedText)
{
attributedText = [self.attributedText mutableCopy];
[attributedText addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:self.font.pointSize] range:range];
}
else
{
attributedText = [[NSMutableAttributedString alloc] initWithString:self.text] ;
[attributedText setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize]} range:range];
}
self.attributedText = attributedText;
}
- (void) boldSubstring: (NSString*) substring {
NSRange range = [self.text rangeOfString:substring];
[self boldRange:range];
}
- (void) colorRange: (NSRange) range AndColor:(UIColor*) color {
if (![self respondsToSelector:@selector(setAttributedText:)]) {
return;
}
NSMutableAttributedString *attributedText= nil;
if(self.attributedText)
{
attributedText = [self.attributedText mutableCopy];
[attributedText addAttribute:NSForegroundColorAttributeName value:color range:range];
}
else
{
attributedText = [[NSMutableAttributedString alloc] initWithString:self.text] ;
[attributedText setAttributes:@{NSForegroundColorAttributeName:color} range:range];
}
self.attributedText = attributedText;
}
- (void) colorSubstring: (NSString*) substring AndColor:(UIColor*) color {
NSRange range = [self.text rangeOfString:substring];
[self colorRange:range AndColor:color];
}
- (void) increaseFontSizeRange: (NSRange) range AndSize:(CGFloat) size {
if (![self respondsToSelector:@selector(setAttributedText:)]) {
return;
}
NSMutableAttributedString *attributedText= nil;
if(self.attributedText)
{
attributedText = [self.attributedText mutableCopy];
UIFont *font = [self.font fontWithSize:self.font.pointSize + size];
[attributedText addAttribute:NSFontAttributeName value:font range:range];
}
else
{
attributedText = [[NSMutableAttributedString alloc] initWithString:self.text] ;
UIFont *font = [UIFont fontWithName:self.font.fontName size:self.font.pointSize + size ];
[attributedText setAttributes:@{NSFontAttributeName:font} range:range];
}
self.attributedText = attributedText;
}
- (void) increaseFontSizeSubstring: (NSString*) substring AndSize:(CGFloat) size {
NSRange range = [self.text rangeOfString:substring];
[self increaseFontSizeRange:range AndSize:size];
}
- (void) decreaseFontSizeRange: (NSRange) range AndSize:(CGFloat) size {
if (![self respondsToSelector:@selector(setAttributedText:)]) {
return;
}
NSMutableAttributedString *attributedText= nil;
if(self.attributedText)
{
attributedText = [self.attributedText mutableCopy];
UIFont *font = [self.font fontWithSize:self.font.pointSize - size];
[attributedText addAttribute:NSFontAttributeName value:font range:range];
}
else
{
attributedText = [[NSMutableAttributedString alloc] initWithString:self.text] ;
UIFont *font = [UIFont fontWithName:self.font.fontName size:self.font.pointSize + size ];
[attributedText setAttributes:@{NSFontAttributeName:font} range:range];
}
self.attributedText = attributedText;
}
- (void) decreaseFontSizeSubstring: (NSString*) substring AndSize:(CGFloat) size {
NSRange range = [self.text rangeOfString:substring];
[self decreaseFontSizeRange:range AndSize:size];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment