2015-10-01 18:19:52 +02:00
|
|
|
#import "TGNeoLabelViewModel.h"
|
|
|
|
|
|
|
|
@implementation TGNeoLabelViewModel
|
|
|
|
|
|
|
|
- (instancetype)initWithText:(NSString *)text font:(UIFont *)font color:(UIColor *)color attributes:(NSDictionary *)attributes
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil)
|
|
|
|
{
|
|
|
|
_text = text;
|
|
|
|
_multiline = true;
|
|
|
|
|
|
|
|
NSMutableDictionary *finalAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
|
|
|
|
finalAttributes[NSFontAttributeName] = font;
|
|
|
|
finalAttributes[NSForegroundColorAttributeName] = color;
|
|
|
|
_attributes = finalAttributes;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithAttributedText:(NSAttributedString *)attributedText
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil)
|
|
|
|
{
|
|
|
|
_attributedText = attributedText;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGSize)contentSizeWithContainerSize:(CGSize)containerSize
|
|
|
|
{
|
|
|
|
NSAttributedString *string = nil;
|
|
|
|
|
|
|
|
if (_attributedText != nil)
|
|
|
|
string = _attributedText;
|
|
|
|
else if (self.text.length > 0)
|
|
|
|
string = [[NSAttributedString alloc] initWithString:self.text attributes:_attributes];
|
|
|
|
else
|
|
|
|
string = [[NSAttributedString alloc] initWithString:@" "];
|
|
|
|
|
2016-02-25 01:03:51 +01:00
|
|
|
CGSize contentSize = [string boundingRectWithSize:containerSize options:[self _stringDrawingOptionsForMetrics:true] context:nil].size;
|
2015-10-01 18:19:52 +02:00
|
|
|
contentSize.width = ceilf(contentSize.width);
|
|
|
|
contentSize.height = ceilf(contentSize.height);
|
|
|
|
|
|
|
|
return contentSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drawInContext:(CGContextRef)context
|
|
|
|
{
|
|
|
|
UIGraphicsPushContext(context);
|
2016-02-25 01:03:51 +01:00
|
|
|
NSStringDrawingOptions options = [self _stringDrawingOptionsForMetrics:false];
|
2015-10-01 18:19:52 +02:00
|
|
|
if (self.attributedText.length > 0)
|
2016-02-25 01:03:51 +01:00
|
|
|
[self.attributedText drawWithRect:self.bounds options:options context:nil];
|
2015-10-01 18:19:52 +02:00
|
|
|
else if (self.text.length > 0)
|
2016-02-25 01:03:51 +01:00
|
|
|
[self.text drawWithRect:self.bounds options:options attributes:self.attributes context:nil];
|
2015-10-01 18:19:52 +02:00
|
|
|
UIGraphicsPopContext();
|
|
|
|
}
|
|
|
|
|
2016-02-25 01:03:51 +01:00
|
|
|
- (NSStringDrawingOptions)_stringDrawingOptionsForMetrics:(bool)forMetrics
|
2015-10-01 18:19:52 +02:00
|
|
|
{
|
2016-02-25 01:03:51 +01:00
|
|
|
NSStringDrawingOptions options = kNilOptions;
|
|
|
|
if (self.multiline || !forMetrics)
|
|
|
|
options |= NSStringDrawingUsesLineFragmentOrigin;
|
|
|
|
|
2015-10-01 18:19:52 +02:00
|
|
|
if (!self.multiline)
|
|
|
|
options |= NSStringDrawingTruncatesLastVisibleLine;
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|