Textview will not add text on load - ios

I've been spending hours trying to get my textview to load with text from a string when loading but it always appears blank but when I do it through button action it works fine.
Is this a known issue or is there even a solution?
Here is the code. NSLog does show the string contents.
Variables *GetVariables = [Variables SharedInstance];
NSString *Text = GetVariables.FileText;
NSLog(#"%#", Text);
_MyTextField.text = Text;

Related

Get Rid of Line Breaks UITextView

For the past few hours I have been trying to get rid of all the visible line breaks that my UITextView is showing. So I thought it would be as simple as running this code:
NSString *trimmedString = [textView.text stringByReplacingOccurrencesOfString:#"\n" withString:#""];
[textView setText:trimmedString];
However, it seems like nothing changes after executing that. The textView text is in fact being set to trimmedString but the line breaks are still visible like shown in the picture below.
How would I properly fix this so I can have the text shown in the image (as an example), be without line breaks?
I just put your example code in the viewDidLoad of my UIViewController and totally saw the expected result. Here is one screenshot without your code snippet:
And here is a screenshot with the code snippet:
You can see that the line breaks were replaced. Did you connected your UITextView Outlet correctly ?
I fixed it with this code:
NSString *trimmedString = [textView.text stringByReplacingOccurrencesOfString:#"\r\n" withString:#" "];
[textView setText:trimmedString];

Adding label text together

Hi im relatively new to iOS and I'm just wondering how i would add two labels together.
- (IBAction)switch1
{
if (switch1.on) {
value1.text = #"3";
} else {
value1.text = #"0";
}
}
- (IBAction)switch2
{
if (switch2.on) {
value2.text = #"3";
} else {
value2.text = #"0";
}
}
As you can see i have used two switches which would show two different values if they were turned on or off.
Could someone help me understand how i would add two values together.
I.e if switch one was on and switch two was off the value would be three i want this value to be shown in another label.
So far i have come up with this but for some reason it doesn't work, i have a feeling it is the format specifier but I'm not sure.
int sum = [[value1 text] intValue] + [[value2 text] intValue];
value3.text = [NSString stringWithFormat:#"%d", sum];
Dont you have this:
int sum = [[value1 text] intValue] + [[value2 text] intValue];
value3.text = [NSString stringWithFormat:#"%d", sum];
in ViewDidLoad or something? Because you have to call this at the end of both IBActions. If you don't, your final value will never change.
Make sure you properly created an outlet connection in Interface Builder as described here:
Creating an Outlet Connection
In short words. Ctrl+Click & Drag from the UISwitch to File’s Owner and click the new switch1 or switch2 action. Create outlets for the text fields and switches and link them.
Set breakpoints in switch1 and switch2 methods and ensure there are being called.
Use po command in the console to check if the text fields and switches are configured correctly.
For example:
po _textField1
should print text field's description. It will print nil when the text field is not there - not linked to a control in interface builder.

How to keep Placeholders even after clearing all textfields

So I have 3 textfields. After they are cleared, I would like the placeholder to STILL remain there. So far for about the past week I've been fighting with this issue and no one is able to help me solve it. I was wondering if this is even possible?...
Here is my sample code to set the placeholder after clearing it:
personYouOweMoney.text = #" ";
amtYouOwe.text = #" ";
self.cellNum.text = #" ";
personYouOweMoney.placeholder = #"Name of person you owe";
amtYouOwe.placeholder = #"Amount Owed (USD)";
self.cellNum.placeholder = #"Their cell number";
All help is appreciated, thanks in advance
You are basically setting the textField's text to a single space so, technically, it ain't empty.
(Only when the textField is empty will the placeholder will be seen)
Do:
//set the placeholder's and when you want to clear the textfield, this...
personYouOweMoney.text = #"";
amtYouOwe.text = #"";
self.cellNum.text = #"";

Replace lines of text in iOS

I am needing to replace a line of text in code on my iOS app, however the lines that need to be replaced in the particular NSString will be different for different entries on the XML that is being parsed.
For example, I need to replace 129727-the-cave.mp3 with 129727.jpg.
However, I can't just tell it to replace -the-cave.mp3, because some instances of the string will have a different number and title of mp3. I think the next line is:
129838-my-song.mp3.
So, basically, I need a way to find everything from the first hyphen through mp3 and replace it, no matter what the text is?
Try this:
NSString *original = #"129727-the-cave.mp3";
NSString *sub = [original substringToIndex:[original rangeOfString:#"-"].location];
NSString *finalString = [sub stringByAppendingString:#".jpg"];
NSLog(#"finalString: %#", finalString);

Change size of text in UITextView based on content of NSString

I am trying to load a UITextView with content from instances of a NSManagedObject subclass (verses in a Bible reader app).
My UITextView is loaded by iterating through the verses in a selected chapter and appending each consecutive verse.
NSArray *selectedVerses = [[Store sharedStore] versesForBookName:selectedBook
ChapterNumber:selectedChapterNum];
displayString = #"";
for (Verse *v in selectedVerses) {
NSMutableString *addedString =
[NSMutableString stringWithFormat:#"%d %#", [v versenum], [v verse]];
displayString = [NSMutableString stringWithFormat:#"%# %#", addedString, displayString];
}
NSString *title = [NSString stringWithFormat:#"%# %#", selectedBook, selectedChapter];
[self setTitle:title];
[[self readerTextView] setNeedsDisplay];
[[self readerTextView] setText:displayString];
The above code generates the correct view, but only allows for one size of text for the entire UITextView.
I would like to have the verse number that precedes each verse to be a smaller size font than its verse, but have not found a way to make it work. I've been reading through the documentation, and it seems that this should be possible with TextKit and CoreText through the use of attributed strings, but I can't seem to get this to compile.
I do not want to load the view as a UIWebView.
Any suggestions for this are greatly appreciated.
You're looking for NSAttributedString and NSMutableAttributedString. You should read Introduction to Attributed String Programming Guide. They're generally like normal strings, but additionally contain attributes with ranges to which they apply. One more method that would be helpful to you is:
[self readerTextView].attributedText = yourAttributedString;
If you have some specific problems with attributed strings, please post your code.

Resources