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;
}
- 스토리 보드로 이동
- PickerView 선택
- Go to Identity inspector (3rd tab)
- Add User Defined Runtime Attribute
- KeyPath = textColor
- Type = Color
- Value = [Color of you choice]
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;
}
'development' 카테고리의 다른 글
이미지가 언제로드되는지 알기 위해 JavaScript 콜백을 만드는 방법은 무엇입니까? (0) | 2020.07.22 |
---|---|
MSDeploy.exe를 통해 WMSvc에서 404 얻기 (0) | 2020.07.21 |
접기와 축소의 차이점은 무엇입니까? (0) | 2020.07.21 |
Rails에서 언제 "has_many : through"관계를 사용해야합니까? (0) | 2020.07.21 |
GitHub README.md에서 이미지를 나란히 표시하려면 어떻게해야합니까? (0) | 2020.07.21 |