Skip to content

Instantly share code, notes, and snippets.

@guvener
Last active December 20, 2015 13:09
Show Gist options
  • Save guvener/6136836 to your computer and use it in GitHub Desktop.
Save guvener/6136836 to your computer and use it in GitHub Desktop.
Simply applying partial bold style between tags inside UILabel class category. Usage: #import "UILabel+BoldTag.h" // import the category [lbl_player setText:@"You [b]want to play[/b]"]; [lbl_player renderBoldTag]; // render bold tag
//
// UILabel+BoldTag.h
//
// Created by Guvener Gokce on 8/2/13.
// Copyright (c) 2013 Gladly Interactive All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UILabel (BoldTag)
-(void)renderBoldTag;
@end
//
// UILabel+BoldTag.m
//
// Created by Guvener Gokce on 8/2/13.
// Copyright (c) 2013 Gladly Interactive All rights reserved.
//
#import "UILabel+BoldTag.h"
@implementation UILabel (BoldTag)
-(void)renderBoldTag
{
if ([self respondsToSelector:@selector(setAttributedText:)])
{
UIFont *boldFont = [UIFont boldSystemFontOfSize:self.font.pointSize];
// Create the attributes
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
boldFont, NSFontAttributeName,nil];
NSString *aText = [NSString stringWithFormat:@"%@",self.text];
NSRange start = [aText rangeOfString:@"[b]"];
NSRange end = [aText rangeOfString:@"[/b]"];
if(end.location > 0)
{
aText = [aText stringByReplacingOccurrencesOfString:@"[b]" withString:@""];
aText = [aText stringByReplacingOccurrencesOfString:@"[/b]" withString:@""];
[self setText:aText];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:self.text
attributes:nil];
NSRange boldRange = NSMakeRange(start.location, end.location-start.location-3);
[attributedText setAttributes:subAttrs range:boldRange];
[self setAttributedText:attributedText];
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment