2015-07-02 11 views
6

Szukałem rozwiązania, które sprawiłoby, że klikalne linki działają. Mogę uzyskać to działa przy użyciu UITextView + NSAttributedString, ale po prostu nie jest autolayout poprawnie, gdy jest to UITableViewCell.Łącza TTTAttributedLabel są stylizowane, ale nie można klikać

Dodałem teraz TTTAttributedLabel do mojego projektu i idealnie pasuje do widoków. Łącza zmieniają kolor na niebieski i są podkreślone.

Jednak ich kliknięcie nic nie robi. Zrobiłem implementację TTTAttributedLabelDelegate na moim kontrolerze, utworzyłem etykietę w implementacji storyboardu MyLabel (która tylko rozszerza TTTAttributedLabel i ma opcje delegatów, ponieważ chcę, aby były uruchamiane wewnątrz tej samej funkcji). Na razie ustawiłem kontroler, aby był delegatem, o którym myślałem, że może nie działać, wskazując na siebie.

Ale żadna z tych funkcji nie zostanie zwolniona, mam w niej punkty przerwania i logi.

Zaimplementowałem program didSelectLinkWithUrl i didLongPressLinkWithUrl.

func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) { 
     Debug.log("link clicked") 
    } 
    func attributedLabel(label: TTTAttributedLabel!, didLongPressLinkWithURL url: NSURL!, atPoint point: CGPoint) { 
     Debug.log("link long clicked") 
    } 

Outlet

@IBOutlet weak var content: MyLabel! 

MyLabel

import UIKit import TTTAttributedLabel

class MyLabel : TTTAttributedLabel, TTTAttributedLabelDelegate { 

override func didMoveToSuperview() { 
    if (self.delegate == nil) { 
     self.delegate = self 
    } 
    self.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue 
    self.userInteractionEnabled = true 
} 

func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) { 
    Debug.log("link clicked") 
} 
func attributedLabel(label: TTTAttributedLabel!, didLongPressLinkWithURL url: NSURL!, atPoint point: CGPoint) { 
    Debug.log("link long clicked") 
} 

ktoś wie co mogę brakować?

Aktualizacja

I okazało się, że po prostu wklejając w url f/e http://example.com staje się aktywna i jest rzeczywiście klikalne i didSelectLinkWithUrl staje klikalny, allthough muszę mieć nadany ciąg i jest oparta na sznurku HTML.

+1

Swift 3/4: 'NSTextCheckingResult.CheckingType.link.rawValue' – aehlke

Odpowiedz

10

The implementation of setAttributedText: nie aktualizuje tablicy linkModels, a robi to the implementation of setText:. Uważam, że właśnie to powoduje twój problem.

Aby rozwiązać problem, ustaw właściwość etykiety na wartość text zamiast właściwości attributedText.

The docs obejmują także to ostrzeżenie:

TTTAttributedLabel może wyświetlać zarówno zwykły tekst i przypisać: wystarczy zdać NSString lub NSAttributedString do setText: seter. Nigdy nie przypisz do właściwości attributedText.

Docs pokazują również ten przykład wykorzystania:

TTTAttributedLabel *attributedLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero]; 

NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"Tom Bombadil" 
                   attributes:@{ 
     (id)kCTForegroundColorAttributeName : (id)[UIColor redColor].CGColor, 
     NSFontAttributeName : [UIFont boldSystemFontOfSize:16], 
     NSKernAttributeName : [NSNull null], 
     (id)kTTTBackgroundFillColorAttributeName : (id)[UIColor greenColor].CGColor 
}]; 

// The attributed string is directly set, without inheriting any other text 
// properties of the label. 
attributedLabel.text = attString; 
+0

@MathijsSegers dodałam trochę więcej szczegółów na temat przyczyn takiej sytuacji - ale zasadniczo, po prostu ustaw przypisany ciąg do właściwości 'text'. –

+0

@MathijsSegers może ukazywać kod szybkiego kodu, naprawdę nie mogę go uruchomić. Mówi: Nie można przypisać wartości typu "NSAttributedString" do typu "String?" – hugocarlmartin

+0

@hugocarlmartin, zamiast 'label.text = attrbText', użyj' label.setText (attrbText) '. –

1

Aaron Brager jest poprawny! Kiedyś miałem ten sam problem, ale mam to naprawić w Swift zastępując wiersz:

label.attributedText = attributedString 

z linii:

label.setText(attributedString) 

ten został zaakceptowany przez kompilator ponieważ metoda setText akceptuje AnyObject. Zwiększyłem również czcionkę przypisanego ciągu linku, więc przechwytuje mój kran i działa teraz! użyłem to dla łącza obcięcia, to jest cała część:

label.lineBreakMode = .ByTruncatingHead 
label.attributedTruncationToken = NSMutableAttributedString(string: "... Show more", attributes: [NSForegroundColorAttributeName: UIColor.cueCyan(), NSLinkAttributeName: readMoreLink, NSFontAttributeName: UIFont.formFont(.Light, size: fontSize+24)!]) 
label.userInteractionEnabled = true 
label.delegate = self 
Powiązane problemy