1// This file is part of VSTGUI. It is subject to the license terms 2// in the LICENSE file found in the top-level directory of this 3// distribution and at http://github.com/steinbergmedia/vstgui/LICENSE 4 5#import "uitextedit.h" 6#import <UIKit/UIKit.h> 7 8#if TARGET_OS_IPHONE 9 10#import "../cfontmac.h" 11#import "../macglobals.h" 12#import "../macstring.h" 13 14#if __has_feature(objc_arc) && __clang_major__ >= 3 15#define ARC_ENABLED 1 16#endif // __has_feature(objc_arc) 17 18//------------------------------------------------------------------------------------ 19@interface VSTGUI_UITextFieldDelegate : NSObject<UITextFieldDelegate> 20//------------------------------------------------------------------------------------ 21{ 22 VSTGUI::IPlatformTextEditCallback* textEdit; 23} 24@end 25 26//------------------------------------------------------------------------------------ 27@implementation VSTGUI_UITextFieldDelegate 28//------------------------------------------------------------------------------------ 29 30//------------------------------------------------------------------------------------ 31- (id)initWithUITextEdit:(VSTGUI::IPlatformTextEditCallback*)_textEdit 32{ 33 self = [super init]; 34 if (self) 35 textEdit = _textEdit; 36 return self; 37} 38 39//------------------------------------------------------------------------------------ 40- (void)looseFocus 41{ 42 if (textEdit) 43 { 44 textEdit->platformLooseFocus (false); 45 } 46} 47 48//------------------------------------------------------------------------------------ 49- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 50{ 51 if (textEdit) 52 { 53 [self performSelector:@selector(looseFocus) withObject:nil afterDelay:0]; 54 } 55 return YES; 56} 57 58//------------------------------------------------------------------------------------ 59- (BOOL)textFieldShouldReturn:(UITextField *)textField 60{ 61 VSTGUI::IPlatformTextEditCallback* tmp = textEdit; 62 textEdit = 0; 63 tmp->platformLooseFocus (true); 64 return YES; 65} 66 67@end 68 69//------------------------------------------------------------------------------------ 70namespace VSTGUI { 71 72//------------------------------------------------------------------------------------ 73UITextEdit::UITextEdit (UIView* parent, IPlatformTextEditCallback* textEdit) 74: IPlatformTextEdit (textEdit) 75, platformControl (0) 76, parent (parent) 77{ 78 delegate = [[VSTGUI_UITextFieldDelegate alloc] initWithUITextEdit:textEdit]; 79 80 CRect rect (textEdit->platformGetSize ()); 81 CPoint textInset = textEdit->platformGetTextInset (); 82 CGRect r = CGRectFromCRect (rect); 83 r.origin.x += textInset.x / 2.; 84 r.origin.y += textInset.y / 2.; 85 r.size.width -= textInset.x / 2; 86 r.size.height -= textInset.y / 2; 87 platformControl = [[UITextField alloc] initWithFrame:r]; 88 89 bool fontSet = false; 90 CoreTextFont* ctf = textEdit->platformGetFont ()->getPlatformFont ().cast<CoreTextFont> (); 91 if (ctf) 92 { 93 CTFontRef fontRef = ctf->getFontRef (); 94 if (fontRef) 95 { 96 CTFontDescriptorRef fontDesc = CTFontCopyFontDescriptor (fontRef); 97 [platformControl setFont:[UIFont fontWithDescriptor:(__bridge UIFontDescriptor *)fontDesc size:0]]; 98 CFRelease (fontDesc); 99 fontSet = true; 100 } 101 } 102 if (!fontSet) 103 { 104 NSString* fontName = [NSString stringWithCString:textEdit->platformGetFont ()->getName () encoding:NSUTF8StringEncoding]; 105 [platformControl setFont:[UIFont fontWithName:fontName size:static_cast<CGFloat> (textEdit->platformGetFont ()->getSize ())]]; 106 } 107 CColor fontColor = textEdit->platformGetFontColor (); 108 platformControl.textColor = [UIColor colorWithRed:fontColor.red / 255.f green:fontColor.green / 255.f blue:fontColor.red / 255.f alpha:fontColor.alpha / 255.f]; 109 platformControl.borderStyle = UITextBorderStyleNone; 110 platformControl.opaque = NO; 111 platformControl.clearsContextBeforeDrawing = YES; 112 NSTextAlignment textAlignment; 113 switch (textEdit->platformGetHoriTxtAlign ()) 114 { 115 case kLeftText: textAlignment = NSTextAlignmentLeft; break; 116 case kCenterText: textAlignment = NSTextAlignmentCenter; break; 117 case kRightText:textAlignment = NSTextAlignmentRight; break; 118 } 119 platformControl.textAlignment = textAlignment; 120 platformControl.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 121 platformControl.clearButtonMode = UITextFieldViewModeNever; 122 platformControl.returnKeyType = UIReturnKeyDefault; 123 platformControl.enablesReturnKeyAutomatically = YES; 124 platformControl.delegate = delegate; 125 126 setText (textEdit->platformGetText ()); 127 128 [parent addSubview:platformControl]; 129 130 [platformControl becomeFirstResponder]; 131} 132 133//------------------------------------------------------------------------------------ 134UITextEdit::~UITextEdit () 135{ 136 if (platformControl) 137 { 138 [platformControl removeFromSuperview]; 139 #if !ARC_ENABLED 140 [platformControl release]; 141 #endif 142 platformControl = nil; 143 } 144 delegate = nil; 145#if !ARC_ENABLED 146 [delegate release]; 147#endif 148} 149 150//------------------------------------------------------------------------------------ 151UTF8String UITextEdit::getText () 152{ 153 if (platformControl) 154 { 155 NSString* text = [platformControl text]; 156 return [text UTF8String]; 157 } 158 return 0; 159} 160 161//------------------------------------------------------------------------------------ 162bool UITextEdit::setText (const UTF8String& text) 163{ 164 if (platformControl == nullptr) 165 return false; 166 if (NSString* t = fromUTF8String<NSString*> (text)) 167 { 168 [platformControl setText:t]; 169 return true; 170 } 171 return false; 172} 173 174//------------------------------------------------------------------------------------ 175bool UITextEdit::updateSize () 176{ 177 return false; 178} 179 180} // VSTGUI 181 182#endif 183