2013-03-10 16 views
9

Próbuję podkreślić tekst w etykiecie. Jednak nie wiem, jak uzyskać zakres całego tekstu na etykiecie. Oto, co mam do tej pory:Jak mogę podkreślić tekst w systemie iOS 6?

NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy]; 
[mat addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)} range://??]; 
self.tableLabel.attributedText = mat; 

Co powinienem dodać do zakresu?

+0

http://stackoverflow.com/questions/2711297/underline-text-in- uilabel –

Odpowiedz

22

Dla zakresu może chcesz używać:

NSMakeRange (0, mat.length); 

tak:

NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy]; 
[mat addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)} range:NSMakeRange (0, mat.length)]; 
self.tableLabel.attributedText = mat; 
+2

NSMakeRange() jest prawdopodobnie lepszy, ponieważ nie opiera się na niejawnych literałach C99, które mają pewne problemy. –

+0

Prawda, aktualizacja odpowiedzi. – Ares

+0

działa świetnie! Jak działa NSMakeRange? – user1420042

0
NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:strComplete]; 

[attributedString addAttribute:NSUnderlineStyleAttributeName 
         value:@(NSUnderlineStyleSingle) 
         range:[strComplete rangeOfString:strFirst]]; 

[attributedString addAttribute:NSForegroundColorAttributeName 
         value:[UIColor redColor] 
         range:[strComplete rangeOfString:strSecond]]; 


cell.textLabel.attributedText = attributedString; 
+0

Należy pamiętać, że odradza się tylko odpowiedzi w formie kodu. – GhostCat

Powiązane problemy