2013-06-03 4 views
0

# 및 7이 위 첨자 인 "G # m7"과 같은 코드 이름을 만들기 위해 속성이 지정된 문자열을 추가하려고합니다. 나는 이렇게하고있다 :NSAttributedStrings가 예상대로 작동하지 않음을 추가했습니다.

NSFont* font = [NSFont fontWithName:kGillSans size:24]; 
NSMutableDictionary* attributeNormal = [[NSMutableDictionary alloc] init]; 
[attributeNormal setObject:font forKey:NSFontAttributeName]; 
[attributeNormal setObject:color forKey:NSForegroundColorAttributeName]; 

NSFont* fontSmall = [NSFont fontWithName:kGillSans size:14]; 
NSMutableDictionary* attributeSuperScript = [[NSMutableDictionary alloc] init]; 
[attributeSuperScript setObject:fontSmall forKey:NSFontAttributeName]; 
[attributeSuperScript setObject:[NSNumber numberWithInteger:5] forKey:NSSuperscriptAttributeName]; 
[attributeSuperScript setObject:color forKey:NSForegroundColorAttributeName]; 


NSAttributedString* pitch = [[NSAttributedString alloc] initWithString:@"G" attributes:attributeNormal]; 

NSAttributedString* accidental = [[NSAttributedString alloc] initWithString:@"#" attributes:attributeSuperScript]; 

NSAttributedString* quality = [[NSAttributedString alloc] initWithString:@"m" attributes:attributeNormal]; 

NSAttributedString* seventh = [[NSAttributedString alloc] initWithString:@"7" attributes:attributeSuperScript]; 

NSMutableAttributedString* fullString = [[NSMutableAttributedString alloc] initWithAttributedString:pitch]; 
[fullString appendAttributedString:accidental]; 
[fullString appendAttributedString:quality]; 
[fullString appendAttributedString:seventh]; 

나는 이것을 표시하기 위해 CATextLayer에 추가한다. 불행히도 예상대로 작동하지 않습니다. 위 첨자는 위 첨자가 아니며, 함께 반환되는 [전체 문자열 크기]는 꺼져있어 레이어를 배치하기 어렵습니다. 크기 문제를 설명하기 위해 테두리 폭을 설정했습니다.

enter image description here

나는 첨자 속성에 대한 다른 글꼴 크기와 값으로 실험을했습니다. 어떤 아이디어?

답변

0

이 시도 :

NSFont* font = [NSFont fontWithName:@"GillSans" size:24]; 
NSDictionary * attributeNormal = @{NSFontAttributeName: font, 
            NSForegroundColorAttributeName: [NSColor blackColor]}; 

NSFont* fontSmall = [NSFont fontWithName:@"GillSans" size:14]; 
NSDictionary * attributeSuperScript = @{NSFontAttributeName: fontSmall, 
             NSSuperscriptAttributeName: @(2), 
             NSForegroundColorAttributeName: [NSColor blackColor]}; 

NSMutableAttributedString* fullString = 
[[NSMutableAttributedString alloc] 
initWithAttributedString:[[NSAttributedString alloc] 
          initWithString:@"G" 
          attributes:attributeNormal]]; 

[fullString appendAttributedString: 
[[NSAttributedString alloc] initWithString:@"#" 
           attributes:attributeSuperScript]]; 

[fullString appendAttributedString: 
[[NSAttributedString alloc] initWithString:@"m" 
           attributes:attributeNormal]]; 

[fullString appendAttributedString: 
[[NSAttributedString alloc] initWithString:@"7" 
           attributes:attributeSuperScript]]; 
+0

를 그냥 FYI : NSAttributedString은 * fullString = [NSAttributedString은 ALLOC] initWithArray :하십시오 NSAttributedString은 초기화하는 속성/문자열의 배열을 취하는 방법 I은 NSAttributedString은 카테고리 initWithArray이 @ [attributeNormal, @ "Welcome to", attributeBold, @ "GeoWorld", attributeNormal, @ "! (당장 집으로 돌아 간다)"]]; 사본을 원하면 메시지를 보내주십시오. – geowar

+0

내 보관 용 보관함에 보관했습니다. . – geowar

+0

안녕하세요 고마워,하지만 그건 내 코드와 다른 점이 아니며 결과도 같습니다. – olynoise