2013-07-26 1 views
2

나는 두 개의 서로 다른 색깔의 문자열을 표시 할 필요가있는 UILabel의이 : 다음은 내 코드입니다 :여러 가지 빛깔의 5

는 미세하지만의에서 iOS 5 및 이전 버전을 작동하는 아이폰 OS 6에서
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: lbl_question.attributedText]; 

[text addAttribute: NSForegroundColorAttributeName value: [UIColor colorWithRed:52.0f/255.0f green:104.0f/255.0f blue:165.0f/255.0f alpha:1.0f] range: NSMakeRange(0,[result integerValue]+1)]; 

[text addAttribute: NSForegroundColorAttributeName value: [UIColor colorWithRed:75.0f/255.0f green:75.0f/255.0f blue:75.0f/255.0f alpha:2.0f] range: NSMakeRange([result integerValue]+1,[strq length])]; 
[lbl_question setAttributedText: text]; 

두 문자열은 매번 ovelapped있어. 또한 텍스트 및 글꼴에 따라 너비를 가져오고 싶습니다. 텍스트에 따라 높이를 높이십시오.

내가 솔루션 ...이 좀 도와주세요 거기있다 확신합니다 .... NSAttributedString은 UIKit 추가 참조에서

+0

기인 문자열 지원은 당신이 DTCoreText 프레임 워크 같은 것을 함께 가야 할 것이다 이전의 OS가 iOS 6에 추가 된 . –

답변

7

NSMutableAttributedString을 사용하여이 작업을 수행 할 수 있습니다. ios 6, ios 7 및 ios 8에서도 작동합니다.

NSMakeRange (시작점은 0부터 시작하여 문자 수)입니다.

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"RedGreenBlue"]; 
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,3)]; 
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3,5)]; 
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(8,4)]; 
    lable.attributedText=string; 

감사

등 UILabel의에 대한
+0

모든 속성에 대해 "https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/AttributedStrings/Articles/standardAttributes.html#//apple_ref/doc/uid/TP40004903-SW2" – Rinku

+0

링크를 엽니 다. 이것이 최고의 답입니다. –

-1

:

NSForegroundColorAttributeName이 속성의 값은이다 UIColor 개체입니다. 이 속성을 사용하여 렌더링 중 텍스트 의 색상을 지정합니다. 이 속성을 지정하지 않으면 텍스트는 검은 색으로 렌더링 된 입니다. iOS 6.0 이상에서 사용할 수 있습니다.

두 개의 레이블이 필요하거나 TTTAttributedLabel 또는 DTCoreText와 같은 것을 사용해야합니다.

+0

아니요, 하나의 라벨에 넣고 싶습니다. –

-1

시도 TTTAttributedLabel. NSAttributedStrings을 지원하는 UILabel의 서브 클래스로, 동일한 문자열에 여러 색상, 글꼴 및 스타일을 쉽게 가질 수 있습니다.