Skip to content

Commit

Permalink
Merge pull request apache#3084 from wqyfavor/dark-input
Browse files Browse the repository at this point in the history
[iOS] Support dark scheme for input controls.
  • Loading branch information
wqyfavor authored Dec 16, 2019
2 parents f29a6f2 + 36ccbb1 commit 4f73e51
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 17 deletions.
4 changes: 3 additions & 1 deletion ios/sdk/WeexSDK/Sources/Component/WXEditComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
@interface WXEditComponent : WXComponent<WXDatePickerManagerDelegate,UITextViewDelegate,UITextFieldDelegate>

//attribute
@property (nonatomic, strong) UIColor *placeholderColor;
@property (atomic, strong) UIColor *placeholderColor;
@property (atomic, strong) UIColor *darkSchemePlaceholderColor;
@property (atomic, strong) UIColor *lightSchemePlaceholderColor;
@property (nonatomic, strong) NSString *placeholderString;
@property (nonatomic, strong) UILabel *placeHolderLabel;

Expand Down
101 changes: 89 additions & 12 deletions ios/sdk/WeexSDK/Sources/Component/WXEditComponent.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#import "WXComponent+PseudoClassManagement.h"
#import "WXTextInputComponent.h"
#import "WXComponent+Layout.h"
#import "WXComponent_internal.h"

@interface WXEditComponent()
{
Expand All @@ -52,7 +53,9 @@ @interface WXEditComponent()
@property (nonatomic) WXTextStyle fontStyle;
@property (nonatomic) CGFloat fontWeight;
@property (nonatomic, strong) NSString *fontFamily;
@property (nonatomic, strong) UIColor *colorForStyle;
@property (atomic, strong) UIColor *colorForStyle;
@property (atomic, strong) UIColor *darkSchemeColorForStyle;
@property (atomic, strong) UIColor *lightSchemeColorForStyle;
@property (nonatomic)NSTextAlignment textAlignForStyle;

//event
Expand Down Expand Up @@ -142,7 +145,16 @@ - (instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDict

// handle styles
if (styles[@"color"]) {
_colorForStyle = [WXConvert UIColor:styles[@"color"]];
self.colorForStyle = [WXConvert UIColor:styles[@"color"]];
}
else {
self.colorForStyle = [UIColor blackColor];
}
if (styles[@"weexDarkSchemeColor"]) {
self.darkSchemeColorForStyle = [WXConvert UIColor:styles[@"weexDarkSchemeColor"]];
}
if (styles[@"weexLightSchemeColor"]) {
self.lightSchemeColorForStyle = [WXConvert UIColor:styles[@"weexLightSchemeColor"]];
}
if (styles[@"fontSize"]) {
_fontSize = [WXConvert WXPixelType:styles[@"fontSize"] scaleFactor:self.weexInstance.pixelScaleFactor];
Expand All @@ -160,9 +172,15 @@ - (instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDict
_textAlignForStyle = [WXConvert NSTextAlignment:styles[@"textAlign"]];
}
if (styles[@"placeholderColor"]) {
_placeholderColor = [WXConvert UIColor:styles[@"placeholderColor"]];
self.placeholderColor = [WXConvert UIColor:styles[@"placeholderColor"]];
}else {
_placeholderColor = [UIColor colorWithRed:0x99/255.0 green:0x99/255.0 blue:0x99/255.0 alpha:1.0];
self.placeholderColor = [UIColor colorWithRed:0x99/255.0 green:0x99/255.0 blue:0x99/255.0 alpha:1.0];
}
if (styles[@"weexDarkSchemePlaceholderColor"]) {
self.darkSchemePlaceholderColor = [WXConvert UIColor:styles[@"weexDarkSchemePlaceholderColor"]];
}
if (styles[@"weexLightSchemePlaceholderColor"]) {
self.lightSchemePlaceholderColor = [WXConvert UIColor:styles[@"weexLightSchemePlaceholderColor"]];
}
}

Expand All @@ -187,7 +205,7 @@ - (void)viewDidLoad
[self setTextFont];
[self setPlaceholderAttributedString];
[self setTextAlignment];
[self setTextColor:_colorForStyle];
[self setTextColor:[self.weexInstance chooseColor:self.colorForStyle lightSchemeColor:self.lightSchemeColorForStyle darkSchemeColor:self.darkSchemeColorForStyle invert:self.invertForDarkScheme scene:WXColorSceneText]];
[self setText:_value];
[self setEnabled:!_disabled];
[self setRows:_rows];
Expand Down Expand Up @@ -300,6 +318,11 @@ - (void)setText:(NSString *)text
{
}

- (void)_setTextColor
{
[self setTextColor:[self.weexInstance chooseColor:self.colorForStyle lightSchemeColor:self.lightSchemeColorForStyle darkSchemeColor:self.darkSchemeColorForStyle invert:self.invertForDarkScheme scene:WXColorSceneText]];
}

-(void)setTextColor:(UIColor *)color
{
}
Expand Down Expand Up @@ -479,10 +502,23 @@ - (void)updateAttributes:(NSDictionary *)attributes

- (void)updateStyles:(NSDictionary *)styles
{
BOOL colorChanged = NO;
if (styles[@"color"]) {
_colorForStyle = [WXConvert UIColor:styles[@"color"]];
[self setTextColor:_colorForStyle];
self.colorForStyle = [WXConvert UIColor:styles[@"color"]];
colorChanged = YES;
}
if (styles[@"weexDarkSchemeColor"]) {
self.darkSchemeColorForStyle = [WXConvert UIColor:styles[@"weexDarkSchemeColor"]];
colorChanged = YES;
}
if (styles[@"weexLightSchemeColor"]) {
self.lightSchemeColorForStyle = [WXConvert UIColor:styles[@"weexLightSchemeColor"]];
colorChanged = YES;
}
if (colorChanged) {
[self _setTextColor];
}

if (styles[@"fontSize"]) {
_fontSize = [WXConvert WXPixelType:styles[@"fontSize"] scaleFactor:self.weexInstance.pixelScaleFactor];
}
Expand All @@ -501,12 +537,26 @@ - (void)updateStyles:(NSDictionary *)styles
_textAlignForStyle = [WXConvert NSTextAlignment:styles[@"textAlign"]];
[self setTextAlignment:_textAlignForStyle] ;
}

BOOL placeholderColorChanged = NO;
if (styles[@"placeholderColor"]) {
_placeholderColor = [WXConvert UIColor:styles[@"placeholderColor"]];
self.placeholderColor = [WXConvert UIColor:styles[@"placeholderColor"]];
placeholderColorChanged = YES;
}else {
_placeholderColor = [UIColor colorWithRed:0x99/255.0 green:0x99/255.0 blue:0x99/255.0 alpha:1.0];
self.placeholderColor = [UIColor colorWithRed:0x99/255.0 green:0x99/255.0 blue:0x99/255.0 alpha:1.0];
}
[self setPlaceholderAttributedString];
if (styles[@"weexDarkSchemePlaceholderColor"]) {
self.darkSchemePlaceholderColor = [WXConvert UIColor:styles[@"weexDarkSchemePlaceholderColor"]];
placeholderColorChanged = YES;
}
if (styles[@"weexLightSchemePlaceholderColor"]) {
self.lightSchemePlaceholderColor = [WXConvert UIColor:styles[@"weexLightSchemePlaceholderColor"]];
placeholderColorChanged = YES;
}
if (placeholderColorChanged && [self.placeHolderLabel.text length] > 0) {
[self setPlaceholderAttributedString];
}

[self updatePattern];
}

Expand Down Expand Up @@ -825,8 +875,9 @@ - (BOOL)isPureInt:(NSString*)textString

- (void)setPlaceholderAttributedString
{
UIColor* placeholderColor = [self.weexInstance chooseColor:self.placeholderColor lightSchemeColor:self.lightSchemePlaceholderColor darkSchemeColor:self.darkSchemePlaceholderColor invert:self.invertForDarkScheme scene:WXColorSceneText];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_placeholderString];
[attributedString addAttribute:NSForegroundColorAttributeName value:_placeholderColor range:NSMakeRange(0, _placeholderString.length)];
[attributedString addAttribute:NSForegroundColorAttributeName value:placeholderColor range:NSMakeRange(0, _placeholderString.length)];
UIFont *font = [WXUtility fontWithSize:_fontSize textWeight:_fontWeight textStyle:_fontStyle fontFamily:_fontFamily scaleFactor:self.weexInstance.pixelScaleFactor];
[self setAttributedPlaceholder:attributedString font:font];
}
Expand Down Expand Up @@ -986,13 +1037,39 @@ - (void)closeKeyboard
#pragma mark -reset color
- (void)resetStyles:(NSArray *)styles
{
BOOL colorChanged = NO;
if ([styles containsObject:@"color"]) {
[self setTextColor:[UIColor blackColor]];
self.colorForStyle = [UIColor blackColor];
colorChanged = YES;
}
if ([styles containsObject:@"weexDarkSchemeColor"]) {
self.darkSchemeColorForStyle = nil;
colorChanged = YES;
}
if ([styles containsObject:@"weexLightSchemeColor"]) {
self.lightSchemeColorForStyle = nil;
colorChanged = YES;
}
if (colorChanged) {
[self _setTextColor];
}

if ([styles containsObject:@"fontSize"]) {
_fontSize = WX_TEXT_FONT_SIZE;
[self setTextFont];
}
}

- (void)schemeDidChange:(NSString*)scheme
{
[super schemeDidChange:scheme];
if (_view) {
[self _setTextColor];
if ([self.placeHolderLabel.text length] > 0) {
[self setPlaceholderAttributedString];
}
}
}

@end

8 changes: 4 additions & 4 deletions ios/sdk/WeexSDK/Sources/Component/WXTextAreaComponent.mm
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ -(void)setEditBorder:(UIEdgeInsets)border

-(void)setAttributedPlaceholder:(NSMutableAttributedString *)attributedString font:(UIFont *)font
{
if (self.placeholderColor) {
[attributedString addAttribute:NSForegroundColorAttributeName value:self.placeholderColor range:NSMakeRange(0, self.placeholderString.length)];
[attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.placeholderString.length)];
}
UIColor* placeholderColor = [self.weexInstance chooseColor:self.placeholderColor lightSchemeColor:self.lightSchemePlaceholderColor darkSchemeColor:self.darkSchemePlaceholderColor invert:self.invertForDarkScheme scene:WXColorSceneText];
[attributedString addAttribute:NSForegroundColorAttributeName value:placeholderColor range:NSMakeRange(0, self.placeholderString.length)];
[attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.placeholderString.length)];

self.placeHolderLabel.backgroundColor = [UIColor clearColor];
CGRect expectedLabelSize = [attributedString boundingRectWithSize:(CGSize){self.view.frame.size.width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
Expand Down

0 comments on commit 4f73e51

Please sign in to comment.