iOS-获取控件的隐藏属性并修改
2017-06-01 17:54
501 查看
我们在开发的过程中用到的一些控件,明明一些属性我们一定会用到,但是是查看API就是没有,这个时候一般人会吐槽一下苹果爸爸。随后就是选择自己封装或者使用其他的实现方式来实现 。
其实很多的属性并不是没有,而是因为一些原因苹果将其隐藏了,这种隐藏的属性我们一般会选择两种方式来修改它:
一、就是使用RunTime
比较麻烦,不推荐使用。
二、就是使用KVC
使用KVC其实也是基于RunTime来实现的,但是在代码的可读性上还是比较好的。首先我们需要使用RunTime轮询一个控件的所有私有类,然后找到我们需要的属性。
输出结果如下:
我们现在已经拿到了所有UITextView的隐藏属性了,其中_placeholderLabel正是我们所需要的,直接使用KVC就可以修改我们想要修改的属性了。
现在系统所有隐藏的属性的外套都被剥下来了,这样就可以减少很多控件的重写工作了。
有问题请私信,有错误请指正,谢谢。
其实很多的属性并不是没有,而是因为一些原因苹果将其隐藏了,这种隐藏的属性我们一般会选择两种方式来修改它:
一、就是使用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"];
现在系统所有隐藏的属性的外套都被剥下来了,这样就可以减少很多控件的重写工作了。
有问题请私信,有错误请指正,谢谢。
相关文章推荐
- 获取iOS控件的隐藏属性
- IOS 关于如何修改facebook内部封装好的控件属性(如何修改封装好的控件)
- iOS 开发中获取控件的坐标并修改
- IOS开发-代码创建UI控件并修改控件属性(代码创建UIButton、UILabel)
- robotium android 自动化UI测试获取控件是否隐藏的属性
- ios 通过runtime 获取属性列表并修改变量值
- 【iOS开发-12】UISwitch开关控件属性介绍以及获取开关状态并做出响应
- ios开发之获取修改文件属性
- Form提交时,后台获取jquery修改过的隐藏控件的值
- iphone ios 文件系统操作(创建、删除,修改,获取属性)
- iOS开发UISwitch开关控件属性介绍以及获取开关状态并做出响应
- iOS之 利用通知(NSNotificationCenter)获取键盘的高度,以及显示和隐藏键盘时修改界面的注意事项
- 写Form设计器尝试(四) 修改窗体上的控件属性
- 在控件的点击事件中获取空间自身的属性,如ID
- [导入][ASP.NET 控件实作 Day18] 修改集合属性编辑器
- 对话框控件属性的修改
- 对话框控件属性的修改
- 根据控件名字和属性动态修改值
- 获取或设置datalist内的控件的属性和值
- vb.net 中MDI子窗体对其父窗体属性的获取与修改