development

iOS 7에서 UIPickerView의 텍스트 색상을 어떻게 변경합니까?

big-blog 2020. 7. 21. 07:32
반응형

iOS 7에서 UIPickerView의 텍스트 색상을 어떻게 변경합니까?


나는 알고 있어요 pickerView:viewForRow:forComponent:reusingView방법 만 사용하는 경우 view가에 전달을 reusingView:어떻게하면 다른 텍스트 색상을 사용하여 변경합니까? view.backgroundColor = [UIColor whiteColor];뷰를 사용 하지 않으면 더 이상 표시되지 않습니다.


델리게이트 메서드에는 더 우아한 함수가 있습니다.

목표 -C :

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title = @"sample title";
    NSAttributedString *attString = 
        [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    return attString;
}

선택 막대 색상도 변경하려면 선택기 높이 180의 간격으로 35pt 간격으로 2 개를 별도 UIViews의 뷰 에 추가해야한다는 것을 알았습니다 UIPickerView.

스위프트 3 :

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let string = "myString"
    return NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName:UIColor.white])
}

스위프트 4 :

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let string = "myString"
    return NSAttributedString(string: string, attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
}

스위프트 4.2 :

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let string = "myString"
    return NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
}

메소드를 사용할 때 기억하십시오.를 사용할 때 titleForRowInComponent()호출 되지 않으므로 구현할 필요가 없습니다 attributedTitleForRow().


여기에 원래 게시물 : 나는 iOS7에있는 날짜 선택기의 글꼴 색상을 변경할 수 있습니까?

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
    label.backgroundColor = [UIColor grayColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
    label.text = [NSString stringWithFormat:@" %d", row+1];
    return label;
}

// number Of Components
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// number Of Rows In Component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:   (NSInteger)component
{
    return 6;
}

  1. 스토리 보드로 이동
  2. PickerView 선택
  3. Go to Identity inspector (3rd tab)
  4. Add User Defined Runtime Attribute
  5. KeyPath = textColor
  6. Type = Color
  7. Value = [Color of you choice]

screenshot


in Xamarin, override the UIPickerModelView method GetAttributedTitle

public override NSAttributedString GetAttributedTitle(UIPickerView picker, nint row, nint component)
{
    // change text white
    string title = GetTitle (picker, row, component); // get current text from UIPickerViewModel::GetTitle
    NSAttributedString ns = new NSAttributedString (title, null, UIColor.White); // null = font, use current font
    return ns;
}

I ran into the same problem with a pickerView using two components. My solution is similar to above with a few modifications. Because I am using two components it is necessary to pull from two different arrays.

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

    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor blueColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];

    //WithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 60)];

    if(component == 0)
    {
        label.text = [countryArray objectAtIndex:row];
    }
    else
    {
        label.text = [cityArray objectAtIndex:row];
    }
    return label;
}

Swift 4 (Update to the accepted answer)

extension MyViewController: UIPickerViewDelegate{
    }

    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        return NSAttributedString(string: "your-title-goes-here", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
        UILabel* pickerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 37)];
        pickerLabel.text = @"text";
        pickerLabel.textColor = [UIColor redColor];
        return pickerLabel;
}

참고URL : https://stackoverflow.com/questions/19232817/how-do-i-change-the-color-of-the-text-in-a-uipickerview-under-ios-7

반응형