Merge pull request #17127 from DavidDeSimone/ios-edbox-improvements-v3

Fix for issue #17114.
This commit is contained in:
子龙山人 2017-01-09 09:26:32 +08:00 committed by GitHub
commit eb730c10ec
6 changed files with 125 additions and 16 deletions

View File

@ -93,6 +93,8 @@
textInput.ccui_text = _textInput.ccui_text ?: @""; textInput.ccui_text = _textInput.ccui_text ?: @"";
textInput.ccui_placeholder = _textInput.ccui_placeholder ?: @""; textInput.ccui_placeholder = _textInput.ccui_placeholder ?: @"";
textInput.ccui_font = _textInput.ccui_font ?: [UIFont systemFontOfSize:self.frameRect.size.height*2/3]; textInput.ccui_font = _textInput.ccui_font ?: [UIFont systemFontOfSize:self.frameRect.size.height*2/3];
textInput.ccui_placeholderFont = _textInput.ccui_placeholderFont ?: textInput.ccui_font;
textInput.ccui_placeholderTextColor = _textInput.ccui_placeholderTextColor ?: [UIColor lightGrayColor];
[_textInput resignFirstResponder]; [_textInput resignFirstResponder];
[_textInput removeFromSuperview]; [_textInput removeFromSuperview];
@ -137,12 +139,12 @@
- (void)setPlaceholderFont:(UIFont *)font - (void)setPlaceholderFont:(UIFont *)font
{ {
self.textInput.ccui_placeholderLabel.font = font; self.textInput.ccui_placeholderFont = font;
} }
- (void)setPlaceholderTextColor:(UIColor *)color - (void)setPlaceholderTextColor:(UIColor *)color
{ {
self.textInput.ccui_placeholderLabel.textColor = color; self.textInput.ccui_placeholderTextColor = color;
} }
- (void)setInputMode:(cocos2d::ui::EditBox::InputMode)inputMode - (void)setInputMode:(cocos2d::ui::EditBox::InputMode)inputMode

View File

@ -27,7 +27,11 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import "ui/UIEditBox/iOS/UITextField+CCUITextInput.h" #import "ui/UIEditBox/iOS/UITextField+CCUITextInput.h"
/** TODO: Missing doc - Why is this subclass necessary? #pragma mark - UISingleLineTextField implementation
*/
@interface CCUISingleLineTextField : UITextField @interface CCUISingleLineTextField : UITextField
@property (nonatomic, retain) UIColor *placeholderTextColor;
@property (nonatomic, retain) UIFont *placeholderFont;
@end @end

View File

@ -29,9 +29,52 @@
#include "base/CCDirector.h" #include "base/CCDirector.h"
/**
* http://stackoverflow.com/questions/18244790/changing-uitextfield-placeholder-font
*/
@implementation CCUISingleLineTextField @implementation CCUISingleLineTextField
#pragma mark - Init & Dealloc
- (void)dealloc
{
[_placeholderFont release];
[_placeholderTextColor release];
[super dealloc];
}
#pragma mark - Properties
- (UIColor *)placeholderTextColor
{
return _placeholderTextColor;
}
- (UIFont *)placeholderFont
{
return _placeholderFont;
}
#pragma mark - Public methods
- (void)drawPlaceholderInRect:(CGRect)rect {
NSDictionary *attributes = @{
NSForegroundColorAttributeName:_placeholderTextColor,
NSFontAttributeName:_placeholderFont
};
// center vertically
CGSize textSize = [self.placeholder sizeWithAttributes:attributes];
CGFloat hdif = rect.size.height - textSize.height;
hdif = MAX(0, hdif);
rect.origin.y += ceil(hdif/2.0);
[[self placeholder] drawInRect:rect withAttributes:attributes];
}
- (CGRect)textRectForBounds:(CGRect)bounds - (CGRect)textRectForBounds:(CGRect)bounds
{ {
auto glview = cocos2d::Director::getInstance()->getOpenGLView(); auto glview = cocos2d::Director::getInstance()->getOpenGLView();

View File

@ -34,9 +34,10 @@ static const int CC_EDIT_BOX_PADDING = 5;
@property (nonatomic, retain, setter=ccui_setText:) NSString *ccui_text; @property (nonatomic, retain, setter=ccui_setText:) NSString *ccui_text;
@property (nonatomic, retain, setter=ccui_setPlaceholder:) NSString *ccui_placeholder; @property (nonatomic, retain, setter=ccui_setPlaceholder:) NSString *ccui_placeholder;
@property (nonatomic, retain, setter=ccui_setPlaceholderLabel:) UILabel *ccui_placeholderLabel;
@property (nonatomic, retain, setter=ccui_setTextColor:) UIColor *ccui_textColor; @property (nonatomic, retain, setter=ccui_setTextColor:) UIColor *ccui_textColor;
@property (nonatomic, retain, setter=ccui_setFont:) UIFont *ccui_font; @property (nonatomic, retain, setter=ccui_setFont:) UIFont *ccui_font;
@property (nonatomic, retain, setter=ccui_setPlaceholderTextColor:) UIColor *ccui_placeholderTextColor;
@property (nonatomic, retain, setter=ccui_setPlaceholderFont:) UIFont *ccui_placeholderFont;
@property (nonatomic, assign, setter=ccui_setSecureTextEntry:) BOOL ccui_secureTextEntry; @property (nonatomic, assign, setter=ccui_setSecureTextEntry:) BOOL ccui_secureTextEntry;
@property (nonatomic, assign, setter=ccui_setTextHorizontalAlignment:) NSTextAlignment ccui_alignment; @property (nonatomic, assign, setter=ccui_setTextHorizontalAlignment:) NSTextAlignment ccui_alignment;

View File

@ -76,6 +76,40 @@
self.textAlignment = ccui_alignment; self.textAlignment = ccui_alignment;
} }
- (UIColor *)ccui_placeholderTextColor
{
SEL selector = @selector(placeholderTextColor);
if ([self respondsToSelector:selector]) {
return [self performSelector:selector];
}
return nil;
}
- (void)ccui_setPlaceholderTextColor:(UIColor *)ccui_placeholderTextColor
{
SEL selector = @selector(setPlaceholderTextColor:);
if ([self respondsToSelector:selector]) {
[self performSelector:selector withObject:ccui_placeholderTextColor];
}
}
- (UIFont *)ccui_placeholderFont
{
SEL selector = @selector(placeholderFont);
if ([self respondsToSelector:selector]) {
return [self performSelector:selector];
}
return nil;
}
- (void)ccui_setPlaceholderFont:(UIFont *)ccui_placeholderFont
{
SEL selector = @selector(setPlaceholderFont:);
if ([self respondsToSelector:selector]) {
[self performSelector:selector withObject:ccui_placeholderFont];
}
}
- (BOOL)ccui_secureTextEntry - (BOOL)ccui_secureTextEntry
{ {
return self.secureTextEntry; return self.secureTextEntry;

View File

@ -46,15 +46,6 @@
return nil; return nil;
} }
- (UILabel *)ccui_placeholderLabel
{
SEL selector = @selector(placeHolderLabel);
if ([self respondsToSelector:selector]) {
return [self performSelector:selector];
}
return nil;
}
- (void)ccui_setPlaceholder:(NSString *)ccui_placeholder - (void)ccui_setPlaceholder:(NSString *)ccui_placeholder
{ {
SEL selector = @selector(setPlaceholder:); SEL selector = @selector(setPlaceholder:);
@ -85,12 +76,46 @@
- (NSTextAlignment)ccui_alignment - (NSTextAlignment)ccui_alignment
{ {
return self.textAlignment; return self.textAlignment;
} }
- (void)ccui_setTextHorizontalAlignment:(NSTextAlignment)ccui_alignment - (void)ccui_setTextHorizontalAlignment:(NSTextAlignment)ccui_alignment
{ {
self.textAlignment = ccui_alignment; self.textAlignment = ccui_alignment;
}
- (UIColor *)ccui_placeholderTextColor
{
SEL selector = @selector(placeHolderLabel);
if ([self respondsToSelector:selector]) {
return ((UILabel *)[self performSelector:selector]).textColor;
}
return nil;
}
- (void)ccui_setPlaceholderTextColor:(UIColor *)ccui_placeholderTextColor
{
SEL selector = @selector(placeHolderLabel);
if ([self respondsToSelector:selector]) {
((UILabel *)[self performSelector:selector]).textColor = ccui_placeholderTextColor;
}
}
- (UIFont *)ccui_placeholderFont
{
SEL selector = @selector(placeHolderLabel);
if ([self respondsToSelector:selector]) {
return ((UILabel *)[self performSelector:selector]).font;
}
return nil;
}
- (void)ccui_setPlaceholderFont:(UIFont *)ccui_placeholderFont
{
SEL selector = @selector(placeHolderLabel);
if ([self respondsToSelector:selector]) {
((UILabel *)[self performSelector:selector]).font = ccui_placeholderFont;
}
} }
- (BOOL)ccui_secureTextEntry - (BOOL)ccui_secureTextEntry