Appending data in UITextField programmatically - iphone

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"]];

Related

Each button click adds text to current label objective-c

I'm new to the Objective-c language. I'm trying to create an app that has a button and a label. The button should display some text which I did already. The only problem is that when I click the button, it only adds the specified text once. I want it to keep adding the same text to the label each I time I press the button.
Here is my .h file
{
IBOutlet UILabel *label;
}
-(IBAction)btnClcik:(id)sender;
Here is the .m file
-(IBAction)btnClcik:(id)sender
{
label.text=#"test";
}
To append to the existing text, use the string's concatenation method...
label.text = [label.text stringByAppendingString:#"test"];
You need to append to the string?
Then do
label.text = [label.text stringByAppendingFormat:#"%#", textToAdd];
where textToAdd is a NSString or some other valid object where %# is the correct format specifier.

How to set uitextfield to italics Xcode 4.5

I have an app where the users enters information in a few textfields which are then added to a uitextview by a nsstring does anybody know how i can set one of the textfields to italics and keep the others normal
regards
Edit:
Heres my code i want to only change once textfield (e.g textfbookpublisher):
NSString* combinedString = [NSString stringWithFormat:
#"%#,%#.'%#.'%#.%#,%#.",
textfbookpublisher.text,
textfbookauthor.text,
textfbooktitle.text,
textfbookplace.text,
nameofuni.text,
textfbookyear.text];
DissertationGen*bookg = [[DissertationGen alloc] init];
bookg.message = combinedString;
bookg.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:bookg animated:YES]
A quick search would have given you an easy answer. For example, this post.
Simply set the font of the UITextfield you want to a [UIFont italicSystemFontOfSize:].
try this link see accepted answer and also check TTTAttributedLabel library
Bold one Word
now you want only textfbookpublisher in italic then pass it individually to next DissertationGen view
in DissertationGen controller's viewdidload method use following code
UILabel *lblpubl = [[UILabel alloc] init];
lblpubl.font = [UIFont italicSystemFontOfSize:18.0];
lblpubl.text = your variable which contains textfbookpublisher's text;
now use lblpubl as you wish

Is it possible to highlight a NSString or draw some color to a string

i am having this code to get the text between "." for example i am having lots of text like .1 this is first.2 this is second.3 this is fourth etc etc.when i tap the first ext it displays the first text in log .the code is
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[NSTimer scheduledTimerWithTimeInterval:0.001 target:maintextview selector:#selector(resignFirstResponder) userInfo:nil repeats:NO];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
NSRange selectedRange = [textView selectedRange];
NSString *backString = [maintextview.text substringToIndex:selectedRange.location];
NSRange backRange = [backString rangeOfString:#"." options:NSBackwardsSearch];
NSRange backRangee = [backString rangeOfString:#"." options:NSBackwardsSearch];
int myRangeLenght = backRangee.location - backRange.location;
NSRange myStringRange = NSMakeRange (backRange.location, myRangeLenght);
NSString *forwardString = [maintextview.text substringFromIndex:backRange.location];
NSLog(#"%#",[[forwardString componentsSeparatedByString:#"."] objectAtIndex:1]);
}
forwadString contains the tapped text,i just want to highlight this string or draw a color above this text using core graphics or something like that.is out possible?
thanks in advance
Much to my and many other's disappointment, Apple chose not to implement NSAttributedString until iOS 3.2, and even then, all standard UI elements are incapable of rendering them!
Luckily, the few, the proud, and the brave have answered the call and DTCoreText was born.
As for an actual selection, because UITextView conforms to UITextInput as of iOS 3.2, you can use and set the selectedTextRange.
It's impossible to 'colour' an NSString, a string is just a representation of characters, it holds no font, colour or style properties. Instead you need to colour the UI element that draws the text to the screen.
If forwardString is in a UILabel or UITextView you can colour the text inside these by setting the textColor property. For example if you had a UILabel called lbl you could set the colour by using:
lbl.textColor = [UIColor redColor];

How can I remove tab spacing from text for UILabel

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

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.