Ham*_*ish 8 cocoa-touch calayer uiview uilabel ios
我试图将添加UIView到UILabel,从而使文本视图的面具,使我能够做的事情一样的文字动画背景(很像滑动解锁在锁定屏幕上的标签).
我计划这样做的方法是使用mask视图上的属性将layer其屏蔽为文本的形状.但是,我无法找到一种方法来获得UILabel文本形状CALayer.
这甚至可能吗?我只能找到覆盖该-(void)drawRect:方法的解决方案UILabel,但这不会给我很大的灵活性.
mop*_*led 10
UIViewmaskView在iOS 8.0中添加了该属性.现在,只需创建一个UILabel用作a的掩码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];
Run Code Online (Sandbox Code Playgroud)
斯威夫特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)
Run Code Online (Sandbox Code Playgroud)
这样就形成了一种清爽UILabel的UIColor.blueColor()颜色overlayView.
如果你为iOS 8+构建,mopsled的解决方案会更灵活.但是,如果您正在寻找iOS 8之前的答案,那么就是这样.
感谢Linuxios指点我这个问题.关键是要用CATextLayer而不是UILabel.
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];
Run Code Online (Sandbox Code Playgroud)
迅速:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7854 次 |
| 最近记录: |