-
Notifications
You must be signed in to change notification settings - Fork 25
/
NativeDrawable.m
69 lines (57 loc) · 1.93 KB
/
NativeDrawable.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
//
// NativeDrawable.m
// Client
//
// Created by Krzysztof Modras on 29.08.19.
// Copyright © 2019 Cliqz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <React/RCTViewManager.h>
@interface NativeDrawableImageView : UIImageView
@property BOOL hasTint;
@end
@implementation NativeDrawableImageView
@end
@interface NativeDrawable : RCTViewManager
@end
@implementation NativeDrawable
RCT_EXPORT_MODULE()
- (UIImageView *)view {
NativeDrawableImageView* imageView = [[NativeDrawableImageView alloc] init];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
return imageView;
}
RCT_CUSTOM_VIEW_PROPERTY(color, NSString, UIImageView) {
NSString *color_str = (NSString*)json;
UIColor* uicolor = [NativeDrawable colorFromHexString:color_str];
if (uicolor) {
view.image = [view.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[view setTintColor: uicolor];
[(NativeDrawableImageView*)view setHasTint:YES];
} else {
NSLog(@"color %@ is not valid", json);
}
}
RCT_CUSTOM_VIEW_PROPERTY(source, NSString, UIImageView) {
NSString *imageName = (NSString*)json;
if (imageName) {
UIImage* image = [UIImage imageNamed:imageName];
if (((NativeDrawableImageView*) view).hasTint) {
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
if (image) {
[view setImage:image];
} else {
NSLog(@"image %@ is missing", imageName);
}
}
}
+ (UIColor *)colorFromHexString:(NSString *)hexString {
unsigned rgbValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&rgbValue];
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
}
@end