highlighted shadow color - iphone

How to set the shadowColor of a highlighted Text?
titleLabel.textColor = [UIColor colorWithRed:20.0f/255.0f green:50.0f/255.0f blue:90.0f/255.0f alpha:1.0f];
titleLabel.highlightedTextColor = [UIColor whiteColor];
titleLabel.shadowColor = [UIColor whiteColor];
titleLabel.shadowOffset = CGSizeMake(0, 1.0);
I'm looking for something like: titleLabel.highlightedTextColor.shadowColor = [UIColor blackColor];

I was annoyed by this oversight from Apple, too, and created a subclass of UILabel that adds the missing highlightedShadowColor and highlightedShadowOffset properties.
By default it just disables the shadow in highlighted mode and sets the offset to the reverse of the standard offset.
#interface FTLabel : UILabel
#property(nonatomic, strong) UIColor *highlightedShadowColor;
#property(nonatomic) CGSize highlightedShadowOffset;
#end
#interface FTLabel ()
#property(nonatomic, strong) UIColor *savedShadowColor;
#property(nonatomic) CGSize savedShadowOffset;
#end
#implementation FTLabel
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self configure];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self configure];
}
return self;
}
- (void)configure {
self.highlightedShadowColor = nil;
self.highlightedShadowOffset = CGSizeMake(self.shadowOffset.width * (-1), self.shadowOffset.height * (-1));
self.savedShadowColor = self.shadowColor;
self.savedShadowOffset = self.shadowOffset;
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
self.shadowColor = self.highlighted ? self.highlightedShadowColor : self.savedShadowColor;
self.shadowOffset = self.highlighted ? self.highlightedShadowOffset : self.savedShadowOffset;
}
#end

This should work:
if(!yourlabel.highlighed)
{
[yourLabel setShadowColor:[UIColor greenColor]];
}
else
{
[yourLabel setShadowColor:[UIColor blueColor]];
}
EDIT:
A UIlabel has a property called highlighted and is a BOOL. You can check if it's true or not and change the shadow color at the moment you want it done.

I had the same issue.
What I did in the end is to create a second UILabel underneath the primary UILabel to act as a shadow.
Don't set shadows on your primary and shadow labels. For the shadow label, set the 'Normal Color' to what you wanted your shadow color to be and set the highlighted color to 'Clear Color'.
Obviously you have to update the shadow label each time you update the primary label. Not a big price to pay in many cases.
Hope that helps!

Related

Label not showing up in overlay UIView

Ok, I have done this kind of thing a million times but this particular case escapes me and I'm sure its just something stupid I am missing...
But I am trying to display a UIView on top of a mapView to give the impression the mapview is 'disabled.'
I created a UIView subclass and added a single label. like so...
.h
#import <UIKit/UIKit.h>
#interface DisabledOverlayView : UIView
#property (nonatomic, strong) UILabel *disabledOverlayViewText;
#end
nothing complicated...
in my .m I set the text and add it to my UIView.
#synthesize disabledOverlayViewText = _disabledOverlayViewText;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
_disabledOverlayViewText.textColor = [UIColor whiteColor];
_disabledOverlayViewText.frame = CGRectMake(0, 0, 200, 200);
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self addSubview:_disabledOverlayViewText];
}
return self;
}
then the class i wish to use this overlay i call it with...
DisabledOverlayView *disabledView = [[DisabledOverlayView alloc] initWithFrame:CGRectMake(12, 12, _mapView.frame.size.width, _mapView.frame.size.height)];
disabledView.disabledOverlayViewText.text = #"No Location";
[self.view addSubview:disabledView];
None of this is too complicated.. but the label doesn't show up. the overlay appears just fine, but no label text. Ive messed with the frames of the label thinking maybe it was off the visible view but no dice. and i cannot figure out what is going on.
Replace your initWithFrame method with the below code. I have added the alloc init line and changes the background color of the label. Try it and see whether it works :)
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
_disabledOverlayViewText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
_disabledOverlayViewText.textColor = [UIColor whiteColor];
_disabledOverlayViewText.backgroundColor = [UIColor blackColor];
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self.view addSubview:_disabledOverlayViewText];
}
It seems the label is not allocated and it's nil.
Try this
(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
self.disabledOverlayViewText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
_disabledOverlayViewText.textColor = [UIColor whiteColor];
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self addSubview:_disabledOverlayViewText];
}

UILabel with fixed width and flexible height

I've got an UILabel in a Detail View Controller, so its content changes depending on the selected table row. I have a problem, I would set a fixed width for my UILabel and a dynamic height depending on the text. How can I do this? (I'm sorry for my mistakes, but I'm not English)
I like to subclass UILabel to do this for me.
AutosizingLabel.h
#import <UIKit/UIKit.h>
#interface AutosizingLabel : UILabel {
double minHeight;
}
#property (nonatomic) double minHeight;
- (void)calculateSize;
#end
AutosizingLabel.m
#define MIN_HEIGHT 10.0f
#import "AutosizingLabel.h"
#implementation AutosizingLabel
#synthesize minHeight;
- (id)init {
if ([super init]) {
self.minHeight = MIN_HEIGHT;
}
return self;
}
- (void)calculateSize {
CGSize constraint = CGSizeMake(self.frame.size.width, 20000.0f);
CGSize size = [self.text sizeWithFont:self.font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[self setLineBreakMode:UILineBreakModeWordWrap];
[self setAdjustsFontSizeToFitWidth:NO];
[self setNumberOfLines:0];
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, MAX(size.height, MIN_HEIGHT))];
}
- (void)setText:(NSString *)text {
[super setText:text];
[self calculateSize];
}
- (void)setFont:(UIFont *)font {
[super setFont:font];
[self calculateSize];
}
#end
To use this, import/create the .h and .m files in your project. Then if you are creating your UILabel in code, it would look something like this:
#import "AutosizingLabel.h"
- (void)viewDidLoad {
[super viewDidLoad];
AutosizingLabel *label = [[AutosizingLabel alloc] init];
label.text = #"Some text here";
[self.view addSubview:label];
}
If you're using a XIB, you can select any UILabel and click on the Identity Inspector in the right sidebar to set it's class to AutosizingLabel. In either case, setting the .text property will auto update the size of the label.
You can do it ..here is code.
UILabel *yourlabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 100, sizeToFit)];
yourlabel.numberOfLines = 0;
For Any Query please comment.

Border around UITextView is not showing up

I am trying to add border around UITextView by using
self.textView.layer.borderWidth = 1;
self.textView.layer.borderColor = [[UIColor brownColor] CGColor];
but it is not showing up. If anyone can help me with why it is not showing up. Eventhough i have added Quartz framework. But still is not showing up.
#import "uitextviewViewController.h"
#import <QuartzCore/QuartzCore.h>
#implementation uitextviewViewController
#synthesize textView;
#synthesize navBar;
- (void)dealloc {
[navBar release];
[textView release];
[super dealloc];
}
- (void) viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem * button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(done:)];
[[self navigationItem] setRightBarButtonItem:button];
[button release];
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor whiteColor];
self.textView.font = [UIFont fontWithName:#"Arial" size:15];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor brownColor];
self.textView.layer.borderWidth = 1;
self.textView.layer.borderColor = [[UIColor brownColor] CGColor];
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"This is UITextView\n\nThis is UITextView\n\nThis is UITextView\n\nThis is UITextView\n\nThis is UITextView.";
self.textView.scrollEnabled = YES;
[self.view addSubview: self.textView];
}
I will appreciate help.
Thanks in advance.
Following code is use for the give the arc of the UITextbox or other controller for that you need to use the #import frame work then you can use the following code for the border or arc shape on the control.
imgThumb.layer.cornerRadius = 5;
imgThumb.layer.masksToBounds = YES;
imgThumb.layer.borderColor = [UIColor grayColor].CGColor;
imgThumb.layer.borderWidth = 0.9;
If any query regarding this please comment here...
Happy coding
Here, use this.
Modify it as you'd like.
UITextFieldWrapper.h
#import <UIKit/UIKit.h>
#interface UITextFieldWrapper : UIView
#property (nonatomic, weak, readonly) UITextField * textField;
#end
UITextFieldWrapper.m
#import "UITextFieldWrapper.h"
#import <QuartzCore/QuartzCore.h>
#implementation UITextFieldWrapper
#synthesize textField = _textField;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.layer.borderWidth = 1.0;
self.layer.borderColor = [[UIColor blackColor] CGColor];
self.backgroundColor = [UIColor whiteColor];
self.clipsToBounds = YES;
UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(3, 3, frame.size.width - 6, 21)];
textField.backgroundColor = [UIColor whiteColor];
[self addSubview:textField];
_textField = textField;
}
return self;
}
#end

How to achieve background Image in UILabel as in pic?

How to achieve below effect??
I want that when my UIImage width is smaller then UILabel width then my image should be displayed only once as below.
I already have done some more work with UILabel and UIImage. Refer to my previous question : How to stretch Image to fill the Label Width set in Background in UILabel?.
But now i want some more fun with UILabel... :D
EDIT :
SBLabel *lbl = [[SBLabel alloc] initWithFrame:CGRectMake(0, 50, 320, 28)];
lbl.text = #"Hello World!!!...";
UIImage *img = [UIImage imageNamed:#"cn3.png"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320, 28)];
imgView.image = img;
[lbl setNonRepeatingBackgroundImage:imgView];
[self.view addSubview:lbl];
[imgView release];
if (image.size.width < label.size.width ||image.size.height < label.size.height )
{
//rect here is frame of image
[img drawInRect:rect];
label.backgroudColor = [UIColor clearColor];
}
It seems like the most straightforward solution would be to have your UIImage sit behind your UILabel and your UILabel has a transparent background color.
Edit
Assuming you're using ARC...
SBLabel.h
#import <UIKit/UIKit.h>
#interface SBLabel : UILabel
#property (nonatomic, strong) UIImageView *nonRepeatingBackgroundImage;
#end
SBLabel.m
#import "SBLabel.h"
#implementation SBLabel
#synthesize nonRepeatingBackgroundImage;
- (void)setNonRepeatingBackgroundImage:(UIImageView *)aNonRepeatingBackgroundImage {
nonRepeatingBackgroundImage = aNonRepeatingBackgroundImage;
[self addSubview:aNonRepeatingBackgroundImage];
[self sendSubviewToBack:aNonRepeatingBackgroundImage];
[self setBackgroundColor:[UIColor clearColor]];
}
#end
You would need to call this setter method after you add the UILabel to the parent view. I have not tested but I believe it should work, might require some tweaks but hopefully it explains how to Subclass UILabel to make a custom enhancement such as this.
A subclass of UILabel could look like this (without ARC)
CustomLabel.h
#import <UIKit/UIKit.h>
#interface CustomLabel : UILabel {
UIImage *mImage;
}
#property (retain) UIImage *image;
#end
CustomLabel.m
#import "CustomLabel.h"
#implementation CustomLabel
- (void)dealloc {
self.image = nil;
[super dealloc];
}
- (void)drawRect:(CGRect)rect {
[mImage drawAtPoint:CGPointMake(0, 0)];
[super drawRect:rect];
}
- (UIImage*)image {
return mImage;
}
- (void)setImage:(UIImage *)image {
self.backgroundColor = [UIColor clearColor];
if(mImage)
[mImage release];
mImage = [image retain];
[self setNeedsDisplay];
}
#end
Usage
yourCustomLabel.image = [UIImage imageNamed:#"test.png"];

why does this custom UIView code not display the UILabel's?

I'm kinda stuck trying to see why, when I use this custom UIView to show a row of 3 x UILabels, that whilst I see the outline in RED for the frame, I don't see anything inside the frame. That is the labels don't appear, nor does the black border for the UILabels which I've set.
Can anyone spot an issue here?
#import <UIKit/UIKit.h>
#interface LabelRowView : UIView {
UILabel *_dateLabel;
UILabel *_titleLabel;
UILabel *_locationLabel;
}
#property (nonatomic, retain) UILabel *dateLabel;
#property (nonatomic, retain) UILabel *titleLabel;
#property (nonatomic, retain) UILabel *locationLabel;
- (void)setFont:(UIFont *)newFont;
- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr;
#end
// ==============================================================
#implementation LabelRowView
#synthesize dateLabel = _dateLabel;
#synthesize titleLabel = _titleLabel;
#synthesize locationLabel = _locationLabel;
- (void)setDefaultsForLabel:(UILabel*)label {
label.layer.borderColor = [UIColor blackColor].CGColor;
label.layer.borderWidth = 1.0;
label.textAlignment = UITextAlignmentLeft;
label.lineBreakMode = UILineBreakModeTailTruncation;
label.opaque = YES; // TODO: put back in for performance reasons if possible
label.backgroundColor = [UIColor clearColor];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.dateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
self.titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
self.locationLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
// Set Defaults
[self setDefaultsForLabel:self.dateLabel];
[self setDefaultsForLabel:self.titleLabel];
[self setDefaultsForLabel:self.locationLabel];
self.layer.borderColor = [UIColor redColor].CGColor;
self.layer.borderWidth = 1.0;
}
return self;
}
- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr {
self.dateLabel.text = dateStr;
self.titleLabel.text = titleStr;
self.locationLabel.text = locationStr;
}
- (void)setFont:(UIFont *)newFont {
self.dateLabel.font = newFont;
self.titleLabel.font = newFont;
self.locationLabel.font = newFont;
}
- (void)layoutSubviews {
[super layoutSubviews];
// Get current width for this subview
CGFloat contentViewWidth = self.frame.size.width;
CGSize maximumLabelSize = CGSizeMake(contentViewWidth, 9999);
CGFloat oneLineHeight = [#"A" sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].height;
// Calculate Widths
CGFloat dateColumnWidth = [#"Ddd 12:22 " sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].width;
CGFloat titleColumnWidth = (contentViewWidth - dateColumnWidth) * 2.0/3.0;
CGFloat locationColumnWidth = contentViewWidth - dateColumnWidth - titleColumnWidth;
// Date - Layout
CGRect newRect = CGRectMake(0.0, 0.0, dateColumnWidth, oneLineHeight);
self.dateLabel.frame = newRect;
// Date - Layout
newRect = CGRectMake(dateColumnWidth, 0.0, titleColumnWidth, oneLineHeight);
self.titleLabel.frame = newRect;
// Title - Layout
newRect = CGRectMake(dateColumnWidth + titleColumnWidth, 0.0, locationColumnWidth, oneLineHeight);
self.locationLabel.frame = newRect;
}
- (void)dealloc
{
[super dealloc];
}
#end
You don't seem to be adding the UILabels as subviews.