2014-11-17 1 views
0

기본적으로 특정 위치가 충족 될 때 다른 위치 (newPostion)를 확인하는 방법이 있습니다. 특정 레이블을 다음과 같이 굵게 표시합니다. 당신은 아래에서 볼 수 있습니다. 그러나, 이것은 상당히 많은 코드처럼 보입니다. 이 작업을 수행하는 더 좋은 방법이 있습니까? 더보기 좋게하려면 리팩토링이 필요하지만 어디서부터 시작해야할지 모르겠습니다. 오류 wjem의 적은 기회가 처음 작성 및 오류 나중에 개정 될 때의 적은 변화 :iOS : 레이블을 변경하는 코드 조각을 쓰는 더 좋은 방법이 필요합니다. fontSize (굵게 및 보통)

if(newPosition == 100){ 

    self.label.font = [utilClass boldFontWithSize:12]; 
    self.label1.font = [utilClass fontWithSize:12]; 
    self.label2.font = [utilClass fontWithSize:12]; 
    self.label3.font = [utilClass fontWithSize:12]; 
    self.label4.font = [utilClass fontWithSize:12]; 

} 

else if(newPosition == 200){ 

    self.label.font = [utilClass fontWithSize:12]; 
    self.label1.font = [utilClass boldFontWithSize:12]; 
    self.label2.font = [utilClass fontWithSize:12]; 
    self.label3.font = [utilClass fontWithSize:12]; 
    self.label4.font = [utilClass fontWithSize:12]; 
} 
else if (newPosition == -100) { 

    self.label.font = [utilClass fontWithSize:12]; 
    self.label1.font = [utilClass fontWithSize:12]; 
    self.label2.font = [utilClass boldFontWithSize:12]; 
    self.label3.font = [utilClass fontWithSize:12]; 
    self.label4.font = [utilClass fontWithSize:12]; 

} 

else (newPosition == -200) { 

    self.label.font = [utilClass fontWithSize:12]; 
    self.label1.font = [utilClass fontWithSize:12]; 
    self.label2.font = [utilClass fontWithSize:12]; 
    self.label3.font = [utilClass boldFontWithSize:12]; 
    self.label4.font = [utilClass fontWithSize:12]; 

} 

답변

1

하나의 해결책이 될 수 :

UIFont *font = [utilClass fontWithSize:12]; 
UIFont *bold = [utilClass boldFontSize:12]; 

self.label.font = font; 
self.label1.font = font; 
self.label2.font = font; 
self.label3.font = font; 
self.label4.font = font; 

if (newPosition == 100) { 
    self.label.font = boldFont; 
} else if (newPosition == 200) { 
    self.label1.font = boldFont; 
} else if (newPosition == -100) { 
    self.label2.font = boldFont; 
} else if (newPosition == -200) { 
    self.label3.font = boldFont; 
} 

이것은 중복 코드를 많이 방지 할 수 있습니다.

별도의 ivars 대신 레이블 배열을 사용하면 훨씬 쉽게 만들 수 있습니다.

NSArray *labels = @[ self.label, self.label1, self.label2, self.label3, self.label4 ]; 
UIFont *font = [utilClass fontWithSize:12]; 
UIFont *bold = [utilClass boldFontSize:12]; 

for (UILabel *label in labels) { 
    label.font = font; 
} 

if (newPosition == 100) { 
    labels[0].font = boldFont; 
} else if (newPosition == 200) { 
    labels[1].font = boldFont; 
} else if (newPosition == -100) { 
    labels[2].font = boldFont; 
} else if (newPosition == -200) { 
    labels[3].font = boldFont; 
} 
0

예, 적은 코드는, 특히 적은 반복 코드가 더 좋다.

이런 식으로 무엇이 일어나고 있는지, 하나의 것이 newPosition마다 변경된다는 것을 알 수 있습니다.

UIFont *baseFont = [utilClass fontWithSize:12]; 
UIFont *boldFont = [utilClass boldFontSize:12]; 

self.label.font = baseFont; 
self.label1.font = baseFont; 
self.label2.font = baseFont; 
self.label3.font = baseFont; 
self.label4.font = baseFont; 

switch (newPosition) { 
    case 100: 
     self.label.font = boldFont; 
     break; 
    case 200: 
     self.label1.font = boldFont; 
     break; 
    case -100: 
     self.label2.font = boldFont; 
     break; 
    case -200: 
     self.label3.font = boldFont; 
} 
관련 문제