2014-12-01 3 views
0

이의 내가이 있다고 가정 해 봅시다 색깔. 그러나 두 번째 "ㅋ"는 괄호로 묶고 작은 크기와 회색을 사용합니다. 이 일을 어떻게 하죠? 심지어 가능할까요?다른 글꼴 스타일

답변

0

NSMutableAttributedString을 사용하려고합니다.

- setAttributes:range:을 호출하기 만하면 모든 사용자 문자열을 연결하고 각 하위 문자열에 원하는 특성을 표시 할 수 있습니다.

0

NSString을 사용하면 특정 스타일을 사용할 수 없습니다. 문자열에 스타일/속성이 필요하면 NSAttributedString을 사용해야합니다.

0

바로 지금은 NSString 개의 개체 만 저장합니다. NSString 그것에 연결된 글꼴이 없습니다. 당신이 글꼴 정보를 유지하려면 - 당신은 NSAttributedString 클래스에서 좀 걸릴 수 있습니다 - https://developer.apple.com/Library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/index.html

그래서 코드는 같을 것이다

self.items = @[ 
[[NSAttributedString alloc] initWithString: @"bla" attributes: @{ 
    NSFontAttributeName: [UIFont systemFontOfSize: 12.0], 
    NSForegroundColorAttributeName: [UIColor blackColor] 
}], 
[[NSAttributedString alloc] initWithString: @"bla" attributes: @{ 
    NSFontAttributeName: [UIFont systemFontOfSize: 14.0], 
    NSForegroundColorAttributeName: [UIColor grayColor] 
}] 
]; 
0

코드 아래에보십시오 : -

NSArray *yourArr= @[@"blah", 
         @"blah", 
         @"blah (blah)", 
         @"blah", 
         @"blah"]; 


    for (NSString *word in yourArr) 
    { 
     if ([word isEqualToString:@"blah (blah)"]) 
     { 

      [yourAtt appendAttributedString:[[NSAttributedString alloc]initWithString:[word componentsSeparatedByString:@" "][0] attributes:@{NSFontAttributeName:[NSFont boldSystemFontOfSize:18]}]]; 
      [yourAtt appendAttributedString:[[NSAttributedString alloc]initWithString:@" "]]; 
      [yourAtt appendAttributedString:[[NSAttributedString alloc]initWithString:[word componentsSeparatedByString:@" "][1] attributes:@{NSFontAttributeName:[NSFont boldSystemFontOfSize:14],NSForegroundColorAttributeName:[NSColor grayColor]}]]; 
     } 
     else 
     { 
      [yourAtt appendAttributedString:[[NSAttributedString alloc]initWithString:word]]; 
     } 
     [yourAtt appendAttributedString:[[NSAttributedString alloc]initWithString:@",\n"]]; 

    } 
    self.yourAttStr=yourAtt; 

enter image description here