Prevent iOS SIGABRT when calling 'UIColor colorWithPatternImage' - iphone

I am writing an iPad application that has a label with a gradient image background. This has worked in 4.2 but it is working no longer. Every time I try to run I get a SIGABRT whether I am in 4.2 or 4.3. I will put the offending code below, I would appreciate any help. Thanks.
LabelSingAvg = [[UILabel alloc] initWithFrame:CGRectMake(70, -300, 200, 800)];
LabelSingAvg.text = #"99.8";
LabelSingAvg.font = [UIFont systemFontOfSize:90];
UIImage *myGradient = [UIImage imageNamed:#"textGradient.png"];
//Line that causes SIGABRT below:
LabelSingAvg.textColor = [UIColor colorWithPatternImage:myGradient];
LabelSingAvg.backgroundColor = [UIColor clearColor];
LabelSingAvg.textAlignment = UITextAlignmentCenter;
LabelSingAvg.adjustsFontSizeToFitWidth = YES;
LabelSingAvg.layer.shadowOpacity = .5;
LabelSingAvg.layer.shadowRadius = 4.0;
LabelSingAvg.layer.shadowColor = [UIColor blackColor].CGColor;
LabelSingAvg.layer.shadowOffset = CGSizeMake(1.0, 1.0);
//LabelSingAvg.alpha = 0.8;
[self.view addSubview:LabelSingAvg];

Nevermind, I got it. Thanks for your help. The image I was referencing got moved out of my resources folder.

Related

How to decrease the UIFont size and show large sentence one line UILabel iPhone?

I have an UILabel in my iPhone app. I want to show large sentence in UILabel in a single line. That means I want to decrease the font size and show the full text in a visible area. I don't want to show it in multiple lines. Can anyone please help me to do this?
My code is:
textViewLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 5, 194, 20)];
textViewLabel.text = #"I have an UILabel in my iPhone app. I have want";
textViewLabel.textColor = [UIColor lightGrayColor];
textViewLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
textViewLabel.numberOfLines = 0;
textViewLabel.backgroundColor = [UIColor clearColor];
CGSize maximumLabelsize2 = CGSizeMake(194,20);
CGSize expectedLabelsize2 = [result sizeWithFont:textViewLabel.font constrainedToSize:maximumLabelsize2 lineBreakMode:textViewLabel.lineBreakMode];
CGRect messagesFrame = textViewLabel.frame;
messagesFrame.size.height = expectedLabelsize2.height;
textViewLabel.frame = messagesFrame;
Any one please help me to do this? Thanks in advance.
change numberOfLines property from
textViewLabel.numberOfLines = 0; // 0 means as many lines as needed
to
textViewLabel.numberOfLines = 1;
add
textViewLabel.adjustsFontSizeToFitWidth = YES;
Conside setting
textViewLabel.minimumFontSize = someValue; // default is 0.0
Try this property:
myLabel.adjustsFontSizeToFitWidth = YES;
try this :
[textViewLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

Jerky scroll with UIButton.layer.shadowColor

I got a UITableView header with lots of buttons.
I use this to create a shadow on the buttons:
Buttona.layer.shadowColor = [UIColor blackColor].CGColor;
Buttona.layer.shadowOpacity = 0.7f;
Buttona.layer.shadowOffset = CGSizeMake(10.0f, 10.0f);
Buttona.layer.shadowRadius = 7.0f;
Buttona.layer.masksToBounds = NO;
Buttonb.layer.shadowColor = [UIColor blackColor].CGColor;
Buttonb.layer.shadowOpacity = 0.7f;
Buttonb.layer.shadowOffset = CGSizeMake(2.0f, 3.0f);
Buttonb.layer.shadowRadius = 2.0f;
Buttonb.layer.masksToBounds = NO;
... and so on for Buttonc to Buttonf.
The scroll on the table becomes jerky with all theses buttons.
Should I get rid of the effect and just Photoshop the buttons with the shadow, or am I doing something wrong here?
Thanks in advance.
I had the same problem and the solution is
[view.layer setShadowPath:[[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)] CGPath]];

TTStyledTextLabel text alignment not works when transform rotation is used

I am having a problem with Three20's TTSTyledTextLabel. i have specified the view like this
//iphone properties
htmlSubtitle_ = [[TTStyledTextLabel alloc] init];
htmlSubtitle_.font = [UIFont systemFontOfSize:14];
htmlSubtitle_.contentInset = UIEdgeInsetsMake(2, 2, 2, 2);
htmlSubtitle_.backgroundColor = [UIColor clearColor];
htmlSubtitle_.textColor = [UIColor whiteColor];
htmlSubtitle_.textAlignment = UITextAlignmentCenter;
htmlSubtitle_.userInteractionEnabled = NO;
htmlSubtitle_.text = [TTStyledText textFromXHTML:#"<p><p>I dont know where this\n is going<br/> but it is ok :D</p></p>" lineBreaks:YES URLs:YES];;
htmlSubtitle_.frame = CGRectMake(0, 390, 200, 90);
htmlSubtitle_.backgroundColor = [UIColor greenColor];
[self.view addSubview:htmlSubtitle_];
it looks like this :
And when i rotate the screen i have written this code :
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
htmlSubtitle_.frame = CGRectMake(0, 0, 90, 200);
htmlSubtitle_.transform = CGAffineTransformMakeRotation(M_PI / 2);
htmlSubtitle_.backgroundColor = [UIColor greenColor];
}
and it looks like this :
see the text alignment, the alignment has been changed to left instead of remaining in center. is somebody else having this problem? or am i doing something wrong?
I have used UIWebView to show the HTML Styled Text instead of using this TTStyledLabel crap ... Thanx everybody.

UILabel Letters being cut off

I am trying to use a special character in the UILabel. But it is chopping off the labels. I put in a red background to show where the edge of the label is. Any reason why this may be happening? I have maskToBounds and clipToBounds set to NO;
CGSize lblSize = [FontHelper sizeForText:text withFont:[self getBodyFont] withWidth:kColumnWidth andNumberOfLines:1000];
UILabel*lbl = [[UILabel alloc] initWithFrame:CGRectMake(x,
y + body.frame.size.height - columnHeight - kBottomMargin,
kColumnWidth,
lblSize.height)];
lbl.clipsToBounds = NO;
lbl.layer.masksToBounds = NO;
lbl.text = text;
lbl.backgroundColor = [UIColor redColor];
lbl.font = [self getBodyFont];
lbl.textColor = [UIColor blackColor];
lbl.numberOfLines = 0;
lbl.textAlignment = UITextAlignmentLeft;
lbl.lineBreakMode = UILineBreakModeWordWrap;
I think I found the solution. The client had give me the above text in a Word Doc and I had copy and pasted into the file. The space between the dot and the text appeared to be a space but was other special character. So copy/paste flared its ugly head.
For anyone else who stumbles on this question, I had the same problem and in my case there were a few tab characters in the UILabel, which caused the same effect.

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: