2014-03-26 14 views
6

Ich versuche, ein UIView zu einem UILabel, hinzufügen, so dass der Text der Maske ist die Ansicht ist, mich ermöglicht, Dinge wie animierte Texthintergründe zu tun (ähnlich wie die Dia-Label auf dem zu entsperren Sperrbildschirm).Anzeige UIView mit einem UILabel Text Maske

Die Art, wie ich es zu tun plante, war mit der mask Eigenschaft auf der Ansicht layer, um es auf die Form des Textes zu maskieren. Allerdings kann ich keinen Weg finden, um die UILabel Textform als CALayer zu erhalten.

Ist das überhaupt möglich? Ich kann nur Lösungen finden, die die -(void)drawRect: Methode in UILabel überschreiben, aber das würde mir nicht viel Flexibilität geben.

+2

Hilft dies [http://stackoverflow.com/questions/17817378/ios-uiview-subclass-draw-see-through-text-to-background]? – Linuxios

+0

Ja, vielen Dank! Ich habe meine Implementierung als Antwort veröffentlicht. – Hamish

Antwort

6

UIView added the maskView property in IOS 8.0. Erstellen Sie jetzt nur eine UILabel als Maske zu verwenden, um eine UIView:

Objective-C:

UILabel* label = [[UILabel alloc] initWithFrame:self.view.frame]; 
label.text = @"Label Text"; 
label.font = [UIFont systemFontOfSize:70]; 
label.textAlignment = NSTextAlignmentCenter; 
label.textColor = [UIColor whiteColor]; 

UIView* overlayView = [[UIView alloc] initWithFrame:self.view.frame]; 
overlayView.backgroundColor = [UIColor blueColor]; 
overlayView.maskView = label; 
[self.view addSubview:overlayView]; 

Swift 2:

let label = UILabel.init(frame: view.frame) 
label.text = "Label Text" 
label.font = UIFont.systemFontOfSize(70) 
label.textAlignment = .Center 
label.textColor = UIColor.whiteColor() 

let overlayView = UIView.init(frame: view.frame) 
overlayView.backgroundColor = UIColor.blueColor() 
overlayView.maskView = label 

view.addSubview(overlayView) 

Dies schafft eine knackige UILabel mit UIColor.blueColor() Farbe genommen von overlayView.

+0

ohh, das ist eine nette Ergänzung zu UIKit! Habe das vorher nicht gesehen. Da dies flexibler ist, werde ich es als die akzeptierte Antwort markieren :) – Hamish

+2

Da die Frage ursprünglich mit obj-c gekennzeichnet war, habe ich eine obj-c-Version des Codes zu Ihrer Antwort hinzugefügt. – Hamish

5

mopsled's solution ist flexibler, wenn Sie für iOS 8+ erstellen. Wenn Sie jedoch nach einer Pre-iOS-8-Antwort suchen, hier ist es.


Dank Linuxios für mich this question zeigen. Der Schlüssel ist CATextLayer statt UILabel zu verwenden.

Objective-C:

CGRect textRect = {0, 100, self.view.frame.size.width, 100}; // rect to display the view in 

CATextLayer* textMask = [CATextLayer layer]; 
textMask.contentsScale = [UIScreen mainScreen].scale; // sets the layer's scale to the main screen scale 
textMask.frame = (CGRect){CGPointZero, textRect.size}; 
textMask.foregroundColor = [UIColor whiteColor].CGColor; // an opaque color so that the mask covers the text 
textMask.string = @"Text Mask"; // your text here 
textMask.font = (__bridge CFTypeRef _Nullable)([UIFont systemFontOfSize:30]); // your font here 
textMask.alignmentMode = kCAAlignmentCenter; // centered text 

UIView* view = [[UIView alloc] initWithFrame:textRect]; 
view.backgroundColor = [UIColor blueColor]; 
view.layer.mask = textMask; // mask the view to the textMask 
[self.view addSubview:view]; 

Swift:

let textRect = CGRect(x: 0, y: 100, width: view.frame.size.width, height: 100) // rect to display the view in 

let textMask = CATextLayer() 
textMask.contentsScale = UIScreen.mainScreen().scale // sets the layer's scale to the main screen scale 
textMask.frame = CGRect(origin: CGPointZero, size: textRect.size) 
textMask.foregroundColor = UIColor.whiteColor().CGColor // an opaque color so that the mask covers the text 
textMask.string = "Text Mask" // your text here 
textMask.font = UIFont.systemFontOfSize(30) // your font here 
textMask.alignmentMode = kCAAlignmentCenter // centered text 

let bgView = UIView(frame: textRect) 
bgView.backgroundColor = UIColor.blueColor() 
bgView.layer.mask = textMask // mask the view to the textMask 
view.addSubview(bgView) 
+0

Es funktioniert, aber kommt ziemlich pixelig raus, nein? –

+1

@JohnnyRockex ja, tut mir leid! Ich habe das vor ein paar Jahren gemacht und vergessen, die 'contentsScale' von' CATextLayer' zu setzen, so dass es auf einem 2x oder 3x Display ziemlich schrecklich rausgekommen wäre. Ich habe das korrigiert. – Hamish