Skip to content

Instantly share code, notes, and snippets.

@andreacremaschi
Last active August 29, 2015 14:02
Show Gist options
  • Save andreacremaschi/ec7f6569fbb612778031 to your computer and use it in GitHub Desktop.
Save andreacremaschi/ec7f6569fbb612778031 to your computer and use it in GitHub Desktop.
MarkdownAttributedLabel pod
A TTTAttributedLabel convenience subclass that uses MMarkdown to parse markdown text.
The MIT License (MIT)
Copyright (c) 2014 Andrea Cremaschi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
//
// MarkdownAttributedLabel.h
//
// Created by Andrea Cremaschi on 20/03/14.
// Copyright (c) 2014 midapp. All rights reserved.
//
#import <TTTAttributedLabel/TTTAttributedLabel.h>
@interface MarkdownAttributedLabel : TTTAttributedLabel
-(void)setMarkdownText: (NSString*)markdown;
@end
//
// MarkdownAttributedLabel.m
//
// Created by Andrea Cremaschi on 20/03/14.
// Copyright (c) 2014 midapp. All rights reserved.
//
#import "MarkdownAttributedLabel.h"
#import <MMMarkDown/MMMarkdown.h>
#import <TTTAttributedLabel/TTTAttributedLabel.h>
@implementation MarkdownAttributedLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)setMarkdownText: (NSString*)markdown {
NSError *error;
if (markdown == nil) {
[self setText:nil];
return;
}
NSString *html = [MMMarkdown HTMLStringWithMarkdown:markdown error:nil];
NSString *fontName = self.font.fontName;
NSString *fontSize = [[NSNumber numberWithFloat:self.font.pointSize] stringValue];
NSString *lineHeight = [[NSNumber numberWithFloat:self.font.pointSize * self.lineHeightMultiple] stringValue];
NSString *textAlignment;
switch (self.textAlignment) {
case NSTextAlignmentCenter: textAlignment = @"center"; break;
case NSTextAlignmentLeft: textAlignment = @"left"; break;
case NSTextAlignmentRight: textAlignment = @"right"; break;
case NSTextAlignmentJustified: textAlignment = @"justify"; break;
default: textAlignment = @"left";break;
};
NSString *fontFamilyName = [self.font.familyName stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray *strongFontSuffixes = @[@"Bold", @"-Bold", @"Medium", @"-Medium"];
NSMutableArray *mArray = [NSMutableArray array];
[mArray addObject: [NSString stringWithFormat:@"\"%@ Bold\"", fontFamilyName]];
[mArray addObject: [NSString stringWithFormat:@"\"%@ Medium\"", fontFamilyName]];
for (NSString *suffix in strongFontSuffixes) {
[mArray addObject: [NSString stringWithFormat:@"%@%@", fontFamilyName, suffix]];
}
NSString *strongFontNames = [mArray componentsJoinedByString:@","];
html = [NSString stringWithFormat:@"<head><style> p {font-family:%@;font-size:%@px;line-height:%@px;text-%@} strong {font-family:%@}</style></head><body>%@</body>", fontName, fontSize, lineHeight, textAlignment, strongFontNames, html];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF16StringEncoding]
options:options
documentAttributes:nil
error:&error];
TTTAttributedLabel *attributedLabel = (TTTAttributedLabel*) self;
[attributedLabel setText:attributedString];
}
@end
Pod::Spec.new do |s|
s.name = "MarkdownAttributedLabel"
s.version = "0.0.1"
s.summary = "A TTTAttributedLabel convenience subclass that uses MMarkdown to parse markdown text."
s.description = <<-DESC
DESC
s.homepage = "https://gist.github.com/ec7f6569fbb612778031.git"
# s.license = "MIT"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Andrea Cremaschi" => "andrea.cremaschi@midainformatica.it" }
s.platform = :ios, "5.0"
s.source = { :git => "https://gist.github.com/ec7f6569fbb612778031.git", :tag => "0.0.1" }
s.source_files = "*.{h,m}"
s.exclude_files = "Classes/Exclude"
s.public_header_files = "*.h"
s.requires_arc = true
s.dependency 'TTTAttributedLabel'
s.dependency 'MMMarkdown'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment