iPhone: Phone Number UITextView vs UILabel - iphone

I have a UITableView. In row I need to setup text, so I need to use UILabel or UITextView. I need to detect phone number in text, so I use UITextView. BUT I need to set text only in 2 lines and if it's longer, setup ellipses in the end , because row height isn't big, so that work UILabel do perfect. So how to be ??? Can UILabel detect phone number ? or can i set UITextView in 2 lines and set ellipses in the end ?? Thanks...

I do not think that the UITextView has a property that let you set ellipses at the end if the text is to long. However that is simple to fix:
NSString documentation
You could use this method if all you want to do is to allow let say 120 characters, and starting from 0.
substringToIndex:
Returns a new string containing the
characters of the receiver up to, but
not including, the one at a given
index.
(NSString *)substringToIndex:(NSUInteger)anIndex
NSString *shortText =
[[someLongTextVariable substringToIndex: 120] stringWithString:#"..."];
Maybe you should use a mutable string for this, I do not know what performes the best. But if you do this, you could atleast detect phone numbers.

Related

how to set the bold in part of the text in UILabel

I am new in iPhone application development. How to set the bold in part of the text in UILabel.
Example:
Step 1- Separate all parts of the plant (stem, roots, leaves, flower etc..)
Step 2- Only test one part at a time
I need like example in UILabel
Thanks in advance
Implement TTTAttributedLabel or OHAttributedLabel and use:
NSMutableAttributedString *theText = theLabel.text;
[theText applyFontTraits:NSBoldFontMask range:NSMakeRange(0,[text length])];
theLabel.text = theText;
to bold specific parts of the string (change the NSRange as required).
For now, you're going to have to use a third party component, since there's no attributed string support in UILabel.
Try https://github.com/mattt/TTTAttributedLabel/
In order of increasing complexity, here's some other solutions:
Alternatively, use a WebView with HTML.
Alternatively, use a CATextLayer, this is only slightly more work than adding a UILabel, but you need a little understanding of how layers work vs. views.
Alternatively, draw your strings yourself using CoreText.

NSString UITextView without replacing previous text

How would I go about adding text to a UITextView without replacing the previous text?
So far I have a UITextView and a UIButton that adds the text to the UITextView, but I would like the text field to append more text every time you hit the button instead of completely deleting the text and replacing it.
Here are some ways to overcome obstacles in iOS development:
Look at the documentation for the particular class you're trying to manipulate. In this case, UITextView documentation can be found within Xcode or online.
Command-Click on UITextView or any other object anywhere in your code, and it will bring you to the header file for that class. The header file will list every public method and property.
Look at your existing code. I'm assuming that since you have a button that adds text to a UITextView, you understand how to set its text. 99% of the time you'll find that any setter (mutator) methods will have a corresponding getter (accessor) method. In this case, UITextView has a method called setText: and a matching method just called text.
Finally, NSString has a convenience method called stringWithFormat: that you can use to concatenate (join) two strings, among other very useful things. %# is the format specifier for a string. For example, to combine two strings, stringOne and stringTwo, you could do the following:
NSString *string = [NSString stringWithFormat:#"%# %#", stringOne, stringTwo];
I will leave you to come up with the answer as to how to combine NSString stringWithFormat: and UITextField text and setText: to achieve what you'd like to accomplish.
Edit:
The OP was unable to figure out how to utilize the information above so a complete code sample has been provided below.
Assume you have synthesized property (possibly an IBOutlet) UITextView that you have initialized called myTextView. Assume also that we are currently in the method scope of the method that gets called (your IBAction, if you're using IB) when you tap your UIButton.
[myTextView setText:[NSString stringWithFormat:#"%# %#", myTextView.text, #"this is some new text"]];
Explanation: myTextView.text grabs the existing text inside of the UITextView and then you simply append whatever string you want to it. So if the text view is originally populated with the text "Hello world" and you clicked the button three times, you would end up with the following progression:
Initial String: #"Hello world"
Tap one: #"Hello world this is some new text"
Tap Two: #"Hello world this is some new text this is some new text"
Tap Three: #"Hello world this is some new text this is some new text text this is some new text"
If all you are doing is appending text, you might find this a little simpler:
myTextView.text = [myTextView stringByAppendingString:#"suffix\n"];
I found this on UITextView insert text in the textview text. Sadly, I have not found a way to append text directly without a wholesale replacement of the text in the UITextView. It bugs me that the effort involved is proportional to the total length of the existing string and the suffix, rather than just the suffix.
A more efficient way to append text is to use replace() at the end:
extension UITextInput {
func append(_ string : String) {
let endOfDocument = self.endOfDocument
if let atEnd = self.textRange(from: endOfDocument, to: endOfDocument) {
self.replace(atEnd, withText: string)
}
}
}
#Jack Lawrence: Your answer doesn't cover the question completely.
The example below will not scroll neatly while running off the bottom when called every second:
self.consoleView.text = [NSString stringWithFormat:#"%#%#%#", self.consoleView.text, data, #"\n"];
[self.consoleView scrollRangeToVisible:NSMakeRange(self.consoleView.text.length, 0)];
This is caused by setText replacing the original text every time thereby resetting associated contentOffsets etc.
This was possible prior to iOS 7, but since iOS 7 it seems that setText cannot be prevented from exhibiting jumpy behaviour. Appending does not seem to be an option for TextViews in this scenario?

UILabel Fit Text Perfectly

I have a UILabel that needs to be set to multiple lines of text. I have set the noOfLines property to 0 and I have the following code:
self.aboutLabel.lineBreakMode = UILineBreakModeWordWrap;
self.aboutLabel.text = self.selectedVegetableCategory.about;
[self.aboutLabel sizeToFit];
The above code does make the text span multiple lines but still some of the text is missing. I am using CoreData to retrieve the text from db and the fields are varchar but they do not have any limit on it (I have not placed any limit on the number of characters). Does, varchar by default has a limit of number of characters?
UPDATE 1: I just checked again and this is definitely the UILabel issue since CoreData returns the complete text without truncation.
UPDATE 2: The original problem still exists and now there is a new problem. I put a new label just below the original label and now the new label does not move down when the original label expands.
What is the frame of the label before & after the call to sizeToFit?
I think that you'll have to provide it a specific frame if you want it to word wrap properly.

UITextView or UIWebView: How to display pages of a simple editable TXT file

All I want to have is a full-screen simple text editor. However, I don't want the scrolling component, but rather let the user flick through the pages (instead of scrolling). So I need to import or open a TXT and then format it by braking it down (e.g. by dividing its contents to 10 lines per screen/page).
My question is how I will display the txt? UITextView is scrollable (even though I can disable this in IB)... I did not find any method for UIWebView to let it format my contents on different 'pages' or screens.
Where will I need to start? Ideally I'd need some sample code. All the samples for UIWebView do not tell me anything about how to format editable text on several pages.
So all I really want is an UITextView which is not scrollable and opens up a new page/screen if I run out of space on the first page/screen.
Thanks for any help to get me started.
first thing first ...... there is no particular method to achieve this
1.You need to break your single string into multiple strings, to do that you can use
int pageNumber; // suppose this keep track of on what page you are
int count; //suppose this keep track of how long string your one screen support
NSString* completeString; //suppose this is your string
NSRange range = NSMakeRange(pageNumber * count, count);
NSString* temp = [completeString substringWithRange:range];
2.Now instead of using UITextView (if you don't want user interaction ) you should use UILable
just change the property of UILabel (this one is of your interest)
UILabel* myLabel; //suppose this is that label
myLabel.numberOfLines = 0; //this will chage your label to go multyline.
You should be able to achieve this by putting your UITextView into a UIScrollView and setting pagingEnabled on the UIScrollView to YES.

One label, two different fonts?

I need to format text in a label like this:
username: some text from this user. This will
create additional lines of text that will go
on and on and on.
Where "username" is bold. This will go into a UILabel, which is in a custom table cell. Is there a way to get this type of layout?
For this relatively simple case, you might be able to fake it. Have one label with the bold username, and another label with the plain text in the same position. Insert enough spaces before the plain text to leave room for the username. You can use UIStringDrawing methods to measure the bold text and the spaces.
CGSize usernameSize = [theUsername sizeWithFont:theBoldUsernameFont];
CGSize spaceSize = [#" " sizeWithFont:thePlainCommentFont];
NSString *indentedComment = [NSString stringWithFormat:#"%*s%#" , (int)ceil( usernameSize.width / spaceSize.width ) , "" , theComment];
If you use plain UILabel it's not available. Use two labels for this task.
You need to use either a UIWebView or CoreText to do this kind of advanced text layout. A web view has a lot of overhead but is most flexible and you can't use it effectively in a UITableView cell. CoreText is low level and not that well documented. You could ditch the table view and just lay out the table with CSS and HTML in the web view, which is how I do it.
You can still use a UITableViewCell but have the cell use a UIWebView subview. Set up a custom cell subclass with a clever setter method that allows you to send nsstrings to the method with turns those into a pretty formatted view.