美文网首页iOS随笔小记
dataPickerView 在iOS 14和numberPic

dataPickerView 在iOS 14和numberPic

作者: 七一小月 | 来源:发表于2021-02-25 16:36 被阅读0次

    一、dataPickerView 在iOS 14下设置textColor失效

    1.ios14之前,设置下面这一句就可以更改dataPickerView的字体颜色

    [self.datePicker setValue: [UIColor whiteColor] forKey: @"textColor"];
    

    但是更新到新系统iOS 14之后,发现字体颜色设置失效了
    经过重重测验,最终找到了解决之路......

    2. iOS 14系统下设置dataPickerView的字体颜色,直接调用changeDatePickerViewTextColor就可以啦,over.....

    -(void) changeDatePickerViewTextColor {
      if (@available(iOS 13.4, *)) {
          self.datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
      }
      //Set white text
      unsigned int outCount;
      int i;
      objc_property_t *pProperty = class_copyPropertyList([UIDatePicker class], &outCount);
      for (i = outCount -1; i >= 0; i--) {
        //Get the name of the property in a loop The property_getName function returns the name of a property
        NSString *getPropertyName = [NSString stringWithCString:property_getName(pProperty[i]) encoding:NSUTF8StringEncoding];
        NSString *getPropertyNameString = [NSString stringWithCString:property_getAttributes(pProperty[i]) encoding:NSUTF8StringEncoding];
        if([getPropertyName isEqualToString:@"textColor"]) {
            [self.datePicker setValue:[UIColor whiteColor] forKey:@"textColor"];
        }
      }
      SEL selector = NSSelectorFromString(@"setHighlightsToday:");
      NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDatePicker instanceMethodSignatureForSelector:selector]];
      BOOL no = NO;
      [invocation setSelector:selector];
      [invocation setArgument:&no atIndex:2];
      [invocation invokeWithTarget:self.datePicker];
     }
    

    二、numberPicker在Android 10下setNumberPickerTextColor失效

    1.Android 10之前,设置下面这一句就可以更改numberPicker的字体颜色

    SalusUtil.setNumberPickerTextColor(numHours, Color.WHITE);
    

    2.Android 10之后设置dataPickerView的字体颜色

    在styles.xml中添加:

    <style name="numberPicker_textColor" parent="AppTheme" >
      <item name="android:textColorPrimary">@android:color/white</item>
    </style>
    

    然后在您的布局中像这样使用它:

    <NumberPicker
       android:id="@+id/numberPicker_hours"
       android:theme="@style/numberPicker_textColor"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:showDividers="none" >
    </NumberPicker>
    

    太难了!!!!!!Android 10这个,作为一个iOS开发,我真的百度了N久,最终在Google上找到了解决方法,以此写个记录吧,让有相同困境的伙伴早点解决问题

    --------------------------------------------------------------

    再次发现,Android10失效的本质其实是Android10针对非 SDK 接口的进行了限制,所以有更本质的方法解决setNumberPickerTextColor里面SDK失效的问题(Android10一直走catch)
    public static boolean setNumberPickerTextColor(NumberPicker numberPicker,
            int color) {
        final int count = numberPicker.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = numberPicker.getChildAt(i);
    
            if (child instanceof EditText) {
                try {
                    Field selectorWheelPaintField = numberPicker.getClass()
                            .getDeclaredField("mSelectorWheelPaint");
                    selectorWheelPaintField.setAccessible(true);
                    ((Paint) selectorWheelPaintField.get(numberPicker))
                            .setColor(color);
                    Paint paint = (Paint) selectorWheelPaintField
                            .get(numberPicker);
                    // paint.setTextAlign(Align.RIGHT);
    
                    ((EditText) child).setTextColor(color);
                    /*
                     * ((EditText) child).setPadding(150, 0,0,0); ((TextView)
                     * child).setPadding(150, 0,0,0);
                     */
                    numberPicker.invalidate();
                    return true;
                } catch (NoSuchFieldException e) {
                    Log.w("setNumberPickerTextColor", e);
                } catch (IllegalAccessException e) {
                    Log.w("setNumberPickerTextColor", e);
                } catch (IllegalArgumentException e) {
                    Log.w("setNumberPickerTextColor", e);
                }
            }
        }
        return false;
    }
    
    Android 10(API 级别 29)

    如需允许访问非 SDK 接口,请使用以下 adb 命令:

          $ adb shell settings put global hidden_api_policy  1
    
    Android 9(API 级别 28)

    如需允许访问非 SDK 接口,请使用以下 adb 命令:

          $adb shell settings put global hidden_api_policy_pre_p_apps  1
          $adb shell settings put global hidden_api_policy_p_apps 1
    

    相关文章

      网友评论

        本文标题:dataPickerView 在iOS 14和numberPic

        本文链接:https://www.haomeiwen.com/subject/kpjufltx.html