iOS-获取控件的隐藏属性并修改

我们在开发的过程中用到的一些控件,明明一些属性我们一定会用到,但是是查看API就是没有,这个时候一般人会吐槽一下苹果爸爸。随后就是选择自己封装或者使用其他的实现方式来实现 。

其实很多的属性并不是没有,而是因为一些原因苹果将其隐藏了,这种隐藏的属性我们一般会选择两种方式来修改它:
一、就是使用RunTime
比较麻烦,不推荐使用。
二、就是使用KVC
使用KVC其实也是基于RunTime来实现的,但是在代码的可读性上还是比较好的。首先我们需要使用RunTime轮询一个控件的所有私有类,然后找到我们需要的属性。

unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([UITextView class], &count);
    for (int i = 0; i<count; i++) {
        Ivar ivar = ivars[i];

        NSLog(@"UITextView--->%s------%s", ivar_getName(ivar),ivar_getTypeEncoding(ivar));
    }

输出结果如下:

//    2017-06-01 17:32:45.359960 one[846:367764] UITextView--->_private------@
//    2017-06-01 17:32:45.360139 one[846:367764] UITextView--->_textStorage------@"NSTextStorage"
//    2017-06-01 17:32:45.360188 one[846:367764] UITextView--->_textContainer------@"NSTextContainer"
//    2017-06-01 17:32:45.360231 one[846:367764] UITextView--->_layoutManager------@"NSLayoutManager"
//    2017-06-01 17:32:45.360273 one[846:367764] UITextView--->_containerView------@"_UITextContainerView"
//    2017-06-01 17:32:45.360317 one[846:367764] UITextView--->_inputDelegate------@
//    2017-06-01 17:32:45.360358 one[846:367764] UITextView--->_tokenizer------@"<UITextInputTokenizer>"
//    2017-06-01 17:32:45.360399 one[846:367764] UITextView--->_inputController------@"UITextInputController"
//    2017-06-01 17:32:45.360442 one[846:367764] UITextView--->_interactionAssistant------@"UITextInteractionAssistant"
//    2017-06-01 17:32:45.360652 one[846:367764] UITextView--->_textInputTraits------@"UITextInputTraits"
//    2017-06-01 17:32:45.360700 one[846:367764] UITextView--->_autoscroll------@"UIAutoscroll"
//    2017-06-01 17:32:45.360745 one[846:367764] UITextView--->_tvFlags------{?="needsScrollToSelectionAfterLayout"b1"isInteractingWithLink"b1"linkInteractionIsLongPress"b1"linkInteractionIsPreview"b1"editable"b1"reentrancyGuard"b1"usesExplicitPreferredMaxLayoutWidth"b1"interactiveSelectionDisabled"b1"selectable"b1"shouldPresentSheetsInAWindowLayeredAboveTheKeyboard"b1"shouldAutoscrollAboveBottom"b1"disableUpdateTextColorOnTraitCollectionChange"b1}
//    2017-06-01 17:32:45.360830 one[846:367764] UITextView--->_contentSizeUpdateSeqNo------q
//    2017-06-01 17:32:45.360872 one[846:367764] UITextView--->_scrollTarget------@"_UITextViewRestorableScrollPosition"
//    2017-06-01 17:32:45.360916 one[846:367764] UITextView--->_scrollPositionDontRecordCount------Q
//    2017-06-01 17:32:45.360963 one[846:367764] UITextView--->_scrollPosition------@"_UITextViewRestorableScrollPosition"
//    2017-06-01 17:32:45.361006 one[846:367764] UITextView--->_offsetFromScrollPosition------d
//    2017-06-01 17:32:45.361047 one[846:367764] UITextView--->_linkInteractionItem------@
//    2017-06-01 17:32:45.361088 one[846:367764] UITextView--->_dataDetectorTypes------Q
//    2017-06-01 17:32:45.361157 one[846:367764] UITextView--->_preferredMaxLayoutWidth------d
//    2017-06-01 17:32:45.361199 one[846:367764] UITextView--->_placeholderLabel------@"UILabel"
//    2017-06-01 17:32:45.361291 one[846:367764] UITextView--->_inputAccessoryView------@"UIView"
//    2017-06-01 17:32:45.361401 one[846:367764] UITextView--->_linkTextAttributes------@"NSDictionary"
//    2017-06-01 17:32:45.361521 one[846:367764] UITextView--->_streamingManager------@"_UISiriStreamingManager"
//    2017-06-01 17:32:45.361619 one[846:367764] UITextView--->_characterStreamingManager------@"_UICharacterStreamingManager"
//    2017-06-01 17:32:45.361733 one[846:367764] UITextView--->_siriAnimationStyle------q
//    2017-06-01 17:32:45.361826 one[846:367764] UITextView--->_siriParameters------@"NSDictionary"
//    2017-06-01 17:32:45.361936 one[846:367764] UITextView--->_firstBaselineOffsetFromTop------d
//    2017-06-01 17:32:45.362023 one[846:367764] UITextView--->_lastBaselineOffsetFromBottom------d
//    2017-06-01 17:32:45.362125 one[846:367764] UITextView--->_cuiCatalog------@"CUICatalog"
//    2017-06-01 17:32:45.362245 one[846:367764] UITextView--->_beforeFreezingTextContainerInset------{UIEdgeInsets="top"d"left"d"bottom"d"right"d}
//    2017-06-01 17:32:45.362318 one[846:367764] UITextView--->_duringFreezingTextContainerInset------{UIEdgeInsets="top"d"left"d"bottom"d"right"d}
//    2017-06-01 17:32:45.362362 one[846:367764] UITextView--->_beforeFreezingFrameSize------{CGSize="width"d"height"d}
//    2017-06-01 17:32:45.362425 one[846:367764] UITextView--->_unfreezingTextContainerSize------B
//    2017-06-01 17:32:45.362519 one[846:367764] UITextView--->_adjustsFontForContentSizeCategory------B
//    2017-06-01 17:32:45.362561 one[846:367764] UITextView--->_clearsOnInsertion------B
//    2017-06-01 17:32:45.362655 one[846:367764] UITextView--->_multilineContextWidth------d
//    2017-06-01 17:32:45.362723 one[846:367764] UITextView--->_inputView------@"UIView"

我们现在已经拿到了所有UITextView的隐藏属性了,其中_placeholderLabel正是我们所需要的,直接使用KVC就可以修改我们想要修改的属性了。

UITextView *text = [[UITextView alloc]initWithFrame:CGRectMake(50, 180, 280, 120)];
    [self.view addSubview:text];

    UILabel *label = [[UILabel alloc]init];
    label.text =@"UITextViewUITextView";
    label.textColor = [UIColor blackColor];
    [text addSubview:label];

    [text setValue:label forKeyPath:@"placeholderLabel"];

现在系统所有隐藏的属性的外套都被剥下来了,这样就可以减少很多控件的重写工作了。
有问题请私信,有错误请指正,谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值