-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSAttributedString+Convenience.m
83 lines (60 loc) · 2.82 KB
/
NSAttributedString+Convenience.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// NSAttributedString+Convenience.m
// Ringtonic
//
// Created by Alexander Ivanov on 30.04.17.
// Copyright © 2017 Alexander Ivanov. All rights reserved.
//
#import "NSAttributedString+Convenience.h"
@implementation NSAttributedString (Convenience)
- (NSAttributedString *)addAttribute:(NSString *)attribute withValue:(id)value andRange:(NSRange)range {
NSMutableAttributedString *mutable = [self isKindOfClass:[NSMutableAttributedString class]]
? (NSMutableAttributedString *)self
: [self mutableCopy];
[mutable addAttribute:attribute value:value range:range];
return mutable;
}
- (NSAttributedString *)addAttribute:(NSString *)attribute withValue:(id)value {
return [self addAttribute:attribute withValue:value andRange:NSMakeRange(0, [self length])];
}
- (NSAttributedString *)font:(UIFont *)font forRange:(NSRange)range {
return [self addAttribute:NSFontAttributeName withValue:font andRange:range];
}
- (NSAttributedString *)font:(UIFont *)font {
return [self font:font forRange:NSMakeRange(0, [self length])];
}
- (NSAttributedString *)strikethrough:(NSNumber *)styleAndPattern forRange:(NSRange)range {
return [self addAttribute:NSStrikethroughStyleAttributeName withValue:styleAndPattern andRange:range];
}
- (NSAttributedString *)strikethrough:(NSRange)range {
return [self strikethrough:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) forRange:range];
}
- (NSAttributedString *)strikethrough {
return [self strikethrough:NSMakeRange(0, [self length])];
}
- (NSAttributedString *)underline:(NSNumber *)styleAndPattern forRange:(NSRange)range {
return [self addAttribute:NSUnderlineStyleAttributeName withValue:styleAndPattern andRange:range];
}
- (NSAttributedString *)underline:(NSRange)range {
return [self underline:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) forRange:range];
}
- (NSAttributedString *)underline {
return [self underline:NSMakeRange(0, [self length])];
}
+ (instancetype)attributedStringWithString:(NSString *)str attributes:(NSDictionary<NSString *, id> *)attrs {
return str ? [[self alloc] initWithString:str attributes:attrs] : Nil;
}
+ (instancetype)attributedStringWithString:(NSString *)str {
return str ? [[self alloc] initWithString:str] : Nil;
}
+ (instancetype)attributedStringWithHTMLString:(NSString *)htmlString encoding:(NSStringEncoding)encoding {
NSData *data = [htmlString dataUsingEncoding:encoding];
NSError *error = Nil;
NSAttributedString *string = [[NSAttributedString alloc] initWithData:data options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute : @(encoding) } documentAttributes:Nil error:&error];
[error log:@"initWithData:"];
return string;
}
+ (instancetype)attributedStringWithHTMLString:(NSString *)htmlString {
return [self attributedStringWithHTMLString:htmlString encoding:NSUTF8StringEncoding];
}
@end