How to repaint UIView when text in UILabel changes? - objective-c

I've a subclass of UIView with a UILabel with is added as subview to the view. In the layoutSubviews method the position and height of the UILabel is calculated. My problem is, that this height should be recalculated, when the text of the UILabel changes. The text is inserted into the label from the UIViewController, so the view does not know when this happen.

You can subclass UILabel to override setText: like this:
#implementation MyLabel
- (void)setText:(NSString *)text {
[super setText:text];
[self.superview setNeedsLayout];
}

Related

Make UITextView scroll action hide a UILabel

Basically, what I want to do is detect when a user scrolls inside a text view, and then hide a label (Smoothly fade out, if possible). (The label indicates to scroll to view the rest of the text, but I don't want it to still show after the user has done so.)
If you could include in your answer the code used in the h/m files, it would be greatly appreciated.
Updated code for future reference:
.h
#interface myViewController : UIViewController
#property(nonatomic,retain) IBOutlet UILabel *label;
.m
#synthesize label;
- (void)scrollViewDidScroll:(UIScrollView *)textView
{
[UIView animateWithDuration:1.0 animations:^{
label.alpha = 0;
}];
}
Then make sure to set the UITextView delegate to self.
You can use UIScrollView's delegate method – scrollViewDidScroll: to detect that the user has scrolled, and fade out your label with a UIView animation block, like so:
[UIView animateWithDuration:1.0 animations:^{
label.alpha = 0
}];

How to get the current text from a UILabel outlet in another class?

I have a basic calculator app with a label, some numeric buttons, and some simple operatin buttons. In this app, on the bottom of the screen, the top of another view(AdvancedView) is showing and there is a button on that, and when clicked, the subview slides up to the bottom of the UILabel. I'm able to access the current text in the UILabel from the CalcViewController class because there's an outlet:
#property (strong, nonatomic) IBOutlet UILabel *display;
in the .h simply by calling:
self.display.text
But i have a squareRoot button in my AdvancedView (the view that slides up) that also needs access to the UILabel text. How can I both get and set the UILabel text from another class?
You can do this:
NSString *text = nil;
UIViewController *otherViewController;
for (UIView *view in [otherViewController.view subviews])
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *) view;
text = label.text;
break;
}
}
if (text == nil)
{
NSLog(#"Hmm, couldn't find the view...");
}
you have to #synthesize your text as well
#synthesize display
then in the file you want to use it in import the file you made the label in and then give it a pointer to that class. (in the new .h file)
CalcViewController* calcViewController;
then inside your code you would do:
calcViewController.display.text;

Displaying UItableViewController inside UIPopoverController

I am dispalying a UITableViewController inside a UIPopoverController. Everything works fine expect that the size of the TableViewController is very large even thought there are only 3-4 cells. How can I make it so the popover controller's height is exactly the same as tableview's height?
You could use UIPopoverController's method - (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated or use the property on UIViewController #property(nonatomic, readwrite) CGSize contentSizeForViewInPopover to achieve this.
Override the contentSizeForViewInPopover of your UITableViewController like this:
- (CGSize)contentSizeForViewInPopover {
// Currently no way to obtain the width dynamically before viewWillAppear.
CGFloat width = 200.0;
CGRect rect = [self.tableView rectForSection:[self.tableView numberOfSections] - 1];
CGFloat height = CGRectGetMaxY(rect);
return (CGSize){width, height};
}

NSScrollView doesn't show scrollbars even although the content is large enough

I have this class:
Header:
#interface vcMain : NSWindowController {
IBOutlet NSView *scroll;
}
#property (retain) IBOutlet NSView *scroll;
-(IBAction)test:(id)sender;
#end
Source:
#implementation vcMain
#synthesize scroll;
-(IBAction)test:(id)sender {
vItem *item = [[vItem alloc] initWithNibName:#"vItem" bundle:nil];
NSView *view = [item view];
[view setFrame:NSMakeRect(0, 0, 300, 600)];
[view setAutoresizingMask:( NSViewHeightSizable) ];
[scroll addSubview:view];
}
#end
*scroll is a Custom View in a Bordered Scroll View in the Content View of a Window.
vItem is a ViewController subclass with some things on it to identify its position.
Problem: When resizing my vcMain from the default 300x600 to 150x300, I don't see any scrollbars.
What am I doing wrong?
Tom
Solved
It was simple, actually. Resizing the view apparently also resized the subview so it wasn't neccessary to show the scrollbars - however, as the elements in the subview didn't move, I didn't notice that the subview was resizing.
Solved by correcting the resizing of the view.

Anyone know how to slide in a UIDatePicker like keyboard?

Does anyone know how to slide in a UIDatePicker with a Done button like the keyboard control? If so, can you share some sample code on how. Thanks
I suggest you use a UIViewController, and show it modally.
Create UIViewController and setup the view with OK-button in Interface Builder, as you would for any view controller.
And display it using:
- (void)presentModalViewController:(UIViewController *)modalViewController
animated:(BOOL)animated
You can set the root views background to clear if you want it transparent. And optionaly animate it to cemi-transparent black when the transition has finished. Something like this would work:
- (void)viewDidAppear:(BOOL)animated {
if (animated) [UIView beginAnimations:#"fadeDark" context:NULL];
self.view.backgroundColor = [UIColor colorWithWithe:0.0f alpha:0.5f];
if (animated) [UIView commitAnimations];
}
1.create a subclass of the UIButton class
2.redeclare inputview as read-write
#property (nonatomic, strong, readwrite) UIView *inputView;
3.set canBecomeFirstResponder to YES
- (BOOL)canBecomeFirstResponder {
return YES;
}
4.set inputview as datepicker
self.inputView = self.datePicker;
5.set UIResponder to firstResponder when you want
[self becomeFirstResponder];
5.you can see datepicker slide in like keyboard