UILabel UITextView - objective-c

I'm stuck in a problem now,I want to get the text be selectable,but the text had been put into a UILabel,so currently it's impossible for me to select,change the UILabel to UITextView is not a good choice for me now,since i just maintain the old code and only can make some little change on it,but the latest command need me to implement this function on UILabel,so anybody get any idea .or just give me some little instruction maybe good enough,thanks a lot
Getting the word touched in a UILabel/UITextView
is not helpful

You need to switch to a UITextView or UITextField. It will be a smaller, easier change than trying to re-implement the selection UI that those classes already implement. You can make the text view (or field) selectable but not editable by setting the view's editable property to NO.

Related

UITableView Cell - Edit?

Is it possible to use a UITableView to be able to enter a value into a database's field.
For example, if I was to have a UITableView pointing to a field within a database and if I wanted to enter a new entry into the database - tap on the UITableView Cell that would then allow keyboard input into the cell which ultimately end up being a new record in the database??
This is possible, but if something is possible doesn't mean you should be doing so.
You might ask why?
Well! you are trying to input data from view directly to database, this is a very bad practice. There are many reason for it being bad, the major is efficiency and security reasons.
You should consider using MVC pattern.
Now since its completely possible, I will explain the idea on how to do it and conclude with links that will have real code examples.
In tableView:cellForRowAtIndexPath:
add a TextField with tag (to get the reference back in future) and add it to contentView of the cell and have it hidden.
Now in tableView:didSelectRowAtIndexPath: make the cells editing property to YES.
Then, in tableView:willBeginEditingRowAtIndexPath:
get the reference to the textfield in contentview using viewWithTag: method and hide the textLabela and unhide the textfield.
In textfield's delegate textFieldDidEndEditing: make cell's editing property as no (yea, you need to keep the reference) unhide the textlabel and hide textfield.
In tableView:didEndEditingRowAtIndexPath: write methods which will commit the changes to your db.
Below are list of links which will get you code examples:
Having a UITextField in a UITableViewCell
Accessing UITextField in a custom UITableViewCell
iOS Database Tutorial
There are no examples for your requirement 'coz it bit bad way of doing things.
Yes its possible....
You can use delegate methods to take data form you cells textfield to your parent view controller and then save data in database.

UILabel with more... text

I have a text with multiple lines but at the end I want to add the phrase "more..." if the user press the more... word than the system it suppose to display the rest of the paragraph.
How can I do that?
one way is: use a UILabel to display a short text
put a UIButton under that with "show more" make settings to button to look like a UILabel
catch the action of the button, and remove the button and display a multines text and change the size of the UILabel.
This isn't the right approach for novice programmers, I hope I have helped with an idea.
You could use a UITextView that contains the limited text and add a UITapGestureRecognizer that when tapped, removes the gesture and adds text to the textview.
There is no particularly easy way, built-in way to do it. If you want to implement it yourself, your best bet is to use a UITextView instead of a UILabel. UITextView conforms to the UITextInput protocol, so when you detect a tap on the text view, you can use closestPositionToPoint: and related messages to figure out which part of the string was tapped.
Instead of doing it yourself, you could use TTTAttributedLabel or OHAttributedLabel. Both of these have built-in support for detecting taps on links. I haven't used either, so I can't advise you on which is better. There are probably other free solutions available, but those were easy to find.

How do I make a Cocoa NSTextView grow as the user types into it?

For a Cocoa application I am writing, I would like to support a panel to the right of the main document content where users can add notes for the currently selected document content. (If you are familiar with Microsoft Word or Scrivener, this feature is similar to the comment feature in those applications.) Scrivener does a nice job of starting with a text field sized to fit the default text, and then growing it taller as the user types into it. I'd like to implement the same behavior for my Cocoa app.
What's the basic strategy?
There are delegate methods that allow you to capture the actual keystrokes as they come in.
Implement the below delegate method to resign first responder, based upon the keyboard
-(BOOL)textFieldShouldReturn:(UITextField *)textfield
Implement the below delegate method to detect when focus has been given back to the TextField. You may also want to perform the deletion of current text, or retain the text that was already there if you wish
-(void)textFieldDidBeginEditing:(UITextField *)textfield
Implement the below delegate method to detect the character(s) entered and where (based on the caret position), and essentially add the characters to your privately held and displayed string (displayed out to your textfield that is)
-(BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
Implement the below delegate method to detect when editing has finished so that you can perform any other cleanup etc... that you wish to do.
-(void)textFieldDidEndEditing:(UITextField *)textField
I will get back to you on the dynamic sizing of your TextView itself, but that (at least on iOS) as Ive seen has a solution and at one point I have used it. You will essentially make your font size static, potentially your width static, then edit your height based on how many lines you have, or you could keep your height static, and change your width based on characters, etc... up to you.
Here is a great set of responses on StackOverflow about dynamic sizing
How do I size a UITextView to its content?
So if you combine the keystroke recognition with the dynamic sizing you should have it!!!
A custom non NSScrollView embedded NSTextView does the trick. See my answer here How do I make NSTextView grow with text?.

Placing a text field in a Menulet in Mac OS X

I'm working on an menulet-based application for the Mac that requires me to have a text field in the menu. I've searched high and low and have not found any examples of how to do this, yet I have seen many menulet apps that implement a text field. I've found no way to do it from Interface Builder, so I guess this text field would need to be defined and added to the menu programmatically.
If anyone could help me with this issue, it would be greatly appreciated.
An NSTextField is a view based subclass, therefore in code, when you create the NSMenuItem you must use the – setView: property and add the NSTextField to the menu item.
Also if you use an NSStatusItem, it also has a -setView: property.

What is the proper way to align UITableViewCells when only some have an imageView?

I am new to iPhone programming and working on my first real application (i.e. one not written in a book or online) and I've run into a small problem which I could solve a multitude of ways, but feel like there should be a good solution that perhaps I am just missing.
Here is the scenario: I have a UITableView with a bunch of standard UITableViewCells in it. What I want to do is toggle a green check mark when the cell is selected and I have that part working (note: I'm already using the accessoryType for something else, so I can't use it for the checkmark...besides, it's not as pretty). Unfortunately, when I toggle the checkmark like so:
if (...) {
cell.imageView.image = [UIImage imageNamed:#"checkmark.png"];
} else {
cell.imageView.image = nil;
}
It makes the cell's label bounce back and forth depending on whether it is checked or not. What is the proper way to align the cell's text (set via cell.textLabel.text) regardless of whether or not it has an image set? The solutions I have come up with are:
Create a blank 40x40 png image in Photoshop and set the unchecked to that
Create a blank 40x40 image solely in code
Set some setting that I don't know about that will align it for me
Create a subclass of UITableCellView that does what I need (which would be stupid, I'd just go with option 1...)
Suggestions? Thoughts? Comments? Thank you very much :-)
P.S. I'd like the solution to work with OS 3.0 and 4.0 if that makes any sort of difference.
Option 1 is the way to go for a standard cell.
Strictly speaking, you do not have to subclass UITableViewCell to customize the layout. You can add any views you wish to the contentView. So you can add a UILabel and UIImageView to the contentView instead of using the imageView and textLabel properties.