2015-03-13 11 views
14

Jak ustawić kolor szablonu na załączonym ciągu znaków?Jak ustawić kolor obrazu szablonowego w NSTextAtuowanie

Tło:

Mam UILabel i mam ustawienie jego attributedText do NSAttributedString. NSAttributedString zawiera NSTextAttachment z małym obrazem. Teraz chcę, aby mój kolor obrazu był zgodny z kolorem tekstu i nie wiem, jak to zrobić.

Normalnie spodziewam się pokolorować obraz, ustawiając jego tryb renderowania na UIImageRenderingModeAlwaysTemplate, a następnie ustawiając kolor tintColor na zawierającym UIView. Próbowałem ustawić tintColor na mojej UILabel, ale to nie ma wpływu.

Oto mój kod. Jest w Ruby (RubyMotion) więc składnia może wyglądać trochę śmiesznie, ale mapy 1: 1 z Objective C

attachment = NSTextAttachment.alloc.initWithData(nil, ofType: nil) 
attachment.image = UIImage.imageNamed(icon_name).imageWithRenderingMode(UIImageRenderingModeAlwaysTemplate) 

label_string = NSMutableAttributedString.attributedStringWithAttachment(attachment) 
label_string.appendAttributedString(NSMutableAttributedString.alloc.initWithString('my text', attributes: { NSFontAttributeName => UIFont.preferredFontForTextStyle(UIFontTextStyleFootnote), NSForegroundColorAttributeName => foreground_color })) 

label = UILabel.alloc.initWithFrame(CGRectZero) 
label.tintColor = foreground_color 
label.attributedText = label_string 
label.textAlignment = NSTextAlignmentCenter 
label.numberOfLines = 0 
+0

Czy znalazłeś jakieś odpowiedzi na to pytanie, które nie wymagają rysowania obrazu przed dodaniem go jako załącznika tekstowego? Chciałbym mieć prosty sposób robienia tego. –

+0

Niestety nie. W moim przypadku miałem sznur, który był czasem biały na ciemnym tle, a czasem czarny na jasnym tle ... Skończyło się na tym, że zmieniłem obraz załącznika na neutralną szarość, która działała na obu podłożach. – Ghazgkull

Odpowiedz

8

mam dobre doświadczenia z wykorzystaniem biblioteki UIImage+Additions podczas barwienia UIImage jest. Można go znaleźć tutaj: https://github.com/vilanovi/UIImage-Additions. Szczególnie sprawdź sekcję IV.

Jeśli dodanie biblioteki innej firmy nie jest opcją, jest tu coś na początek:

- (UIImage *)colorImage:(UIImage *)image color:(UIColor *)color 
{ 
    UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextTranslateCTM(context, 0, image.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); 

    CGContextSetBlendMode(context, kCGBlendModeNormal); 
    CGContextDrawImage(context, rect, image.CGImage); 
    CGContextSetBlendMode(context, kCGBlendModeSourceIn); 
    [color setFill]; 
    CGContextFillRect(context, rect); 


    UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return coloredImage; 
} 

To zrób UIImage przejść od: UIImage before tinting

Aby

UIImage after tinting

+0

Dzięki za wskaźnik. Mając nadzieję, że nie będę musiał wnieść całej nowej biblioteki, aby rozwiązać ten problem. – Ghazgkull

+0

@Ghazgkull Dodano przykład kodu, aby rozpocząć. Daj mi więcej informacji o tym, jak chcesz odcień, jeśli nie chcesz tego zrobić. –

+0

Normalnie odcień obrazu, po prostu wywołując UIImage.imageWithRenderingMode (UIImageRenderingModeAlwaysTemplate), a następnie ustawienie tintColor zawierający UIView. To tylko dwie linie kodu.Jedyną wadą jest to, że mój UIImage znajduje się wewnątrz NSAttributedString, więc nie mam dostępu do immedate UIView, który go zawiera. – Ghazgkull

22

Wygląda na to, że w UIKit występuje błąd. Istnieje obejście tego problemu;]

Z jakiegoś powodu musisz dołączyć pustą przestrzeń przed załącznikiem obrazu, aby działała poprawnie z UIImageRenderingModeAlwaysTemplate.

Więc fragment będzie wyglądać (kopalnia jest w ObjC):

- (NSAttributedString *)attributedStringWithValue:(NSString *)string image:(UIImage *)image { 
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; 
    attachment.image = image; 

    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; 
    NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:[[NSAttributedString alloc] initWithString:@" "]]; 
    [mutableAttributedString appendAttributedString:attachmentString]; 
    [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:0] range:NSMakeRange(0, mutableAttributedString.length)]; // Put font size 0 to prevent offset 
    [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, mutableAttributedString.length)]; 
    [mutableAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]]; 

    NSAttributedString *ratingText = [[NSAttributedString alloc] initWithString:string]; 
    [mutableAttributedString appendAttributedString:ratingText]; 
    return mutableAttributedString; 
} 
+0

Wygląda na to, że tak jest i jest to przydatna informacja. Myślę, że pójdę z podejściem do barwienia obrazu, ale dobrze jest wiedzieć. –

+0

Znalazłem, że 'NSForegroundColorAttributeName' nie zabarwił załączonego obrazu. Zamiast tego pobierał go z właściwości 'textColor' z' UILabel', mimo że nadpisano to w przypisanym łańcuchu. – Samah

+1

Możesz użyć pustego ciągu zamiast spacji, ponieważ spacja zostanie dodana do układu. [[NSMutableAttributedString alloc] initWithAttributedString: [[NSAttributedString alloc] initWithString: @ "\ 0"]]; – railwayparade

1

Rozwiązanie przez @blazejmar działa, ale nie jest konieczne. Wszystko, co musisz zrobić, aby to zadziałało, jest ustawione na kolor po połączeniu przypisanych ciągów. Oto przykład.

NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; 
attachment.image = [[UIImage imageNamed:@"ImageName"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 

NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; 

NSString *string = @"Some text "; 
NSRange range2 = NSMakeRange(string.length - 1, attachmentString.length); 

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; 
[attributedString appendAttributedString:attachmentString]; 
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range2]; 
self.label.attributedText = attributedString; 
-1

użyć UIImageRenderingModeAlwaysOriginal dla oryginalnego koloru obrazu. UIImageRenderingModeAlwaysTemplate dla niestandardowego koloru.