2014-01-30 3 views
0

NSString을 그릴 수는 있지만 그릴 때 크기를 조정할 수 없었습니다. 내의 drawRect : 방법은 다음과 같습니다NSString atPoint withAttributes를 그리는 방법 및 SIZE를 조정 하시겠습니까?

- (void)drawRect:(CGRect)rect 
{ 

NSArray *objectArray = [NSArray arrayWithObjects:[UIFont systemFontOfSize:80.0f], nil]; 
NSArray *keyArray = [NSArray arrayWithObjects:@"NSFontAttributeName", nil]; 
NSMutableDictionary *textAttributes = [NSMutableDictionary dictionaryWithObjects:objectArray forKeys:keyArray]; 
NSString *myTestString = @"Test String"; 
[textAttributes setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName]; 
[myTestString drawAtPoint:CGPointMake(20, 30) withAttributes:textAttributes]; 
[myTestString drawInRect:CGRectMake(50, 50, 500, 500) withAttributes:textAttributes]; 

NSLog(@"wrote %@ with %@", myTestString, textAttributes); 
} 

TextAttribute가 좋은보고, 폰트 정보로 반환됩니다

NSFontAttributeName = "<UICTFont: 0x14eeff40> font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 80.00pt" 

내가 속성의 배열을 올바르게 색상을 변경할 수 있습니다, 왜 텍스트에서이 결과를 않습니다 기본 10pt 크기입니까?

답변

1

NSAttributedString은 원하는 것입니다. (폴 Hegarty 및 available on iTunes에 의해 운영) iOS의 스탠포드 U MOOC 코스 NSAttributedString은의 개요이 강의에 사용할 수 있습니다

#pragma mark - UIPickerViewDataSource 

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    NSMutableAttributedString *attrTitle = nil; 

    // note: for the custom picker we use custom views instead of titles 
    if (pickerView == self.myPickerView) 
    { 
     if (row == 0) 
     { 
      NSString *title; 
      if (component == 0) 
       title = [self.pickerViewArray objectAtIndex:row]; 
      else 
       title = [[NSNumber numberWithInt:row] stringValue]; 

      // apply red text for normal state 
      attrTitle = [[NSMutableAttributedString alloc] initWithString:title]; 
      [attrTitle addAttribute:NSForegroundColorAttributeName 
          value:[UIColor redColor] 
          range:NSMakeRange(0, [attrTitle length])]; 
     } 
    } 

    return attrTitle; 
} 

4. 강의 (5) 또한 NSAttributedString은 코드를 특징 : UICatalog 샘플 코드는 NSAttributedString은 사용의 예를 가지고 따라 할 수있는 데모. 마지막으로 github 사용자 m2mtech는 repositories of all code exercises and assignments for the course을 게시했으며 관련 프로젝트 파일 here을 다운로드 할 수 있습니다.

+0

NSMutableAttributedString을 사용할 수도 있습니다. –

+0

최소한의 속성 집합 만 조정할 수 있다면 drawAtPoit : withAttributes를 사용해야하는 이유를 이해할 수 없을 것입니다. – ChrisBob