我们在开发的过程中用到的一些控件,明明一些属性我们一定会用到,但是是查看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"];
现在系统所有隐藏的属性的外套都被剥下来了,这样就可以减少很多控件的重写工作了。
有问题请私信,有错误请指正,谢谢。
网友评论