如何直接改变字体颜色?

时间:2015-09-19 09:41:28

标签: ios swift uiactionsheet actionsheetpicker

当用户从ActionSheetPicker中选择字体颜色时,我允许在颜色改变时更改文本的字体颜色。我使用第三方库https://github.com/skywinder/ActionSheetPicker-3.0
我该怎么办? 我必须更改第三方库或自定义ActionSheetPicker。

是否有任何值更改事件才能实现此功能?

这是我想要的图像。 http://i.stack.imgur.com/J2bz0.png

2 个答案:

答案 0 :(得分:0)

showPickerWithTitle:rows:initialSelection:doneBlock:cancelBlock:origin的完成区块中 您可以知道选择了哪个索引或值,并相应地更改了文本的颜色。

[ActionSheetStringPicker showPickerWithTitle:@"Select a Color"
                                        rows:colors
                            initialSelection:0
                                   doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {                                       
                                      yourLabel.textColor = ...
                                    }
                                 cancelBlock:^(ActionSheetStringPicker *picker) {
                                      NSLog(@"Block Picker Canceled");
                                    }
                                      origin:sender];

答案 1 :(得分:0)

首先在你的项目中导航到Picker文件夹并打开ActionSheetStringPicker.m 然后添加以下代码

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{

     NSString *str_lbl = (self.data)[(NSUInteger) row];

    //NSLog(@"////////%@",obj);

    UILabel *label = (UILabel*) view;
    if (label == nil)
    {
        label = [[UILabel alloc] init];
    }

    [label setText:str_lbl];

    // This part just colorizes everything, since you asked about that.

    [label setTextColor:[UIColor whiteColor]];

    if ([str_lbl isEqualToString:@"Red"])
    {
        [label setTextColor:[UIColor redColor]];
    }
    else if([str_lbl isEqualToString:@"Green"])
    {
        [label setTextColor:[UIColor greenColor]];
    }
    else if([str_lbl isEqualToString:@"Blue"])
    {
        [label setTextColor:[UIColor blueColor]];
    }
    else if([str_lbl isEqualToString:@"Orange"])
    {
        [label setTextColor:[UIColor orangeColor]];
    }
    else
    {

    }

   // [label setBackgroundColor:[UIColor blackColor]];
    CGSize rowSize = [pickerView rowSizeForComponent:component];
    CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
    [label setFrame:labelRect];

    return label;
}