2019年6月11日 星期二

obj-c 保護你的delegate method calling


@protocol SearchViewDelegate<NSObject>

@optional
- (void)searchTextFieldDidBeginEditing;
- (void)onSearchTextFieldValueChanged:(NSString *)text;
@end


@interface SearchView()


@end


@implementation SearchView
typedef struct {
    unsigned int searchTextFieldDidBeginEditing:1;
    unsigned int onSearchTextFieldValueChanged:1;
} DelegateRespondsTo;
DelegateRespondsTo delegateRespondsTo;

- (void)setDelegate:(id <SearchViewDelegate>)aDelegate {
    if (_delegate != aDelegate) {
        _delegate = aDelegate;
        
        delegateRespondsTo.searchTextFieldDidBeginEditing = [_delegate respondsToSelector:@selector(searchTextFieldDidBeginEditing)];
        delegateRespondsTo.onSearchTextFieldValueChanged = [_delegate respondsToSelector:@selector(onSearchTextFieldValueChanged:)];
    }
}

2019年5月7日 星期二

textFieldShouldClear dismiss keyboard

func textFieldShouldClear(_ textField: UITextField) -> Bool {

    textField.resignFirstResponder()
        
    textField.text = nil
        
    return false
}

2019年4月8日 星期一

改變UISegmentedControl的font

#import "UISegmentedControl+Font.h"

@implementation UISegmentedControl(Font)

- (void)setFont:(UIFont *)font color:(UIColor *)color {
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, [UIColor blackColor], NSForegroundColorAttributeName, nil];
    [self setTitleTextAttributes:attributes forState:UIControlStateNormal];
    NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [self setTitleTextAttributes:highlightedAttributes forState:UIControlStateSelected];
}

@end