How can I popup keyboard without UITextField - ios

I want a numeric keyboard without any textfield, I want to press a button and the keyboard will pop, is there an easy way to do it?
Or I need to build my own keyboard?
Thanks. :)

//
// EditingView.h
// TextEditing
//
// Created by Jeffrey Sambells on 10-04-21.
// Copyright 2010 TropicalPixels. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface UIKeyInputExampleView : UIView <UIKeyInput> {
NSMutableString *textStore;
}
#property (nonatomic, retain) NSMutableString *textStore;
#end
//
// EditingView.m
// TextEditing
//
// Created by Jeffrey Sambells on 10-04-21.
// Copyright 2010 TropicalPixels. All rights reserved.
//
#import "UIKeyInputExampleView.h"
#implementation UIKeyInputExampleView
#synthesize textStore;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
self.textStore = [NSMutableString string];
[self.textStore appendString:#"Touch screen to edit."];
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (void)dealloc {
[textStore dealloc];
[super dealloc];
}
#pragma mark -
#pragma mark Respond to touch and become first responder.
- (BOOL)canBecomeFirstResponder { return YES; }
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
[self becomeFirstResponder];
}
#pragma mark -
#pragma mark Drawing
- (void)drawRect:(CGRect)rect {
CGRect rectForText = CGRectInset(rect, 20.0, 20.0);
UIRectFrame(rect);
[self.textStore drawInRect:rectForText withFont:[UIFont fontWithName:#"Helvetica" size:24.0f]];
}
#pragma mark -
#pragma mark UIKeyInput Protocol Methods
- (BOOL)hasText {
if (textStore.length > 0) {
return YES;
}
return NO;
}
- (void)insertText:(NSString *)theText {
[self.textStore appendString:theText];
[self setNeedsDisplay];
}
- (void)deleteBackward {
NSRange theRange = NSMakeRange(self.textStore.length-1, 1);
[self.textStore deleteCharactersInRange:theRange];
[self setNeedsDisplay];
}
#end
This code may help you...

You need to implement UIKeyInput protocol. Reference

Related

UI CheckBox Button Not Changing Image

I am trying to set up a class for a UI Button I created for a custom checkbox. For some reason my image is not being set by Xcode. Yet I have other classes in my application that are setting images without a problem.
Checkbox.h
#import <UIKit/UIKit.h>
#interface checkBoxButton : UIButton
#property (nonatomic,assign) IBInspectable BOOL checked;
#end
Checkbox.m
#import "checkBoxButton.h"
#implementation checkBoxButton
-(id) init {
self = [super init];
if(self) {
[self addTarget:self action:#selector(changeState) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)checkedBox:(BOOL) checked{
self.checked = checked;
if(!self.checked){
[self setImage: [UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
} else {
[self setImage: [UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
}
- (void) changeState {
self.checked = !self.checked;
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
#end
-(id) init {
self = [super init];
if(self) {
self.checked = !self.checked;
[self addTarget:self action:#selector(checkedBox:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)checkedBox:(BOOL) checked{
if(!self.checked){
self.checked = checked;
[self setImage: [UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
} else {
self.checked = !checked;
[self setImage: [UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
}
Try it.
UIButtonBox
//
// UIButtonBox.h
//
//
// Created by Nischal Hada on 9/16/15.
// Copyright (c) 2015 Nischal Hada. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface UIButtonBox : UIButton
#property (nonatomic) BOOL isSelected;
#property (nonatomic,strong) NSString *selectImage;
#property (nonatomic,strong) NSString *deSelectImage;
- (void)select;
- (void)deselect;
- (void) setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
#end
//
// UIButtonBox.m
//
//
// Created by Nischal Hada on 9/16/15.
// Copyright (c) 2015 Nischal Hada. All rights reserved.
//
#import "UIButtonBox.h"
#implementation UIButtonBox
- (void) layoutSubviews {
[super layoutSubviews];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (void) setBackgroundImage:(UIImage *)image forState:(UIControlState)state {
[super setBackgroundImage:image forState:state];
}
- (void) setBackgroundImage:(NSString *)imageName {
self.isSelected = ([imageName isEqualToString:self.deSelectImage])?YES:NO;
[self setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[self setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateSelected];
}
- (NSString *)selectImageName {
return self.selectImage;
}
- (NSString *)deSelectImageName {
return self.deSelectImage;
}
- (void)select {
[self setBackgroundImage:self.deSelectImage];
}
- (void)deselect {
[self setBackgroundImage:self.selectImage];
}
#end
CountrySelectCell
//
// CountrySelectCell.h
//
//
// Created by Nischal Hada on 12/12/15.
//
//
#import <UIKit/UIKit.h>
#import "UIButtonBox.h"
#interface CountrySelectCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *lblName;
#property (nonatomic) BOOL isSelected;
#property (weak, nonatomic) IBOutlet UIButtonBox *btnCHeckBox;
- (IBAction)actionCheckBox:(id)sender;
#end
//
// CountrySelectCell.m
//
//
// Created by Nischal Hada on 12/12/15.
//
//
#import "CountrySelectCell.h"
#import "Constants.h"
#implementation CountrySelectCell
- (void)awakeFromNib {
self.lblName.font = [UIFont fontWithName:REGULAR_PROXIMANOVA_FONT size:SIZE_REGULAR__FONT];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#pragma mark -
#pragma mark Object Methods
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.isSelected = NO;
}
return self;
}
#pragma mark -
#pragma mark IBAction Methods
- (IBAction)actionCheckBox:(id)sender {
self.isSelected = !self.isSelected;
(self.isSelected)?[self.btnCHeckBox select]:[self.btnCHeckBox deselect];
}
#end
CountrySelectVC.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CountrySelectCell";
CountrySelectCell *cell = (CountrySelectCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CountrySelectCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lblName.text = [[tableData objectAtIndex:indexPath.row]uppercaseString];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.btnCHeckBox.deSelectImage = #"checkbox.png";
cell.btnCHeckBox.selectImage = #"uncheckbox.png";
[cell.btnCHeckBox setBackgroundImage:[UIImage imageNamed:#"uncheckbox.png"] forState:UIControlStateNormal];
return cell;
}

failure of alloc and Init

So perhaps this is a beginner's mistake and super easy to you guys, but i really do not know how to solve it,really appreciate for any suggestions:
Right Now:
1: I have to ViewController: EnterCommandViewController and DetectionViewController
2: I wrote Delegate protocol in EnterCommandViewController and set DetectionViewController as its delegate.
3: About delegate: I have a inputTextField in the EnterCommandView and a "Save" bar button item on the top toolbar in this view. Once I click the save , current view will be dismissed and return back to DetectionView and show the NSString just entered in the UILabel in DetectionView.
Finally, My question is that Why After I alloc and init a EnterCommandViewController instance , that is enterCVS, the instance is still nil as show in end of my post.
Code:
EnterCommandViewController.h
#import <UIKit/UIKit.h>
#import "RscMgr.h"
#protocol EnterCommandDelegate <NSObject>
#optional
-(void) commandEntered:(NSString*)command;
#end
#interface EnterCommandViewController : UIViewController <RscMgrDelegate,EnterCommandDelegate>
{
RscMgr* rscMgr;
IBOutlet UITextField *inputTextField;
// DetectionViewController* detectionViewController;
// __unsafe_unretained id<EnterCommandDelegate> delegate;
}
-(void)sendMessage:(NSString*)message;
-(id)initWithDelegate:(id)delegateToBe;
- (IBAction)cancelPressed;
- (IBAction)savePressed;
#property (nonatomic,weak) id<EnterCommandDelegate> delegate; //assign replaced
#end
EnterCommandVIewController.m
#import "EnterCommandViewController.h"
#import "DetectionViewController.h"
#interface EnterCommandViewController () <UITextFieldDelegate>
{
#private
BOOL connected;
}
#end
#implementation EnterCommandViewController
#synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
rscMgr = [[RscMgr alloc] init];
[rscMgr setDelegate:self];
// Do any additional setup after loading the view, typically from a nib.
[inputTextField becomeFirstResponder];
}
-(id)initWithDelegate:(id)delegateToBe{
if(self = [super init]){
delegate = delegateToBe;
}
return self;
}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
inputTextField.delegate = self;
}
-(void) viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
inputTextField.delegate = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITextFieldDelegate Methods
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[self sendMessage:textField.text];
textField.text = nil;
return NO;
}
#pragma mark - Serial Tx/Rx Methods Implementation
-(void) sendMessage:(NSString *)message{
if(connected == YES) {
[rscMgr writeString:message];
}
else{
NSLog(#"CableDisconnected!");
NSLog(#"Attempted To Send: %#",message);
}
}
- (IBAction)cancelPressed {
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (IBAction)savePressed {
//is anyone listening
if([[[UIDevice currentDevice]systemVersion] compare:#"7.0" options:NSNumericSearch] != NSOrderedAscending){
NSLog(#"SYStem version > 7.0");
}
if(delegate&&[delegate respondsToSelector:#selector(commandEntered:)]){
NSLog(#"SomeMethod is listening");
[delegate commandEntered:inputTextField.text];
}
[self dismissViewControllerAnimated:YES completion:nil]; //commened: ^{}
}
#pragma mark - RscMgrDelegate Methods Implementation
-(void) cableConnected:(NSString *)protocol{
inputTextField.text = #"cableConnected";
[rscMgr setBaud:9600];
[rscMgr open];
connected = YES;
}
-(void) cableDisconnected{
inputTextField.text = #"cableDisconnected";
connected = NO;
}
-(void) readBytesAvailable:(UInt32)length{}
-(void) portStatusChanged{}
#end
DetectionViewController.h
#import <UIKit/UIKit.h>
#import "EnterCommandViewController.h"
#interface DetectionViewController : UIViewController <EnterCommandDelegate>{
}
- (IBAction)showSettings:(UIBarButtonItem *)sender;
#property (nonatomic, strong) EnterCommandViewController* enterCVC;
#property (nonatomic, strong) IBOutlet UILabel *showReceivedCommand;
#end
DetectionViewController.m
#import <Foundation/Foundation.h>
#import "DetectionViewController.h"
#import "EnterCommandViewController.h"
#implementation DetectionViewController
#synthesize showReceivedCommand;
#synthesize enterCVC;
- (IBAction)showSettings:(UIBarButtonItem *)sender {
}
-(void) viewDidLoad{
[super viewDidLoad];
if(showReceivedCommand){
showReceivedCommand.text=#"Initial text";
NSLog(#"UILAbel in ViewDidload is not nil");
}else {
NSLog(#"UILAbel in viewDidload is nil");
}
enterCVC = [[EnterCommandViewController alloc] init];
if(enterCVC.delegate) NSLog(#"X nil");
[enterCVC setDelegate:self];
}
#pragma mark - EnterCommandDelegate function(s)
-(void)commandEntered:(NSString *)command{
dispatch_async(dispatch_get_main_queue(), ^{
if(showReceivedCommand){
NSLog(#"UILabel is not nil");
}else{NSLog(#"UILabel is nil");}
showReceivedCommand = [[UILabel alloc] init];
NSLog(#"command received: %#",command);
showReceivedCommand.text = command;
[showReceivedCommand setNeedsDisplay];
NSLog(#"text in showReceivedCommand is %#",showReceivedCommand.text);
});
}
#end
I set a break point at DetectionViewController.n --> ViewDidLoad() --> [enterCVC setDelegate:self];
I got:
self DetectionViewController * 0x15c50e850 0x000000015c50e850
UIViewController UIViewController
showReceivedCommand UILabel * 0x15c510650 0x000000015c510650
enterCVC EnterCommandViewController * 0x15c611360 0x000000015c611360
showReceivedCommand UILabel * 0x15c510650 0x000000015c510650
enterCVC EnterCommandViewController * 0x15c611360 0x000000015c611360
UIViewController UIViewController
rscMgr RscMgr * nil 0x0000000000000000
inputTextField UITextField * nil 0x0000000000000000
connected BOOL NO false
delegate id 0x0 0x0000000000000000
enterCVC = [[EnterCommandViewController alloc] init]
Try changing that to....
enterCVC = [[EnterCommandViewController alloc] initWithDelegate:self];

Blue highlighting not appear with custom UITextView

I created a subclass of UITextView to add a custom UIMenuItem. The problem is that when I press my custom action to display custom item, the text is not highlighted. Any idea?
ActionsTextView.h
#import <UIKit/UIKit.h>
#protocol ActionsDelegate <NSObject>
- (void)addDreamSignalWithText:(NSString *)text range:(NSRange)range;
#end
#interface ActionsTextView : UITextView
#pragma mark - Delegate
#property IBOutlet id<ActionsDelegate>actionsDelegate;
#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender;
#end
ActionsTextView.m
#import "ActionsTextView.h"
#implementation ActionsTextView
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == #selector(addDreamSignalAction:)) {
return YES;
}
return NO;
}
#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender {
if ([_actionsDelegate respondsToSelector:#selector(addDreamSignalWithText:range:)]) {
[_actionsDelegate addDreamSignalWithText:[self.text substringWithRange:self.selectedRange]
range:self.selectedRange];
}
// Deselect text
self.selectedTextRange = nil;
}
#end
Thanks!!
Thanks to rmaddy, I found the solution.
Here is the code:
ActionsTextView.h
#import <UIKit/UIKit.h>
#protocol ActionsDelegate <NSObject>
- (void)addDreamSignalWithText:(NSString *)text range:(NSRange)range;
#end
#interface ActionsTextView : UITextView
#pragma mark - Delegate
#property IBOutlet id<ActionsDelegate>actionsDelegate;
#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender;
#pragma mark - Notifications
- (void)menuControllerWillShow:(NSNotification *)notification;
#end
ActionsTextView.m
#import "ActionsTextView.h"
#implementation ActionsTextView
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == #selector(addDreamSignalAction:)) {
return YES;
}
return NO;
}
#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender {
if ([_actionsDelegate respondsToSelector:#selector(addDreamSignalWithText:range:)]) {
[_actionsDelegate addDreamSignalWithText:[self.text substringWithRange:self.selectedRange]
range:self.selectedRange];
}
// Deselect text
self.selectedTextRange = nil;
}
#pragma mark - Notifications
- (void)menuControllerWillShow:(NSNotification *)notification {
if (self.selectedRange.length == 0) {
[self select:self];
}
}
#end

Why my UIScrollView can not scoll updated, add source code. help please

My source code download
------Updated--------
I am trying to implement a sidebar effect using CGAffineTransformMakeTranslate simulate slide-in and slide-out. I want to make my sidebar as a scrollview so it could be add more data but it can not scroll at all.
Here is my code:
SidebarView is a UITableView
#import "SidebarView.h"
#interface SidebarView ()
#property (nonatomic, readwrite) CGFloat offsetX;
#end
#implementation SidebarView
#pragma mark - Initilization
- (void)setup {
// do initilization here
self.offsetX = self.frame.size.width;
[self registerClass:[UITableViewCell class] forCellReuseIdentifier:#"sidebarCell"];
}
- (void)awakeFromNib {
[self setup];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
[self setup];
return self;
}
#pragma mark - Custom function
- (void)show {
self.transform = CGAffineTransformMakeTranslation(-self.offsetX, 0);
}
- (void)hide {
self.transform = CGAffineTransformMakeTranslation(-self.offsetX, 0);
}
And my view controller:
#import "ViewController.h"
#import "SidebarView.h"
#interface ViewController () <UITableViewDataSource>
#property (nonatomic) BOOL isMenuHide;
#property (nonatomic, strong) SidebarView *sidebarView;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.isMenuHide = YES;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if (self.sidebarView) {
self.sidebarView = nil;
}
if (self.view) {
self.view = nil;
}
}
- (SidebarView *)sidebarView {
if (!_sidebarView) {
CGRect frame = [[UIScreen mainScreen] bounds];
frame.size.width /= 2;
_sidebarView = [[SidebarView alloc] initWithFrame:frame];
_sidebarView.transform = CGAffineTransformMakeTranslation(-_sidebarView.offsetX, 0);
_sidebarView.contentSize = CGSizeMake(320, 960);
_sidebarView.scrollEnabled = YES;
_sidebarView.showsVerticalScrollIndicator = YES;
_sidebarView.dataSource = self;
}
return _sidebarView;
}
#define ANIMATE_DURATION 0.5
- (IBAction)showMenu:(UIBarButtonItem *)sender {
if (self.isMenuHide) {
[self.view addSubview:self.sidebarView];
[UIView animateWithDuration:ANIMATE_DURATION animations:^{
[self.sidebarView show];
self.view.transform = CGAffineTransformMakeTranslation(self.sidebarView.offsetX, 0);
}];
} else {
[UIView animateWithDuration:ANIMATE_DURATION animations:^{
[self.sidebarView hide];
self.view.transform = CGAffineTransformMakeTranslation(0, 0);
} completion:^(BOOL finished) {
[self.sidebarView removeFromSuperview];
}];
}
self.isMenuHide = !self.isMenuHide;
}
#pragma mark - UITableView Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 11;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"sidebarCell"];
cell.textLabel.text = #"Bingo";
return cell;
}
#end
Please tell my "why my scrollview cannot scroll" thanks.
contentSize needs to be set to the size of the content being contained, not the size of the frame in which it sits.
I don't know if your code has other problems, but that's the most terminal one.

UIKeyInput is not called when using Japanese keyboard

I have a class which inherits from UIView and conforms to UIKeyInput
*.h*
#interface UIKeyInputExampleView : UIView <UIKeyInput>{
NSMutableString *textStore;
}
#property (nonatomic, retain) NSMutableString *textStore;
#end
.m
#implementation UIKeyInputExampleView
#synthesize textStore;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
self.textStore = [NSMutableString string];
[self.textStore appendString:#"Touch screen to edit."];
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (void)dealloc {
[textStore dealloc];
[super dealloc];
}
#pragma mark -
#pragma mark Respond to touch and become first responder.
- (BOOL)canBecomeFirstResponder { return YES; }
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
[self becomeFirstResponder];
}
#pragma mark -
#pragma mark Drawing
- (void)drawRect:(CGRect)rect {
CGRect rectForText = CGRectInset(rect, 20.0, 20.0);
UIRectFrame(rect);
[self.textStore drawInRect:rectForText withFont:[UIFont fontWithName:#"Helvetica" size:24.0f]];
}
#pragma mark -
#pragma mark UIKeyInput Protocol Methods
- (BOOL)hasText {
if (textStore.length > 0) {
return YES;
}
return NO;
}
- (void)insertText:(NSString *)theText {
NSLog(#"Text have just enter:%# length=%d ascii=%d",theText,theText.length,[theText characterAtIndex:0]);
if ([theText isEqualToString:#"\n"]) {
NSLog(#"Enter have just pressed!");
[self resignFirstResponder];
}
self.textStore = (NSMutableString*)theText;
[self setNeedsDisplay];
}
- (void)deleteBackward {
self.textStore = (NSMutableString*)#"delete";
[self setNeedsDisplay];
}
#end
When I use the English or Vietnamese keyboard, everything is right. but when I use Japanese keyboard, no event is called, no exception is thrown.
I think I have not conformed some protocol
Can you help me?
It took some time, but finally it works.
1. For single character - I still use UIKeyInput protocol.
2. For east-asian languages I use NSString property intlInput to get all input characters. Below are two methods of UITextInput protocol that allow to do this.
- (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange {
self.intlInput = markedText;
}
- (void) unmarkText {
if (!self.intlInput) return;
for (int i=0;i<self.intlInput.length;i++) {
[self sendChar:[self.intlInput characterAtIndex:i]];
}
self.intlInput = nil;
}
sendChar - is any method that called to take all characters of combined input. You can post notification when text is unmarked (some combination is chosen).
i have solved the problem , this example show how convert chinese input to insertText in ios.
#ifndef UIKeyInputExampleView_h
#define UIKeyInputExampleView_h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface UIKeyInputExampleView : UIView <UIKeyInput,UITextInput>{
NSMutableString *textStore;
}
#property (nonatomic, retain) NSString *textStore;
#end
#endif /* UIKeyInputExampleView_h */
//
// UIKeyInputExampleView.m
// CustomInput
//
// Created by Gust on 2018/11/27.
// Copyright © 2018 Gust. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "UIKeyInputExampleView.h"
#implementation UIKeyInputExampleView
#synthesize textStore;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
self.textStore = [NSMutableString string];
//[self.textStore appendString:#"Touch screen to edit."];
//[self.textStore
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (void)dealloc {
//[textStore dealloc];
//[super dealloc];
}
#pragma mark -
#pragma mark Respond to touch and become first responder.
- (BOOL)canBecomeFirstResponder { return YES; }
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
[self becomeFirstResponder];
}
#pragma mark -
#pragma mark Drawing
- (void)drawRect:(CGRect)rect {
CGRect rectForText = CGRectInset(rect, 20.0, 20.0);
UIRectFrame(rect);
[self.textStore drawInRect:rectForText withFont:[UIFont fontWithName:#"Helvetica" size:24.0f]];
}
#pragma mark -
#pragma mark UIKeyInput Protocol Methods
- (BOOL)hasText {
if (textStore.length > 0) {
return YES;
}
return NO;
}
- (void)insertText:(NSString *)theText {
NSLog(#"Text have just enter:%# length=%d ascii=%d",theText,theText.length,[theText characterAtIndex:0]);
if ([theText isEqualToString:#"\n"]) {
NSLog(#"Enter have just pressed!");
[self resignFirstResponder];
}
self.textStore = (NSMutableString*)theText;
[self setNeedsDisplay];
}
- (void)deleteBackward {
self.textStore = (NSMutableString*)#"delete";
[self setNeedsDisplay];
}
#synthesize beginningOfDocument;
#synthesize endOfDocument;
#synthesize inputDelegate;
#synthesize markedTextRange;
#synthesize markedTextStyle;
#synthesize selectedTextRange;
#synthesize tokenizer;
- (UITextWritingDirection)baseWritingDirectionForPosition:(nonnull UITextPosition *)position inDirection:(UITextStorageDirection)direction {
return UITextWritingDirectionLeftToRight;
}
- (CGRect)caretRectForPosition:(nonnull UITextPosition *)position {
return CGRectMake(0, 0, 10, 30);
}
- (nullable UITextRange *)characterRangeAtPoint:(CGPoint)point {
return nil;
}
- (nullable UITextRange *)characterRangeByExtendingPosition:(nonnull UITextPosition *)position inDirection:(UITextLayoutDirection)direction {
return nil;
}
- (nullable UITextPosition *)closestPositionToPoint:(CGPoint)point {
return nil;
}
- (nullable UITextPosition *)closestPositionToPoint:(CGPoint)point withinRange:(nonnull UITextRange *)range {
return nil;
}
- (NSComparisonResult)comparePosition:(nonnull UITextPosition *)position toPosition:(nonnull UITextPosition *)other {
return NSOrderedSame;
}
- (CGRect)firstRectForRange:(nonnull UITextRange *)range {
return [self bounds];
}
- (NSInteger)offsetFromPosition:(nonnull UITextPosition *)from toPosition:(nonnull UITextPosition *)toPosition {
return NSIntegerMax;
}
- (nullable UITextPosition *)positionFromPosition:(nonnull UITextPosition *)position inDirection:(UITextLayoutDirection)direction offset:(NSInteger)offset {
return nil;
}
- (nullable UITextPosition *)positionFromPosition:(nonnull UITextPosition *)position offset:(NSInteger)offset {
return nil;
}
- (nullable UITextPosition *)positionWithinRange:(nonnull UITextRange *)range farthestInDirection:(UITextLayoutDirection)direction {
return nil;
}
- (void)replaceRange:(nonnull UITextRange *)range withText:(nonnull NSString *)text {
//NSLog(#"replaceRange %#",text);
}
- (nonnull NSArray<UITextSelectionRect *> *)selectionRectsForRange:(nonnull UITextRange *)range {
NSArray *arr = NULL;
arr = #[];
return arr;
}
- (void)setBaseWritingDirection:(UITextWritingDirection)writingDirection forRange:(nonnull UITextRange *)range {
//self.textStore = markedText;
}
- (void)setMarkedText:(nullable NSString *)markedText selectedRange:(NSRange)selectedRange {
//NSLog(#"setMarkedText %#",markedText);
self.textStore=markedText;
}
- (nullable NSString *)textInRange:(nonnull UITextRange *)range {
//NSLog(#"textInRange ");
return nil;
}
- (nullable UITextRange *)textRangeFromPosition:(nonnull UITextPosition *)fromPosition toPosition:(nonnull UITextPosition *)toPosition {
return nil;
}
- (void)unmarkText {
if (!self.textStore) return;
[self insertText:textStore];
self.textStore = nil;
//NSLog(#"unmarkText ");
}
- (void)encodeWithCoder:(nonnull NSCoder *)aCoder {
}
+ (nonnull instancetype)appearance {
return nil;
}
+ (nonnull instancetype)appearanceForTraitCollection:(nonnull UITraitCollection *)trait {
return nil;
}
+ (nonnull instancetype)appearanceForTraitCollection:(nonnull UITraitCollection *)trait whenContainedIn:(nullable Class<UIAppearanceContainer>)ContainerClass, ... {
return nil;
}
+ (nonnull instancetype)appearanceForTraitCollection:(nonnull UITraitCollection *)trait whenContainedInInstancesOfClasses:(nonnull NSArray<Class<UIAppearanceContainer>> *)containerTypes {
return nil;
}
+ (nonnull instancetype)appearanceWhenContainedIn:(nullable Class<UIAppearanceContainer>)ContainerClass, ... {
return nil;
}
+ (nonnull instancetype)appearanceWhenContainedInInstancesOfClasses:(nonnull NSArray<Class<UIAppearanceContainer>> *)containerTypes {
return nil;
}
- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection {
}
- (CGPoint)convertPoint:(CGPoint)point fromCoordinateSpace:(nonnull id<UICoordinateSpace>)coordinateSpace {
return CGPointMake(0, 0);
}
- (CGPoint)convertPoint:(CGPoint)point toCoordinateSpace:(nonnull id<UICoordinateSpace>)coordinateSpace {
return CGPointMake(0, 0);
}
- (CGRect)convertRect:(CGRect)rect fromCoordinateSpace:(nonnull id<UICoordinateSpace>)coordinateSpace {
return CGRectMake(0, 0,1,1);
}
- (CGRect)convertRect:(CGRect)rect toCoordinateSpace:(nonnull id<UICoordinateSpace>)coordinateSpace {
return CGRectMake(0, 0,1,1);
}
- (void)didUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context withAnimationCoordinator:(nonnull UIFocusAnimationCoordinator *)coordinator {
}
- (void)setNeedsFocusUpdate {
}
- (BOOL)shouldUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context {
return YES;
}
- (void)updateFocusIfNeeded {
}
- (nonnull NSArray<id<UIFocusItem>> *)focusItemsInRect:(CGRect)rect {
NSArray *arr = NULL;
arr = #[];
return arr;
}
#end

Resources