ios中通过调试来使用私有api

本文探讨如何在iOS中通过查看和利用私有API来改变UITextField光标的颜色。虽然iOS禁止使用私有API,但通过dump头文件获取API并分析,可以找到设置光标颜色的方法。文中以- (id)textInputTraits;为例,展示了分析和使用私有API的过程。
摘要由CSDN通过智能技术生成

       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
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值