UITextView scroll to bottom without animation - uiscrollview

I want the UITextView to show the last text so I did
[self.textBox scrollRangeToVisible:NSMakeRange([self.textBox.text length], 0) ];
However, I don't want the scrolling action since it will start from the very top and scroll down each time I enter in a new line of text.
How do I go about doing this?

UITextView inherits from UIScrollView, so you can call the latter's methods directly on it.
CGPoint offsetPoint = CGPointMake(0.0, textView.contentSize.height - textView.bounds.size.height);
[textView setContentOffset:offsetPoint animated:NO];
P.S. In your question you wrote UITextField, but it was clear from your description and code that you meant UITextView. I edited the question to reflect this.

Related

How to animate a tabbarbutton image

I have a toolBarItem which calls a save action on a database. I would really much like to change the image of the item (a cabinet) dynamically so that a drawer opens, a label is animated "inside" and then it closes. Very much like the trash item on the mail app animation.
I know how to make a UIView pop-up by scaling it up and down on an animation on a given times given in an array (together with an array of CT scale), so I'm guessing it could be done more or less the same way.
Does anyone know about an example of who to accomplish that?. Back on xcode 4.1 i was able to highlight the button while the label was moving, but I cannot do that anymore (somehow I did add a normal button on the toolbarItem, which I cannot do anymore).
Thanks in advance!
well, if somebody is having the same issue here is how it can be done:
- (IBAction)barButtonAction:(UIBarButtonItem *)sender {
NSArray *frameArray=[NSArray arrayWithObjects:[UIImage imageNamed:#"01-refresh.png"], [UIImage imageNamed:#"02-redo.png"], [UIImage imageNamed:#"03-loopback.png"], [UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"], nil];
self.button.image=[UIImage animatedImageWithImages:frameArray duration:10.0];
}

Help with Xcode drawRect using textfield to input value

i am currently trying to do a bar graph in XCode. I have tried CPGraph and all the stuff around but they are all out of date and i need help for XCode 4. I am completely newb with this and that's why i need your help.
Here is my code:
-(void)drawRect:(CGRect)rect
{
NSString *s = textField.text;
value = [s intValue];
NSLog(#"%i",value);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 60);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, 400, value);
CGContextAddLineToPoint(context, 400, 100);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
Notice that i have changed one of the point value for "value".
What i have done is create a UITextField and i want to be able to write like 500 in the UITextField and that the bar adjust itself. Currently the NSLog tell me that value is equal to 0 even if, before building my app i manually enter a number in the TextField.
I have been searching fo 3 days now and everything i found give me errors and are incomplete as i says i know almost nothing about objective-c in xcode. I also noticed during my search that this type of line doesn't refresh real time if you dont tell him. I would like help with that too if that's part of my problem. If you want more information on my code just tell me.
I will be really grateful if somebody can help me.
here is my .h :
#interface draw2D : UIView
{
UITextField *textField;
}
#property (nonatomic, retain) IBOutlet UITextField *textField;
#end
OK, it looks like since you have your text field hooked up in IB to File's Owner, that is typically your app delegate which is not the class you have above as that is a UIView. So, not unless you changed File's Owner to this UIView then your text field is not hooked up. So, typically what I do is to add a UIView to the xib window in IB (if you are not using the one that comes free when you create a XIB). Then that is what I'll attach the text box to, not File's Owner. Kind of begs the question of how anything is attached to Files Owner not unless you have a UITextField declared in there too (again providing you didn't change the class from the app delegate to this view class).
I know that if someone is new to this is is almost like greek. I developed in Java, C/C++, C#, etc for years and this was like learning how to program all over again.
If you are new to this I highly suggest the iPhone dev book by Big Nerd Ranch. I found it very useful.
OK, I found out how to redraw my line with a button refresh.
What I did was create a button and link it with my UIView where my line is being drawn. Inside I wrote [self setNeedsDisplay]; and each time I click the button it refreshes the view and gives me back my NSlog that I set in the same method as my drawRect.
Many thanks again Merky.

Selectable area for NSButton

So I am basically trying to make a list of selectable text items (just a list of text, no button bezels, backgrounds, etc.). I suppose that I could make this happen with an NSTableview, but trying to make the table view completely transparent and still functional was giving me some issues. Anwyays, I am trying to do it with NSButtons that I create programatically and add to my view in a list, without any background or bezel. However, when I set the properties to make the button transparent and without bezel, the clickable area of the button is relegated to the text of the title of the button alone. Clicking anywhere else that the button should be (around the title) no longer works. Here is the code I am using. I want to be able to click anywhere in the rect in which I create the button in order to cause a click. FYI I have tried NSSwitchButton without the checkbox image and it is the same thing. Thanks for your help!
for(NSString *theTask in theTasks){
NSButton *theCheckBox = [[[NSButton alloc] initWithFrame:NSMakeRect(xCoordinate + 25, yCoordinate + ([tasksWindow frame].size.height/2) - 60, [tasksWindow frame].size.width - 40, 25)] autorelease];
[theCheckBox setButtonType:NSToggleButton];
[theCheckBox setAction:#selector(taskChecked:)];
[[theCheckBox cell] setBackgroundColor:[NSColor clearColor]];
[[theCheckBox cell] setBordered:NO];
NSAttributedString *theTitle = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#", theTask] attributes:[NSDictionary dictionaryWithObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName]] autorelease];
[theCheckBox setAttributedTitle:theTitle];
[[tasksWindow contentView] addSubview:theCheckBox];
yCoordinate -= 20;
}
UPDATE: I've been able to confirm that setting the background color to clear is what seems to cause the button to stop responding to clicks within its full boundaries (not the removal of the border).
So to answer my own question, it was because I was overlaying the transparent buttons atop a transparent NSWindow (which refuses mouse events). I simply had to set the window NOT to ignore mouse events and the behavior went away.

Dragging an NSTextField inside of a window

Im attempting to make a little app that lets you add text boxes to you canvas (window). I have an NSTextField that needs to let you drag it around the window. When you drop it it needs to stay in the spot you mouse left it. Heres my code to make the fist text field:
NSTextField *myTextField=[[NSTextField alloc] initWithFrame:NSMakeRect(200.0, 200.0, 200.0, 25.0)];
[myTextField setBordered:NO];
[myTextField setStringValue:#"Double Click to edit"];
[[window contentView] addSubview:myTextField];
//Some sort of dragging code for myTextField
If anyone has ever done something like this any help is really appreciated.
My first instinct would be to create a subclass of NSTextField and override some or all of mouseDown:, mouseDragged:, and mouseUp: to create the dragging behavior you want. The mouse events section in Apple's Event-Handling Guide might be helpful.

NSTextView not refreshed properly on scrolling

I have a NSTextView with a sizeable quantity of text. Whenever I scroll however, the view isn't updated properly. There are some artifacts that remain at the top or the bottom of the view. It appears that the view doesn't refresh itself often enough. If I scroll very slowly the view updates correctly though. If I add a border to the view everything works perfectly, borderless view is the one that has a problem. Here's a link to a screenshot:
Thanks
Have you set the setDrawsBackground and copiesOnScroll propertes for either the NSScrollView or the NSClipView?
The first thing I would suggest is turning off the "draws background" property of the NSScrollView:
[myScrollView setDrawsBackground:NO];
Note that this should be set on the NSScrollView, and not on the embedded NSClipView.
The following excerpt from the documentation may be relevant:
If your NSScrollView encloses an NSClipView sending a setDrawsBackground: message with a parameter of NO to the NSScrollView has the added effect of sending the NSClipView a setCopiesOnScroll: message with a parameter of NO. The side effect of sending the setDrawsBackground: message directly to the NSClipView instead would be the appearance of “trails” (vestiges of previous drawing) in the document view as it is scrolled.
Looks like the text field isn't even in the scrolling-area... Are you sure something isnt overlapping it?
I had a similar trouble - artifacts develop when the NSTextView is embedded in another scrollview (ie. a NSTableView).
I actually turned on the setdrawsbackground, and then added a nice color to make it disappear again.
-(void)awakeFromNib{
NSScrollView *scroll = [self enclosingScrollView];
[scroll setBorderType:NSNoBorder];
[scroll setDrawsBackground:YES];
[scroll setBackgroundColor:[NSColor windowBackgroundColor]];
}
This in combination with a scrollWheel event let me use the NSTextView in a NSTableView.
-(void)scrollWheel:(NSEvent *)theEvent{
NSScrollView *scroll = [self enclosingScrollView];
[[scroll superview] scrollWheel:theEvent];
}
I had the same trouble some time ago. I don't remember how I solved it.
Try to place the NSTextView to another view if the superview is a custom view. Just to see what will happen.

Resources