wisdominessence
V2EX  ›  macOS

UIPickerView 怎么去更改选中 item 的字体颜色,在线等。。求助

  •  
  •   wisdominessence · Dec 30, 2014 · 9711 views
    This topic created in 4176 days ago, the information mentioned may be changed or developed.

    试了一下不行
    这个也不行

    能否在 didSelectRow 中修改 选中 item 的字体颜色。

    4 replies    2014-12-30 15:54:37 +08:00
    icestraw
        1
    icestraw  
       Dec 30, 2014
    新建一个 property rowA来保存当前选定的行号,一个property listArray用来保存每一行的View, property array保存数据
    然后另外实现这两个方法

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
    UILabel *lbl = [UILabel new];
    NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithString:[_array objectAtIndex:row]];
    NSRange range = NSRangeFromString([_array objectAtIndex:row]);
    [aStr addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:range];
    lbl.attributedText = aStr;
    if (row == _rowA) {
    lbl.textColor = [UIColor redColor];
    }
    [_listArray addObject:lbl];
    return lbl;
    }


    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
    _rowA = row;
    [_testPicker reloadAllComponents];
    }
    希望有所帮助
    icestraw
        2
    icestraw  
       Dec 30, 2014
    @icestraw Oh,在这里Attributed String可以不用>_<
    wisdominessence
        3
    wisdominessence  
    OP
       Dec 30, 2014
    @icestraw 能加我QQ 嘛,我把代码发你看看 叁9柒壹45215, 可以有偿咨询。
    smartv
        4
    smartv  
       Dec 30, 2014
    @icestraw 已解决。感谢回答
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2961 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:08 · PVG 17:08 · LAX 02:08 · JFK 05:08
    ♥ Do have faith in what you're doing.