How can I remove tab spacing from text for UILabel - iphone

I'm reading some text from a local xml file and displaying it in a UILabel. The text in the xml initially had tabbed spacing in it. I removed this tabbing manually in the editor but it's still showing up in the UILabel and it makes the text layout look very messy.
How can I resolve this?

Try with below
myLabel.text = [myText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet];

When you assign the text to your label you can do this:
myLabel.text = [textWithTabs stringByReplacingOccurrencesOfString:#"\t" withString:#""];
This will remove the tabs completely.

stringByTrimmingCharactersInSet:
Returns a new string made by removing
from both ends of the receiver
characters contained in a given
character set.
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set
NSString Class Reference

Related

How to replace text in UITextView with selected range?

I want to replace the text in UITextView text in selected range. This is my question. Here i mention what i did? and what i want to do?. I have an UITextView and entered the below text in textview.
Ask question here, i have saved the range in of the text in textview. The text range is {17, 0}. I take the NSRange in -(void) textViewDidChange:(UITextView *)textView delegate.
Now i want to replace the text question with answer and i want to replace the text here with them. The UITextView.text look like this,Ask answer them` after replaced the texts.
How can i replace the texts with the selected ranges? Can you please help me? Thanks in advance.
Since iOS 7 there is the textStorage in the textView that inherits from NSMutableAttributedString so you can use those methods:
Objective-C
[self.textView.textStorage replaceCharactersInRange:withString:];
or
[self.textView.textStorage replaceCharactersInRange:withAttributedString:];
Swift
textView.textStorage.replaceCharacters(in: range, with: string)
Well... I'm not sure to understand correctly what you're trying to do, but if your goal is to change some characters in a selected range you can follow these steps:
Get your UITextView content and put it in a NSString:
NSString *textViewContent = textView.text;
Change the characters in the range you want:
NSString *newContent = [textViewContent stringByReplacingCharactersInRange:range withString:replacement];
Replace old content with new one:
textView.text = newContent;
Anyway if you just want to replace Ask question here with Ask answer them the fastest solution is just:
textView.text = #"Ask answer them";
Well solution from top of my head..
NSArray *Dividedstring = [[self.TextView.text] componentsSeparatedByString:#" question here "]; // this will divide the string into two parts ..one before question here and second after question here.
NSString * firstpart = [Dividedstring objectAtIndex:0];
NSString * secondpart = [Dividedstring objectAtIndex:0];
self.TextView.text = [NSString stringwithFormat:#"%# answer here %#",firstpart,secondpart];

Appending data in UITextField programmatically

I am creating a report & printing the same on a UITextView.My report has some titles and there respective subtitles. I am printing 1st title then its subtitle & then second title and its subtitle and so on. I want different font and font sizes for the titles and subtitles.
But the problem is the text field always prints the last subtitle.If any one know solution to this problem,let me know.
Here is what i have dome in my .m file
- (void) printData {
// data is UITextView object
[data setFont:[UIFont fontWithName:#"Helvetica" size:15]];
NSString *title = #"TITLE" ;
data.text = title;
[data setFont:[UIFont fontWithName:#"Arial" size:15]];
NSString *subtitle = #"SUBTITLE" ;
data.text = subtitle;
}
First of all I would Use UITextView instead of UITextField, as it is build for long text.
If you just keep calling the method text of either UITextField or UITextView, the currently residing text will be replaced with your last text. To avoid that you have to append new text to the text that is already residing in the UITextField or UITextView.
NSString *appendThisText = #"subtitle";
self.myTextView.text=[NSString stringWithFormat:#"%#%#", self.myTextView.text, appendThisText];
As for the fonts. The font property applies to the entire UITextField and UITextView content.
What you could do in your case is to use UIWebView and render your text using HTML.
Another way:
[myTextView setText:[myTextView.text stringByAppendingString:#"my string"]];
Or with a \n line break:
[myTextView setText:[myTextView.text stringByAppendingString:#"\nMy string"]];

Problem to write more than a line in a text view

i have a UITextView in which i have to write programmatically multiple lines. When i do this :
[textView setText:#"String number 1\n"];
[textView setText:#"String number 2\n"];
i get only : String number 2 as if it's writing the line over the other. I appreciate any suggestions :)
As the name suggests, setText: is a setter: it replaces the existing text.
That's quite like if you had an int val and you were doing:
val = 5;
val = 8;
Of course at the end val will be equal to 8 and the value 5 will be lost. Same thing here.
Instead, you need to create a string that contains all the lines, and affect directly to the textView:
[textView setText:#"Line1\nLine2\nLine3\n"];
If you need to do it incrementally, say you need to add text to existing text in the textView, first retrieve the existing text, append the new line, and set the new text back:
// get the existing text in the textView, e.g. "Line1\n", that you had set before
NSString* currentText = [textView text];
// append the new text to it
NSString* newText = [currentText stringByAppendingString:#"New Line\n"];
// affect the new text (combining the existing text + the added line) back to the textView
[textView setText:newText];
setText replace whole content of textView. You better to prepare result string and than set it by single setText method call.
for example:
NSString *resultString = [NSString stringWithFormat:#"%#%#", #"String 1\n", #"String 2\n"];
[textView setText:resultString];
you can append the strings :
mTextview.text = [mTextview.text stringByAppendingString:aStr];
According to How to create a multiline UITextfield?, UITextField is single-line only. Instead, use a UITextView.
In addition, as others have pointed out, if you call setText a second time, you'll overwrite the previous value. Instead, you should do something like this:
[textView setText:#"String number 1\nString number 2"]

how to print a variable into a text box

hi all
I have a one variable. i want to print that varible into a text box. how to do this.
You can only display the string data in text box (UITextView),if you have other than string data it require a conversion to NSString then use the text properity of UITextView to set the display text.
The below code is for your reference.
NSString* myData = #"dISPLAY me in text box";
UITextView* myTextView = [[UITextView alloc] initWithFrame:myframe];
myTextView.text = myData;
write
NSString * str=#"Text";
myTextField.text=str;
myTextField is the name of my textField(you can write the name of your text Field).
yourTextField.text = yourVariable;
If you want to have it as a string value you can make use of stringValue
You create a UILabel object, add it as a subview of your view, and call setText: on it.
Unless you are asking how to do GUI programming in general.

Contents of UITextField and UITextView to NSString

I have a UITextView and 2 UITextField set up. UITextView resigns first responder status when empty part of the screen is tapped, the same for the 2 UITextField, plus for these 2, the return key also resigns first responder status. All 3 are declared in interface.
I would like to get the contents of all of these to individual NSString and/or learn how to enter them directly into something like:
NSString *urlstr = [[NSString alloc] initWithFormat:#"http://server.com/file.php?var1=%#&var2=%#&var3=%#", *content of UITextView*, *content of UITextField*, *content of UITextField*];
This is a very basic question, i know, but i'm pretty much a novice. If i learn how to do this i'll probably be able to pick up from there.
cheers
(edited)
UITextField and UITextView both have a text property that you can use to retrieve the string values. For example,
NSString *string = [NSString stringWithFormat:#"%#, %#", textField.text, textView.text];
Keep in mind you'll probably want to examine the strings to make sure they're not empty or contain invalid characters before putting them into a URL.
The accepted answer is good, I just wanted to add the following for an expanded look at grabbing text in iOS.
See the textInRange: aspect of the below code that I devised to use one function to determine the text whether it's a UITextField, UITextView or any other class that complies with the UITextInput protocol.
//handle text container object length whether it's a UITextField, UITextView et al
NSUInteger LengthOfStringInTextInput(NSObject<UITextInput> *textContainer)
{
UITextPosition *beginningOfDocument = [textContainer beginningOfDocument];
UITextPosition *endOfDocument = [textContainer endOfDocument];
UITextRange *fullTextRange = [textContainer textRangeFromPosition:beginningOfDocument
toPosition:endOfDocument];
return [textContainer textInRange:fullTextRange].length;
}
By changing the return type to NSString and removing .length you could have the functionality of the text property on any class.