, . , ( ). .
, swizzling, ( , , ).
UIWebSelectSinglePicker ( ), UIPickerViewDelegate. NSAttributedString, - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component. , , .
, UIPickerView:
@implementation UIPickerView (LabelColourOverride)
- (NSAttributedString *)overridePickerView:(UIPickerView *)pickerView
attributedTitleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
NSMutableAttributedString* title =
(NSMutableAttributedString*)[self overridePickerView:pickerView
attributedTitleForRow:row
forComponent:component];
[title setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]}
range:NSMakeRange(0, title.length)];
pickerView.backgroundColor = [UIColor yellowColor];
return title;
}
@end
, swizzling (. ), :
[Swizzle swizzleClass:NSClassFromString(@"UIWebSelectSinglePicker")
method:@selector(pickerView:attributedTitleForRow:forComponent:)
forClass:[UIPickerView class]
method:@selector(overridePickerView:attributedTitleForRow:forComponent:)];
Swizzle, .
@implementation Swizzle
+ (void)swizzleClass:(Class)originalClass
method:(SEL)originalSelector
forClass:(Class)overrideClass
method:(SEL)overrideSelector
{
Method originalMethod = class_getInstanceMethod(originalClass, originalSelector);
Method overrideMethod = class_getInstanceMethod(overrideClass, overrideSelector);
if (class_addMethod(originalClass,
originalSelector,
method_getImplementation(overrideMethod),
method_getTypeEncoding(overrideMethod))) {
class_replaceMethod(originalClass,
overrideSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
}
else {
method_exchangeImplementations(originalMethod, overrideMethod);
}
}
@end
, , , , , , NSAttributedString, . , . , .
Picker , , ( ).