iOS不允许使用ios私有api,使用私有api可以获得意想不到的效果 ,同时使用私有api是一个程序员的技术体现之一。至于怎么通过apple的审核,其实是有些办法的,但是不属于今天讨论的范围。私有api可以通过dump头文件获得,也可以去github clone一份现成的,但是还是推荐自己去dump。但是我们今天不是讨论,如何使用dump到的api,而是通过一个例子来讨论如何通过查看头文件使用私有api。
我们以UITextfield为例,其实这是个比较简单的控件,有一个蓝色输入的光标,可以在你输入的时候隐藏或者显示,但是并没有给我们提供改变光标颜色的接口,这个时候我们如果要改变光标的颜色,必然要使用私有api(或者通过比较啰嗦的办法来做这这件事情)。
我们首先需要dump一包头文件,如何class-dump,不是我们今天讨论的要点,现在我们贴上UITextfield的头文件。如果你觉得很长,直接拖到最后面。
/*
* Generated by class-dump 3.4 (64 bit).
*
* class-dump is Copyright (C) 1997-1998, 2000-2001, 2004-2012 by Steve Nygard.
*/
#import <UIKit/UIControl.h>
#import "NSCoding.h"
#import "UIPopoverControllerDelegate.h"
#import "UITextInput.h"
@class NSArray, NSAttributedString, NSDictionary, NSString, UIButton, UIColor, UIFont, UIImage, UIImageView, UILabel, UITextFieldAtomBackgroundView, UITextFieldBackgroundView, UITextFieldBorderView, UITextFieldLabel, UITextInputTraits, UITextInteractionAssistant, UITextPosition, UITextRange, UIView, _UIBaselineLayoutStrut, _UITextServiceSession;
@interface UITextField : UIControl <UIPopoverControllerDelegate, UITextInput, NSCoding>
{
NSAttributedString *_text;
UIColor *_textColor;
int _borderStyle;
float _minimumFontSize;
id _delegate;
UIImage *_background;
UIImage *_disabledBackground;
int _clearButtonMode;
UIView *_leftView;
int _leftViewMode;
UIView *_rightView;
int _rightViewMode;
UITextInputTraits *_traits;
UITextInputTraits *_nonAtomTraits;
float _fullFontSize;
float _paddingLeft;
float _paddingTop;
float _paddingRight;
float _paddingBottom;
NSString *_textFont;
struct _NSRange _selectionRange;
int _scrollXOffset;
int _scrollYOffset;
float _progress;
NSString *_style;
UIButton *_clearButton;
struct CGSize _clearButtonOffset;
struct CGSize _leftViewOffset;
struct CGSize _rightViewOffset;
UITextFieldBorderView *_backgroundView;
UITextFieldBorderView *_disabledBackgroundView;
UITextFieldBackgroundView *_systemBackgroundView;
UITextFieldLabel *_textLabel;
UITextFieldLabel *_placeholderLabel;
UITextFieldLabel *_suffixLabel;
UITextFieldLabel *_prefixLabel;
UIImageView *_iconView;
UILabel *_label;
float _labelOffset;
UITextInteractionAssistant *_interactionAssistant;
UIView *_inputView;
UIView *_inputAccessoryView;
UITextFieldAtomBackgroundView *_atomBackgroundView;
UIColor *_shadowColor;
struct CGSize _shadowOffset;
float _shadowBlur;
struct {
unsigned int secureTextChanged:1;
unsigned int guard:1;
unsigned int delegateRespondsToHandleKeyDown:1;
unsigned int verticallyCenterText:1;
unsigned int isAnimating:4;
unsigned int inactiveHasDimAppearance:1;
unsigned int becomesFirstResponderOnClearButtonTap:1;
unsigned int clearsOnBeginEditing:1;
unsigned int clearsPlaceholderOnBeginEditing:1;
unsigned int adjustsFontSizeToFitWidth:1;
unsigned int fieldEditorAttached:1;
unsigned int canBecomeFirstResponder:1;
unsigned int shouldSuppressShouldBeginEditing:1;
unsigned int inResignFirstResponder:1;
unsigned int undoDisabled:1;
unsigned int contentsRTL:1;
unsigned int explicitAlignment:1;
unsigned int implementsCustomDrawing:1;
unsigned int needsClearing:1;
unsigned int suppressContentChangedNotification:1;
unsigned int allowsEditingTextAttributes:1;
unsigned int usesAttributedText:1;
unsigned int backgroundViewState:2;
unsigned int clearsOnInsertion:1;
} _textFieldFlags;
_UITextServiceSession *_definitionSession;
_UITextServiceSession *_learnSession;
BOOL _deferringBecomeFirstResponder;
BOOL _avoidBecomeFirstResponder;
BOOL _setSelectionRangeAfterFieldEditorIsAttached;
NSArray *_baselineLayoutConstraints;
_UIBaselineLayoutStrut *_baselineLayoutLabel;
}
@property(retain) UIView *inputView; // @synthesize inputView=_inputView;
@property(nonatomic) int rightViewMode; // @synthesize rightViewMode=_rightViewMode;
@property(retain, nonatomic) UIView *rightView; // @synthesize rightView=_rightView;
@property(nonatomic) int leftViewMode; // @synthesize leftViewMode=_leftViewMode;
@property(retain, nonatomic) UIView *leftView; // @synthesize leftView=_leftView;
@property(nonatomic) int clearButtonMode; // @synthesize clearButtonMode=_clearButtonMode;
@property(retain, nonatomic) UIImage *disabledBackground; // @synthesize disabledBackground=_disabledBackground;
@property(retain, n