UITextView Shadow Not Working - objective-c

I am trying to get a UITextView's layer's shadow to work in a UITableView header. I have a UIView that I am formatting everything in, and then setting the headerview equal to it.
UIView * view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.tableView.frame.size.width, 450);
UIColor * baseColor = [[UIColor alloc] initWithRed: 45/255.0
green: 100/255.0
blue: 150/255.0
alpha: 1.0];
view.backgroundColor = baseColor;
...
UITextView * newCommentField = [[UITextView alloc] initWithFrame:CGRectMake(20, 230, 270, 120)];
newCommentField.text = #"New Comment";
newCommentField.tag = 3;
newCommentField.layer.shadowOffset = CGSizeMake(0, 3);
newCommentField.layer.shadowRadius = 5.0;
newCommentField.layer.shadowColor = [UIColor blackColor].CGColor;
newCommentField.layer.shadowOpacity = 0.8;
[view addSubview:newCommentField];
...
self.tableView.tableHeaderView = view;
Everything shows up properly in the view. However, the shadow is not appearing. I don't know what is going wrong here. I have even tried modifying the layer's frame and making it the size of the comment field, bigger and the same size.

You're not setting background color for newCommentField.
Try:
newCommentField.backgroundColor = [UIColor whiteColor];

You can do it this way also... Create a view (eg. bgView) of same size as your textView and add that textView as a subView of bgView, which will be added as subView of you header view. Apply the shadow effect to the bgView instead.
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(20, 230, 270, 120)];
bgView.backgroundColor=[UIColor whiteColor];
bgView.layer.shadowOffset = CGSizeMake(0, 3);
bgView.layer.shadowRadius = 5.0;
bgView.layer.shadowColor = [UIColor blackColor].CGColor;
bgView.layer.shadowOpacity = 0.8;
[bgView addSubview:newCommentField];
[view addSubview:bgView];

Related

Add padding into UIlabel text?

I've a label in a table cell and I wish to add padding to top,bottom,left and right.
CGRect initialFrame = CGRectMake(10,10,100,20);
UIEdgeInsets contentInsets = UIEdgeInsetsMake(5, 0, 5, 0);
CGRect padd = UIEdgeInsetsInsetRect(initialFrame, contentInsets);
self.rewardLabel = [[UILabel alloc] initWithFrame:padd];
self.rewardLabel.backgroundColor =[UIColor colorWithRed:0.192 green:0.373 blue:0.561 alpha:1];
self.rewardLabel.layer.cornerRadius = 5.0f;
self.rewardLabel.layer.masksToBounds = YES;
self.rewardLabel.textColor = [UIColor whiteColor];
self.rewardLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.rewardLabel.numberOfLines = 1;
self.rewardLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
[self.contentView addSubview:self.rewardLabel];
But it seem like not working. Can anyone tell me how to do?
There are several ways on how to achieve this:
If you do not need a specific background color for your label you could just adjust the labels frame to add the padding (e.g. if your text should start 20px from the cell's left side, set the label's frame's x to 20).
To really add a padding, you could use a custom UILabel subclass and override its drawTextInRect: and intrinsicContentSize methods. (See this question for details)
If you just need a left and right padding you could use an NSAttributedString to add insets to UILabel:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = 5.0;
paragraphStyle.firstLineHeadIndent = 5.0;
paragraphStyle.tailIndent = -5.0;
NSDictionary *attrsDictionary = #{NSParagraphStyleAttributeName: paragraphStyle};
label.attributedText = [[NSAttributedString alloc] initWithString:#"Your text" attributes:attrsDictionary];

How to disable vertical scrolling in UIScrollView (Obj-C)

I want to disable vertical scrolling from my UIScrollView if possible.. My code is like below.. Working fine except users can scroll up and down which shouldn't be there I believe.. Thanks in advance..
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height / 3)];
scroll.contentSize = CGSizeMake(scroll.contentSize.width,scroll.frame.size.height);
scroll.pagingEnabled = YES;
scroll.backgroundColor = [UIColor blackColor];
int xVal = 30;
NSInteger numberOfViews = 5;
for (int i = 0; i < numberOfViews; i++) {
UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 0, 90, 100)];
UILabel *testLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 20, 90, 100)];
UILabel *testLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 40, 90, 100)];
testLabel2.backgroundColor = [UIColor clearColor];
testLabel2.text =#"Test1";
testLabel2.textColor = [UIColor whiteColor];
testLabel2.font = [UIFont boldSystemFontOfSize:12];
testLabel1.backgroundColor = [UIColor clearColor];
testLabel1.text =#"Test2";
testLabel1.textColor = [UIColor whiteColor];
testLabel1.font = [UIFont boldSystemFontOfSize:12];
testLabel3.backgroundColor = [UIColor clearColor];
testLabel3.text =#"Test3";
testLabel3.textColor = [UIColor whiteColor];
testLabel3.font = [UIFont boldSystemFontOfSize:12];
xVal += 120;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(xVal, 30, 150, 130)];
view.backgroundColor = [UIColor blackColor];
xVal += 200;
[scroll addSubview:testLabel1];
[scroll addSubview:testLabel2];
[scroll addSubview:testLabel3];
[scroll addSubview:view];
}
[self.view addSubview:scroll];
In my situation, I was unable to get the height of the scrollview (due to autolayout, I wasn't able to get the height in viewDidLoad). You can add this to the delegate method.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}
you must set your scrollview content height to the scroll view height
CGSize scrollableSize = CGSizeMake(scrollableWidth, yourScrollViewHeight);
[myScrollView setContentSize:scrollableSize];
Here may be a possible duplicate
disabling vertical scrolling in UIScrollView
or you can also try this:
self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * number_of_items, 1);
Assuming it's an iPhone app, so the screen resolution is 320×480 .
Now you are setting your scroll view's height as self.view.frame.size.height / 3 .
Here your view's height is actually taken as 460 and not 480 (20px for staus bar).
So when you add the other view as subview to your scroll view, its frame goes out of the scroll's content view. So you need to manage this while setting your frames/content size.
Let me know if this works for you.
There is no problem with that simply change the contentSize of your UIScrollView and you are done.Increase its width size and its height should be as it is at present.Moreover you can also hide the vertical scrollers also.
scroll.showsVerticalScrollIndicator = NO;
scroll.contentSize = CGSizeMake(scroll.contentSize.width + xVal,scroll.frame.size.height);
You should do like this:
aScrollView.scrollsToTop = NO;
aScrollView.delegate = self;
aScrollView.contentSize = CGSizeMake(aScrollView.frame.size.width * X, aScrollView.frame.size.height/2);
In your xml file there are two properties are available for scrollview are horizontal scroll and vertical scroll. as per your requirement you can check or uncheck and if you want to stop vertical or horizontal scroll then you have to make same content size of scrollview with height or width of scrollview respectively

ios "Data Loading" Notification with transparent background

I am trying to create a view in iOS to let the user know their data is loading... it should have about a 200x200 Rounded-corner box in the middle with a spinner and the words "Data Loading..." and a transparent background.
This all is working except my 200x200 rounded-corner box is also transparent.
Here is the code I am using:
UIView *loadingDataView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 367)];
loadingDataView.alpha = 0.4;
loadingDataView.backgroundColor = [UIColor clearColor];
UIView *viewWithSpinner = [[UIView alloc] initWithFrame:CGRectMake(110, 106, 100, 100)];
[viewWithSpinner.layer setCornerRadius:15.0f];
viewWithSpinner.backgroundColor = [UIColor blackColor];
UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(5, 75, 90, 20)];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5, 5, 90, 70)];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[spinner startAnimating];
msg.text = #"Data Loading";
msg.font = [UIFont systemFontOfSize:14];
msg.textAlignment = UITextAlignmentCenter;
msg.textColor = [UIColor whiteColor];
msg.backgroundColor = [UIColor clearColor];
viewWithSpinner.opaque = NO;
viewWithSpinner.backgroundColor = [UIColor blackColor];
[viewWithSpinner addSubview:spinner];
[viewWithSpinner addSubview:msg];
[loadingDataView addSubview:viewWithSpinner];
[self.view addSubview:loadingDataView];
Thanks.
No exactly an answer to your question, but check out the open source MBProgressHUD. It aims to be an open source replacement for the private UIProgressHUD class, and is exactly what you're trying to do.
The solution for your problem is that you should remove the alpha attribute of 0.4, that's what's turning your round view transparent, if your view is set to clearColor (this is not really a color, it just makes the view transparent) it makes no sense to add an alpha of 0.4. If what you want is a semi-transparent view surrounding your black rounded view, you should do the following:
[loadingDataView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 andAlpha:0.4]];
That will give you something whiteish kinda grayish, that should work.
However, I would recommend you to use the GIDAAlertView Class I developed, you can get the source and an example app on my GitHub:
It takes about 3 lines to get it working:
GIDAAlertView *spinnerAlert=[[GIDAAlertView alloc] initAlertWithSpinnerAndMessage:#"GIDAAlertView Spinner"];
//Show it ...
[spinnerAlert presentAlertWithSpinner];
//Later in your code, hide it
[spinnerAlert hideAlertWithSpinner];
This is how it looks like.
you are telling it to be clear...
loadingDataView.backgroundColor = [UIColor clearColor];
What do you want it to be black?

UINavigationBar styling issue?

I have a UINavigationBar I am having style issues. To start out, I use this code (a class inside my navigation controller) to style my UINavigationBar.
#import...
#implementation UINavigationBar (UINavigationBarCustomDraw)
- (void) drawRect:(CGRect)rect {
[self setTintColor:[UIColor colorWithRed:0.9f green: 0.9f blue:0.9f alpha:1]];
if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:#"Back to ..."]) {
[[UIImage imageNamed:#"UINavigationBar_background.png"] drawInRect:rect];
CGRect frame = CGRectMake(0, 0, 320, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
[label setBackgroundColor:[UIColor clearColor]];
label.font = [UIFont boldSystemFontOfSize: 20.0];
label.shadowColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor colorWithRed:(70.0 / 255.0) green:(70.0 / 255.0) blue:(70.0 / 255.0) alpha: 1];
label.text = self.topItem.title;
self.topItem.titleView = label;
} else {
//[[UIImage imageNamed:#"login_button.png"] drawInRect:rect];
self.topItem.titleView = [[[UIView alloc] init] autorelease];
}
}
#end
#implementation...
Here's my two issues:
When I apply this code in another document with different form and styles, it changes everything. Why is this happening?
When moving from one view to another, the textColor changes to white. It shows great on start up and when I go back in the navigation controller it works fine.
Thanks in advance!
PS: If you need any more code, please ask!

Showing a Title and an Image Into the Table Header

I am trying to show into the header of my UITableViewController a title and under it an image illustrating the title. The next code (written into the viewDidLoad method) shows only the image, and this image over the rest of the table sections. How I can fix it to do what I want?
// Creates a header view.
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)] autorelease];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)] autorelease];
headerLabel.text = self.name;
headerLabel.textColor = [UIColor blackColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.backgroundColor = [UIColor clearColor];
GRect imageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *headerImage = [[UIImageView alloc] initWithFrame:imageRect];
UIImage *image = [UIImage imageNamed:self.imagePath];
[headerImage setImage:image];
headerImage.opaque = YES;
[containerView addSubview:headerLabel];
[containerView addSubview:headerImage];
self.tableView.tableHeaderView = containerView;
Thanks for reading.
Just adjust the views' frames accordingly. The container view's frame must be tall enough to contain the label and image view, and the image view's y coordinate must be set so that it is positioned below the label. The frame rects you are using in your code do not match.