Forked from andreacremaschi/ MarkdownAttributedLabel
Last active
August 29, 2015 14:10
-
-
Save AlessioAnesa/1eabbef545cc3d8df6b6 to your computer and use it in GitHub Desktop.
MarkdownAttributeLabel using XNGMarkdownParser
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
A TTTAttributedLabel convenience subclass that uses MMarkdown to parse markdown text. |
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
The MIT License (MIT) | |
Copyright (c) 2015 MidaApp s.r.l. | |
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. |
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
// | |
// 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 |
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
// | |
// MarkdownAttributedLabel.m | |
// | |
// Created by Andrea Cremaschi on 20/03/14. | |
// Copyright (c) 2014 midapp. All rights reserved. | |
// | |
#import "MarkdownAttributedLabel.h" | |
#import <XNGMarkdownParser/XNGMarkdownParser.h> | |
#import <XNGMarkdownParser/XNGMarkdownTokens.h> | |
#import <TTTAttributedLabel/TTTAttributedLabel.h> | |
@interface XNGMarkdownParser () | |
- (void)consumeToken:(XNGMarkdownParserCode)token text:(char *)text; | |
@end | |
@interface MidaMarkdownParser : XNGMarkdownParser | |
@end | |
@interface NSAttributedString (FontSize) | |
- (NSAttributedString*) attributedStringWithFontSize:(CGFloat) fontSize; | |
@end | |
@implementation MarkdownAttributedLabel | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) | |
{ | |
[self commonInitLocal]; | |
} | |
return self; | |
} | |
- (instancetype)initWithCoder:(NSCoder *)coder | |
{ | |
self = [super initWithCoder:coder]; | |
if (self) | |
{ | |
[self commonInitLocal]; | |
} | |
return self; | |
} | |
- (void)commonInitLocal | |
{// Initialization code | |
self.enabledTextCheckingTypes = 0; | |
self.dataDetectorTypes = 0; | |
self.extendsLinkTouchArea = NO; | |
NSMutableDictionary *linkAttributes = [self.linkAttributes mutableCopy]; | |
[linkAttributes removeObjectForKey:NSParagraphStyleAttributeName]; | |
self.linkAttributes = [linkAttributes copy]; | |
linkAttributes = [self.activeLinkAttributes mutableCopy]; | |
[linkAttributes removeObjectForKey:NSParagraphStyleAttributeName]; | |
self.activeLinkAttributes = [linkAttributes copy]; | |
linkAttributes = [self.inactiveLinkAttributes mutableCopy]; | |
[linkAttributes removeObjectForKey:NSParagraphStyleAttributeName]; | |
self.inactiveLinkAttributes = [linkAttributes copy]; | |
} | |
- (void)setFont:(UIFont *)font | |
{ | |
BOOL sizeMatters = self.font.pointSize != font.pointSize; | |
[super setFont:font]; | |
if (sizeMatters) | |
{ | |
self.attributedText = [self.attributedText attributedStringWithFontSize:font.pointSize]; | |
} | |
} | |
- (NSParagraphStyle *)currentParagraphStyle | |
{ | |
TTTAttributedLabel *label = (TTTAttributedLabel*) self; | |
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; | |
paragraphStyle.alignment = label.textAlignment; | |
paragraphStyle.lineSpacing = label.lineSpacing; | |
paragraphStyle.minimumLineHeight = label.minimumLineHeight > 0 ? label.minimumLineHeight : label.font.lineHeight * label.lineHeightMultiple; | |
paragraphStyle.maximumLineHeight = label.maximumLineHeight > 0 ? label.maximumLineHeight : label.font.lineHeight * label.lineHeightMultiple; | |
paragraphStyle.lineHeightMultiple = label.lineHeightMultiple; | |
paragraphStyle.firstLineHeadIndent = label.firstLineIndent; | |
return paragraphStyle; | |
} | |
-(void)setMarkdownText: (NSString*)markdown { | |
NSError *error; | |
if (markdown == nil) { | |
[self setText:nil]; | |
return; | |
} | |
XNGMarkdownParser *parser = [[MidaMarkdownParser alloc] init]; | |
NSAttributedString *attributedString = [parser attributedStringFromMarkdownString:markdown]; | |
TTTAttributedLabel *label = (TTTAttributedLabel*) self; | |
NSParagraphStyle *paragraphStyle = [self currentParagraphStyle]; | |
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString]; | |
[mutableAttributedString addAttributes:@{ NSParagraphStyleAttributeName : paragraphStyle } range:NSMakeRange(0, [mutableAttributedString length])]; | |
[label setText:[mutableAttributedString attributedStringWithFontSize:self.font.pointSize]]; | |
} | |
- (void)setBounds:(CGRect)bounds | |
{ | |
[super setBounds:bounds]; | |
// If this is a multiline label, need to make sure | |
// preferredMaxLayoutWidth always matches the frame width | |
// (i.e. orientation change can mess this up) | |
if (self.numberOfLines == 0 && bounds.size.width != self.preferredMaxLayoutWidth) | |
{ | |
self.preferredMaxLayoutWidth = self.bounds.size.width; | |
[self setNeedsUpdateConstraints]; | |
} | |
} | |
@end | |
@implementation MidaMarkdownParser | |
- (void)consumeToken:(XNGMarkdownParserCode)token text:(char *)text | |
{ | |
if (token == MARKDOWN_URL) | |
{ | |
token = MARKDOWN_UNKNOWN; | |
} | |
[super consumeToken:token text:text]; | |
} | |
@end | |
@implementation NSAttributedString (FontSize) | |
- (NSAttributedString*) attributedStringWithFontSize:(CGFloat) fontSize | |
{ | |
NSMutableAttributedString* attributedString = [self mutableCopy]; | |
{ | |
[attributedString beginEditing]; | |
[attributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) | |
{ | |
UIFont* font = value; | |
font = [font fontWithSize:fontSize]; | |
[attributedString removeAttribute:NSFontAttributeName range:range]; | |
[attributedString addAttribute:NSFontAttributeName value:font range:range]; | |
}]; | |
[attributedString endEditing]; | |
} | |
return [attributedString copy]; | |
} | |
@end |
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
Pod::Spec.new do |s| | |
s.name = "MarkdownAttributedLabel" | |
s.version = "0.0.3" | |
s.summary = "A TTTAttributedLabel convenience subclass that uses XNGMarkdownParser to parse markdown text." | |
s.description = <<-DESC | |
DESC | |
s.homepage = "https://gist.github.com/ec7f6569fbb612778031.git" | |
s.license = "MIT" | |
s.author = { "Andrea Cremaschi" => "[email protected]" } | |
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 'XNGMarkdownParser' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment