CATextLayer not visible on video in iOS7 - iphone

I'm working on a video app that generates a credits clip using a .mov resource as the background and a CATextLayer for the credits. The code works in iOS 5 and 6, but not in iOS 7 - ie: the clip gets generated but text does not appear.
I've tried using the code from the answer to this question (How can I add overlay text on a video, then re-encode it?), but the text in the CATextLayer still does not show up.
Why is iOS 7 behaving differently and how can I get it to work?

After 2 days on this problem
CATextLayer *text = [CATextLayer layer];
text.string = #"Your Text";
text.frame = CGRectMake(0, 0, 320, 50);
CGFontRef font = CGFontCreateWithFontName((CFStringRef)#"HelveticaNeue-UltraLight");
text.font = font;
text.fontSize = 20;
text.foregroundColor = [UIColor whiteColor].CGColor;
[text display];
[aLayer addSublayer:text];
[aLayer display];
The secret is calling [CALayer display] method

Related

WatchKit: How can I implement a text shadow?

In my Apple Watch app, I'm trying to place text over the top of an image. I've got a group with a background image and label positioned correctly. But depending on the image, my white text can sometimes be unreadable. Dark text would have the same problem or be even worse.
Is there a way to have black text just under my white text and slightly offset?
Unfortunately, the current version of WatchKit doesn't provide a way to "stack" or layer WKInterfaceLabel controls. So, the idea of including black text under the white text using a second label won't work.
You might consider rendering your drop-shadowed text as a UIImage in your Watch extension and using the image with a WKInterfaceImage control. Something like:
- (UIImage *)imageWithText:(NSString *)text shadowBlurRadius:(CGFloat)shadowBlurRadius {
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeZero;
shadow.shadowBlurRadius = shadowBlurRadius;
shadow.shadowColor = [UIColor blackColor];
NSDictionary *attributes = #{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont boldSystemFontOfSize:36.0f],
NSShadowAttributeName : shadow };
CGSize size = [text sizeWithAttributes:attributes];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width + (2.0f * shadowBlurRadius),
size.height + (2.0f * shadowBlurRadius)),
NO,
2.0f);
[text drawAtPoint:CGPointMake(shadowBlurRadius, shadowBlurRadius)
withAttributes:attributes];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

custom font not working on iphone device

I am trying to load an external .ttf font into one of my iOS projects. The font works fine within an emulator, but fails to display on an actual device.
I am using the LLVM GCC 4.2 compiler. In another project, with the Apple LLVM compiler 3.0, the same font works. I do not understand how I can fix it? What are the steps I need to follow with the LLVM GCC 4.2 compiler?
Make sure it's added under 'Targets' -> 'Build Phases' -> 'Copy Bundle Resources'. I had a similar problem and by manually adding it to this list, the font started showing up on the device.
For the Custom font below code help
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 240, 40)];
[label1 setFont: [UIFont fontWithName: #"Grinched" size:24]];
[label1 setText:#"Grinched Font"];
[[self view] addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 240, 40)];
[label2 setFont: [UIFont fontWithName: #"Energon" size:18]];
[label2 setText:#"Energon Font"];
[[self view] addSubview:label2];
Also u can download sample code and tutorial here.
You have a limited choice regarding fonts, so you have to choose between the fonts available...
This answer is useful as a reference:
What fonts do iPhone applications support?
First load the font like below:
(void)loadFont{ // Get the path to our custom font and create a data provider.
NSString *fontPath = [[NSBundle mainBundle] pathForResource:#"mycustomfont" ofType:#"ttf"];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
// Create the font with the data provider, then release the data provider. customFont =
CGFontCreateWithDataProvider(fontDataProvider); CGDataProviderRelease(fontDataProvider);
}
Then use them as below:
-(void)drawRect:(CGRect)rect{
[super drawRect:rect]; // Get the context.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect); // Set the customFont to be the font used to draw.
CGContextSetFont(context, customFont);
// Set how the context draws the font, what color, how big.
CGContextSetTextDrawingMode(context, kCGTextFillStroke); CGContextSetFillColorWithColor(context, self.fontColor.CGColor); UIColor * strokeColor = [UIColor blackColor];
CGContextSetStrokeColorWithColor(context, strokeColor.CGColor); CGContextSetFontSize(context, 48.0f);
// Create an array of Glyph's the size of text that will be drawn.
CGGlyph textToPrint[[self.theText length]];
// Loop through the entire length of the text.
for (int i = 0; i < [self.theText length]; ++i) { // Store each letter in a Glyph and subtract the MagicNumber to get appropriate value.
textToPrint[i] = [[self.theText uppercaseString] characterAtIndex:i] + 3 - 32;
}
CGAffineTransform textTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, textTransform);
CGContextShowGlyphsAtPoint(context, 20, 50, textToPrint, [self.theText length]);
}
There are some cases where you may not able to use some of the internet downloaded font. Here is the reference for the same:

UIImageView Border Problem

I'm adding a border to a UIImageView subclass of mine which works great:
self.layer.borderColor = [UIColor whiteColor].CGColor;
self.layer.borderWidth = 10;
Upon adding a UIImageView to the top of the image however, I noticed that the border is covering the image up!
UIImage*pin = [UIImage imageNamed:#"PinDown2"];
UIImageView*view = [[UIImageView alloc] initWithImage:pin];
view.frame = CGRectMake(self.bounds.size.width/2, 0, 32, 39);
[self addSubview:view];
How can I fix this?
If you don't find any alternative, try this,
Link 1
or this,
Link 2
Both are from same post. You can also find other answers there.

iPhone UILabel text soft shadow

I know soft shadows are not supported by the UILabel out of the box, on the iPhone. So what would be the best way to implement my own one?
EDIT:
Obviously I will subclass the UILabel and draw in the -drawRect:
My question is, how do I get the contents of the label as graphics and draw around them, blur them etc...
EDIT 2:
I returned to this question about a year later. In the meantime I've built a class that allows you to easily add soft shadow to a label and tweak it's radius etc and also to draw gradients on the text itself. You can find it on GitHub: https://github.com/doukasd/iOS-Components/tree/master/Views
As of 3.2 there is direct support for shadows in the SDK.
label.layer.shadowColor = [label.textColor CGColor];
label.layer.shadowOffset = CGSizeMake(0.0, 0.0);
#import <QuartzCore/QuartzCore.h> and play with some parameters:
label.layer.shadowRadius = 3.0;
label.layer.shadowOpacity = 0.5;
And, if you find your shadow clipped by the label bounds:
label.layer.masksToBounds = NO;
finally set
label.layer.shouldRasterize = YES;
I advise you to use the shadowColor and shadowOffset properties of UILabel:
UILabel* label = [[UILabel alloc] init];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0,1);
This answer to this similar question provides code for drawing a blurred shadow behind a UILabel. The author uses CGContextSetShadow() to generate the shadow for the drawn text.
Additionally to IIDan's answer:
For some purposes it is necessary to set
label.layer.shouldRasterize = YES
I think this is due to the blend mode that is used to render the shadow. For example I had a dark background and white text on it and wanted to "highlight" the text using a black shadowy glow. It wasn't working until I set this property.
Apply the (soft) shadow on the view's layer, like this:
UILabel *label = [[UIabel alloc] init];
label.layer.shadowColor = [[UIColor whiteColor] CGColor];
label.layer.shadowOpacity = 1.0;
To keep things up to date: Creating the shadow in Swift is as easy as that:
Import the QuartzCore Framework
import QuartzCore
And set the shadow attributes to your label
titleLabel.shadowColor = UIColor.blackColor()
titleLabel.shadowOffset = CGSizeMake(0.0, 0.0)
titleLabel.layer.shadowRadius = 5.0
titleLabel.layer.shadowOpacity = 0.8
titleLabel.layer.masksToBounds = false
titleLabel.layer.shouldRasterize = true
_nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_nameLabel.font = [UIFont boldSystemFontOfSize:19.0f];
_nameLabel.textColor = [UIColor whiteColor];
_nameLabel.backgroundColor = [UIColor clearColor];
_nameLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.2];
_nameLabel.shadowOffset = CGSizeMake(0, 1);
i think you should use the [UIColor colorWithWhite:0 alpha:0.2] to set the alpha value.
I tried almost all of these techniques (except FXLabel) and couldn't get any of them to work with iOS 7. I did eventually find THLabel which is working perfectly for me. I used THLabel in Interface Builder and setup User Defined Runtime Attributes so that it's easy for a non programmer to control the look and feel.
https://github.com/MuscleRumble/THLabel
This like a trick,
UILabel *customLabel = [[UILabel alloc] init];
UIColor *color = [UIColor blueColor];
customLabel.layer.shadowColor = [color CGColor];
customLabel.layer.shadowRadius = 5.0f;
customLabel.layer.shadowOpacity = 1;
customLabel.layer.shadowOffset = CGSizeZero;
customLabel.layer.masksToBounds = NO;
I wrote a library that provides a UILabel subclass with soft shadow support and a bunch of other effects:
https://github.com/nicklockwood/FXLabel
In Swift 3, you can create an extension:
import UIKit
extension UILabel {
func shadow() {
self.layer.shadowColor = self.textColor.cgColor
self.layer.shadowOffset = CGSize.zero
self.layer.shadowRadius = 3.0
self.layer.shadowOpacity = 0.5
self.layer.masksToBounds = false
self.layer.shouldRasterize = true
}
}
and use it via:
label.shadow()
Subclass UILabel, as stated, then, in drawRect:, do [self drawTextInRect:rect]; to get the text drawn into the current context. Once it is in there, you can start working with it by adding filters and whatnot. If you want to make a drop shadow with what you just drew into the context, you should be able to use:
CGContextSetShadowWithColor()
Look that function up in the docs to learn how to use it.
As of iOS 5 Apple provides a private api method to create labels with soft shadows.
The labels are very fast: I'm using dozens at the same time in a series of transparent views and there is no slowdown in scrolling animation.
This is only useful for non-App Store apps (obviously) and you need the header file.
$SBBulletinBlurredShadowLabel = NSClassFromString("SBBulletinBlurredShadowLabel");
CGRect frame = CGRectZero;
SBBulletinBlurredShadowLabel *label = [[[$SBBulletinBlurredShadowLabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont boldSystemFontOfSize:12];
label.text = #"I am a label with a soft shadow!";
[label sizeToFit];
While it's impossible to set a blur radius directly on UILabel, you definitely could change it by manipulating CALayer.
Just set:
//Required properties
customLabel.layer.shadowRadius = 5.0 //set shadow radius to your desired value.
customLabel.layer.shadowOpacity = 1.0 //Choose an opacity. Make sure it's visible (default is 0.0)
//Other options
customLabel.layer.shadowOffset = CGSize(width: 10, height: 10)
customLabel.layer.shadowColor = UIColor.black.cgColor
customLabel.layer.masksToBounds = false
What I hope will help someone and other answers failed to clarify is that it will not work if you also set UILabel Shadow Color property directly on Interface Builder while trying to setup .layer.shadowRadius.
So if setting label.layer.shadowRadius didn't work, please verify Shadow Color for this UILabel on Interface Builder. It should be set to default. And then, please, if you want a shadow color other than black, set this color also through .layer property.
Subclass UILabel, and override -drawInRect:

IPhone Text Glow Effect

In my IPhone application, I want the text in UILabel to glow for a second, then fade for a sec;. Also i want to repeat this cycle for say 3 or 4 times.
Is this possible?
As of 3.2 you there is direct support for shadows in the SDK.
label.layer.shadowColor = [label.textColor CGColor];
label.layer.shadowOffset = CGSizeMake(0.0, 0.0);
Play with the parameters:
label.layer.shadowRadius = 3.0;
label.layer.shadowOpacity = 0.5;
And to avoid shadow being clipped by the label bouds:
label.layer.masksToBounds = NO;
Don't forget to
#include <Quartzcore/Quartzcore.h>
and link against the QuartzCore or CoreGraphics frameworks (thanks to commenters for pointing this out).
I've posted some sample code which subclasses UILabel and enables you to apply glow and soft shadows to text.
http://www.redrobotstudios.com/blog/2010/04/29/create-glow-soft-shadow-text-on-iphone/
Yes. Use beginAnimation...commitAnimation, and use the alpha value to brighten or dim the ULabel. Make sure that the default value of the UILabel's alpha starts at 0.85 and brightens to 1.0 and then dims to 0.75, and when all is done, you go back to 0.85.
There are other ways to do it such as having another view on top of the label that is gray or black and you use the same begin...commitAnimation to change the alpha on that from 0 to 0.20 or so.
There are many ways to do this, with varying quality. One way would be to subclass UILabel, and implement some kind of gradient effect in coregraphics in the drawRect method.
You can also play with the text shadow (change the colour and alpha) and see if you can come up with a decent glow.
The easiest way is probably to make a transparent glow-outline image in photoshop and place it behind your text, and then do like mahboudz suggests... fade the image in and out using coreanimation.
- (UILabel *) setUpGlowLabelWithFrame: (CGRect) frame fontSize: (int)fontSize {
UILabel* label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:fontSize];
label.textColor = [UIColor whiteColor];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
label.textAlignment = UITextAlignmentCenter;
label.layer.shadowColor = [label.textColor CGColor];
label.layer.shadowOffset = CGSizeMake(0.0, 0.0);
label.layer.masksToBounds = NO;
label.layer.shadowRadius = 0.5f;
label.layer.shadowOpacity = 0.95;
label.numberOfLines = 2;
label.tag = 20;
return label;
}
I get the glow effect when using this.
Hope it helps.
Happy coding :)
For those of you using Swift 4, here's what I used for multiple objects to Glow the same color as they are:
let colorRed: UIColor? = timeLabel.textColor
timeLabel.layer.shadowColor = colorRed?.cgColor
timeLabel.layer.shadowRadius = 4.0
timeLabel.layer.shadowOpacity = 0.9
timeLabel.layer.shadowOffset = CGSize.zero
timeLabel.layer.masksToBounds = false
As for animating the glow, just add a timer for 3-4 loops and change .shadowOpacity to something lower.