I have a problem .. I used this http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color to create my own custom UIAlertView.
I do not know why but this will not work:
UILabel *theTitle = [theAlert valueForKey:#"_titleLabel"];
[theTitle setTextColor:[UIColor redColor]];
UILabel *theBody = [theAlert valueForKey:#"_bodyTextLabel"];
[theBody setTextColor:[UIColor blueColor]];
the color of the title does not change .. the color of texbody it's ok.
How can I customize the buttons?
Hi Achieved same thing using custom UIAleartView.
Make a custom view as follows.
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 5.0f, 262.0, 49)];
tempView.backgroundColor = [UIColor clearColor];
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 262.0, 49)];
lblTitle.font = [UIFont fontWithName:#"Arial" size:22.0f];
lblTitle.textColor = [UIColor whiteColor];
lblTitle.textAlignment = UITextAlignmentCenter;
lblTitle.text = #"Subscribe";
lblTitle.backgroundColor = [UIColor clearColor];
[tempView addSubview:lblTitle];
alreadySubscriber = [[UIButton alloc] initWithFrame:CGRectMake(12, 260, 262.0, 50)];
alreadySubscriber.layer.cornerRadius = 25.0f;
[alreadySubscriber setTitle:#"Already a subscriber" forState:UIControlStateNormal];
[alreadySubscriber setBackgroundImage:[UIImage imageNamed:#"BTN0.png"] forState:UIControlStateNormal];
[alreadySubscriber setBackgroundImage:[UIImage imageNamed:#"BTN1.png"] forState:UIControlStateSelected];
[tempView addSubview:alreadySubscriber];
Insert this in UIAleartView
[self insertSubview:tempView atIndex:0];
[self setNeedsLayout];
Override layoutsubview as you have already done push all other controls down equal to view Height.
-> basically matter is to hide UIAleartView's title behind a label.
Not sure exactly what your issue might be, but we faced a similar situation when trying to workout a custom UIAlertView, so might be similar.
The custom solution in the link you provided appears to manipulate the alerts title and background by accessing the subview hierarchy and 'guessing' which subview might be which. (I may be wrong, didn't look through it in detail) The problem with this approach is that it'll work fine for one OS version, but in subsequent OS versions, Apple may restructure this subview hierarchy in some manner, and this 'guesswork' is no longer accurate. (i.e. the subview assumed to be the background image may not be).
This could be the case, seeing that posted link is an year old. If you're proceeding with this, you may have to review the subview hierarchy to see if they still match up.
I believe, that you can find proper solution without using standart tools, which are present in UIAlertView. But in this case, you application will be not approve for AppStore. That way, I strongly recommend you, avoid to using custom buttons in AlertView.
Maybe, you will find solution using UIActionSheet instead UIAlertView . It's more customizing.
If #"_titleLabel" is part of the UIAlertView hierarchy (which looks likely given the _ prefix), I can't recommend your approach.
Apple might some day change the key strings: If #"_titleLabel" ever changes, say to #"_titleLabelView", you're sunk and you might never know that you're sunk. This might even be grounds for rejection, I wouldn't know.
It's better to start out from scratch with your own custom view, subclassing UIView. Only then can you guarantee that this will be stable from OS to OS. On top of this, the time you lose trying to find a shortcut will be positively spent constructing some thing durable.
Related
I'd like to set an action to a "part" of UILabel not the all of UIlabel, like the "term of service" of attached picture. I am using storyboard. Now, I'm thinking to cover the UIButton over the part of UILabel. However, it is troublesome to adjust the part of label with the button at any layout(iPhone 5, 6 and 6 plus) by autolayout. If you know better way, please tell me. Thank you for your kindness.
I'd recommend using a UITextView with an NSAttributedString. Get the range of the text you are looking for, and then add the button as a subview to the view.
NSRange termsOfServiceRange = [self.termsOfUseTextView.text rangeOfString:#"Terms of Service"];
self.termsOfUseTextView.selectedRange = termsOfServiceRange;
UITextRange *termsOfServiceTextRange = [self.termsOfUseTextView selectedTextRange];
CGRect termsOfServiceFrame = [self.termsOfUseTextView firstRectForRange:termsOfServiceTextRange];
CGRect convertedFrame = [self.view convertRect:termsOfServiceFrame fromView:self.termsOfUseTextView];
UIButton *termsOfServiceButton = [[UIButton alloc]initWithFrame:convertedFrame];
[termsOfServiceButton setBackgroundColor:[UIColor clearColor]];
[termsOfServiceButton addTarget:self action:#selector(termsOfServiceButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:termsOfServiceButton];
[self.view bringSubviewToFront:termsOfServiceButton];
I've solved by using TTTAttributedLabel!
https://github.com/TTTAttributedLabel/TTTAttributedLabel
Can we add activity indicator as subview to UIButton?
If yes then plz tell me how to do that?
I used [button addSubview:activityIndicator];
Not working...
I found that adding a UIActivityIndicatorView to a UIButton was a really useful method to allow users to know something is happening without having to use the MBProgressHUD (I think the HUD is really good but should not be used in all situations.
For this reason I created two functions:
I have already allocated my UIButton so it is a class variable called _confirmChangesButton
I then create my activity indicator, set its frame (taking into account the button size) and then adding the indicator is easy.
- (void)addActivityIndicatorToConfirmButton {
// Indicator needs to be in the middle of the button. So half the screen less half the buttons left inset less half the activity indicator size
CGRect rect = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 10 - 15, 5, 30, 30);
UIActivityIndicatorView * activity = [[UIActivityIndicatorView alloc] initWithFrame:rect];
activity.hidesWhenStopped = YES;
[_confirmChangesButton setTitle:#"" forState:UIControlStateNormal];
[_confirmChangesButton addSubview:activity];
[activity startAnimating];
}
Having a removal function is also useful if you are using blocks. It might be that the completion task comes back with a failure and so we want to remove the indicator and change the title back. In this function we need to make sure to remove the indicator and not the button label which is the other subview on this button.
- (void)removeActivityIndicatorFromConfirmButton {
UIActivityIndicatorView * activity = _confirmChangesButton.subviews.lastObject;;
[activity removeFromSuperview];
[_confirmChangesButton setTitle:#"Confirm Change" forState:UIControlStateNormal];
}
I found that using these two you can create a much better user experience letting the user know what is going on when they press buttons.
Hope this helps
Use the below code below to add acitivity indicator a button or any uiview object
UIActivityIndicatorView *aView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
aView.frame = CGRectMake(0, 0, {yourButton}.frame.size.width, {yourButton}.frame.size.height);
aView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
[{yourButton} addSubview:aView];
[aView startAnimating];
Hope this will help..
I don't think it's possible to add a view to a button. UIButton have this method because it's inherited from UIVIew.
The real question is : why do you want to add an activity indicator on a button and not elsewhere ?
did you do [activityIndicator startAnimating]; ALso as u are using it in a tableview just check if the tags are set properly
Does anyone know how to show a rounded squared with a spinning activity indicator? It is used in many apps. If you don't know what im talking about, it looks like the indicator when you change volume on your Mac but with a darker background. Im not sure if it is built-in to iOS or someone made it.
Like the one in this post but not full screen just the activity indicator
How to create a full-screen modal status display on iPhone?
Here's what I use when I want to show that kind of indicators.
UIView *loading = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 120, 120)];
loading.layer.cornerRadius = 15;
loading.opaque = NO;
loading.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
UILabel *loadLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 25, 81, 22)];
loadLabel.text = #"Loading";
loadLabel.font = [UIFont boldSystemFontOfSize:18.0f];
loadLabel.textAlignment = UITextAlignmentCenter;
loadLabel.textColor = [UIColor colorWithWhite:1.0f alpha:1.0f];
loadLabel.backgroundColor = [UIColor clearColor];
[loading addSubview:loadLabel];
[loadLabel release];
UIActivityIndicatorView *spinning = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinning.frame = CGRectMake(42, 54, 37, 37);
[spinning startAnimating];
[loading addSubview:spinning];
[spinning release];
loading.frame = CGRectMake(100, 200, 120, 120);
Then you just add the 'loading' view to the view of your choice and you got it.
Hope this is what you needed.
Your screenshot is probably a usage of David Sinclair's DSActivityView module. Specifically, the DSBezelActivityView component of it. Or if not, it's a close copy.
http://www.dejal.com/developer/dsactivityview
I use DSActivityView all the time. Great library. Toss that thing up while pulling down data, keeps users and clients happy.
One option: MBProgressHUD.
I don't think that screenshot is my DSBezelActivityView; the metrics look a little different. But it is very similar.
Note, DSActivityView and its subclasses don't use any images or nibs; they're pure code.
To answer the original question, it'd be easy to modify DSBezelActivityView to omit the fullscreen gray background. You could do it by subclassing and overriding the -setupBackground method thusly:
- (void)setupBackground;
{
[super setupBackground];
self.backgroundColor = [UIColor clearColor];
}
Hope this helps!
Try this simple method, Its working well for me....
UIActivityIndicatorView *activityIndicator= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
activityIndicator.layer.cornerRadius = 05;
activityIndicator.opaque = NO;
activityIndicator.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
activityIndicator.center = self.view.center;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[activityIndicator setColor:[UIColor colorWithRed:0.6 green:0.8 blue:1.0 alpha:1.0]];
[self.view addSubview: activityIndicator];
You're actually looking at using two UIView subclasses and a custom .png image to get the look you want.
The Gray translucent box would be a UIImageView object, to get the effect you're looking for you need a .png file of a grey square with rounded corners, it doesn't need to be the final size, as long as there's at least one pixel of straight edge between the corners it will work fine. You'll then load it in as a UIImage with the UIImage
stretchableImageWithLeftCapWidth:topCapHeight: method, this let's you specify the top, and left portions of the image that must stay the same, and a 1 pixel slice in each direction will be stretched out to fill the UIImage view you use the image in. http://www.bit-101.com/blog/?p=2275 has a great example of how this works.
So create a UIImage, then create a UIImageView using this image, set its opaque property to NO and the alpha property to something that looks good to you. Add this a subview of your current view.
Now you just need to add the spinning progress indicator, this is even easier, just create a new UIActivityIndicatorView and add it as a subview of the UIImageView you've already created.
The same basic method is used to create pretty much any resizable element in an iOS application. There's some examples of using them for buttons in Apple's UICatalog example code.
I'm creating some UILabels in my UIView, filling them with data, adding them to view, and releasing them.
UILabel *speed = [self scrollLabel:#"some Text" x:455.0f y:75.0f];
[scrollView addSubview:speed];
[speed release];
The method:
- (UILabel *)scrollLabel:(NSString *)text x:(float)x_ y:(float)y_ {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x_, y_, 300.0f, 20.0f)];
[label setText:NSLocalizedString(text,#"")];
[label setFont:[UIFont fontWithName:#"Helvetica" size:14]];
[label setTextColor:[UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.9]];
[label setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]];
return label;
}
I got a button, where the user can reload the data of the uilabels. I'm removing the parent view of all these labels from superfiew, generating the new data and doing the method where the labels are set, again.
The problem is, the old UILabels are still existing, so my question is, whats the best way to remove this special labels?
I made a loop and removed all subviews, the problem is, I also got some other subviews in there, which I don't want to delete.
Another question: Is there a better way to setup font-styles for multiple Labels?
I would suggest adding all the labels in a specific UIView, let's call it labelHolderView. Then every time you want to remove them, just iterate through all of its children and call removeFromSuperview for each one.
If you only want to remove specific UILabels, please provide more info as to which ones they should be.
One thing I would suggest for your code above: your - (UILabel *)scrollLabel:(NSString *)text x:(float)x_ y:(float)y_ method should return an autoreleased UILabel. So its last line should be return [label autorelease];. If you want to return a retained object, add new/copy/retain in the method's name, so that you know that the returned object is being retained every time you call it.
Consequently, you don't need to release the label after you add it to the UIView. This does not affect your specific program, but it's good to get in the habit of doing it this way so that you don't mess your retains/releases in the future.
Is there a way to get access to the up/down arrows used in the Mail app and implement them the same way?
No, there's no supported way to do it. I'd suggest you file a bug on Radar asking Apple to include more built-in artwork. In the mean time, you'll just have to draw your own.
Can you clarify your question?
The graphics for up/down are not easily accessible, though you can probably find a way to get them. For instance, see http://0xced.blogspot.com/2009/04/extract-uikit-artwork.html.
But if you're looking for the behavior, it's really quite simple. The arrows are on a momentary UISegmentedControl. Attach to valueChanged: to tell when the user taps on one of them, and use selectedSegmentIndex to tell which one they tapped.
Nowadays they are part of the sample code of IOS 4.1. Check the NavBar sample.
As Steven Fisher said, you can’t access the Apple graphic easily. But they are relatively easy to remake.
Given the images I had some trouble making this look right in iOS 7 where a UISegmentedControl will not work. This code did the trick however (added to viewDidLoad in the ViewController).
UIButton *upButton = [UIButton buttonWithType:UIButtonTypeCustom];
[upButton setImage:[upImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
forState:UIControlStateNormal];
upButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 6);
[upButton sizeToFit];
UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
[downButton setImage:[downImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
forState:UIControlStateNormal];
downButton.contentEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
[downButton sizeToFit];
UIBarButtonItem *upButtonItem = [[UIBarButtonItem alloc] initWithCustomView:upButton];
UIBarButtonItem *downButtonItem = [[UIBarButtonItem alloc] initWithCustomView:downButton];
[upButton addTarget:self action:#selector(YOUR_UP_SELECTOR:) forControlEvents:UIControlEventTouchUpInside];
[downButton addTarget:self action:#selector(YOUR_DOWN_SELECTOR:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItems = #[downButtonItem, upButtonItem];