Showing Multiple Lines in a TextView - ios

I am doing a project in Swift and I am reading data from a file and trying to display it in a text view. I am reading it line by line. For example, if I have 10 lines in the file the text view only shows the last one. And I'd like to see them all.
How can I do that?
I am doing it like this:
if let aStreamReader = StreamReader(path: "historic.txt") {
defer {
aStreamReader.close()
}
while let line = aStreamReader.nextLine() {
MyTextView.text = "\(line)"
}
The code is working in the console, because if I make a print every show up ok.
But in the text view only last line is visible.

How about trying:
MyTextView.text += "(line)"
The '+' will append rather than replace.
You might have to add \n after the lines string to create a new line:
MyTextView.text += "(line) \n"
Update:
I forgot you can't append onto a textview, so instead create an empty string before the loop and then append the line onto the string in the while loop. Then finally set the textview text to the totalString variable after the loop:
var totalString = ""
while let line = aStreamReader.nextLine() {
totalString = totalString + line + "\n"
}
MyTextView.text = totalString
Hope that helps

Make sure the value of lines property of label is 0 and try this
if let aStreamReader = StreamReader(path: "historic.txt") {
defer {
aStreamReader.close()
}
var str = ""
while let line = aStreamReader.nextLine() {
str = str + line + "\n"
}
MyTextView.text = str

Related

Swift filling textView

How can I fill my textView with restraints so that each string "test" print all the way down the textView. Example in the picture.
-Trying to match "Test" with percentages.
for _ in 1...100{
profitLoss.text = "Test\n"
}
you have to add each string to previous string, run this code in swift play ground it will help you to understand.
var finalString = "1%"
for i in 2...100 {
finalString += "\n\(i)%"
}
print(finalString)
profitLoss.text = finalString
Build your string first, then set it to the textView's text:
var test = "Test"
for _ in 1...99 {
test += "\nTest"
}
profitLoss.text = test
It's not good way as per your image that attached but you can achaive it by below
var strText = "Text \n"
for _ in 1...100 {
txtView.text = strText.appending(strText)
}

Add '...' After UILabel Word Wrapped Property

I have a UILabel that has max-lines of 2, and a word wrapping property. This is done in storyboard.
I need to add a '...' after the last wrapped word on those labels that end up being word wrapped.
Is this possible? I have tried some solutions from around the internet, but they seem to have not worked. Those include:
Testing label if it has been truncated, and appending '...' to those that have been.
Programmatically using attributed text to hijack storyboard.
Tried using Truncate Tail - Unable to use this because it cuts the word off like so "Highli...".
I think I understand what you are trying to do. This is a bit sloppy, but it should work
extension UILabel {
func truncateAndFitText()
{
if let string = self.text
{
let words = string.components(separatedBy: " ")
var lastString = ""
var tempString = ""
for word in words
{
(tempString == "") ? tempString.append(word) : tempString.append(" \(word)")
let size: CGSize = tempString.size(attributes: [NSFontAttributeName: self.font])
if (size.width > (self.bounds.size.width * CGFloat(self.numberOfLines)))
{
lastString.append("...")
break
}
else
{
lastString = tempString
}
}
self.text = lastString
}
}
}
and then use it like
myLabel.truncateAndFitText

Using a switch statement to construct a Regular Expression in Swift 3

I've created a function that generates a RegularExpression in Swift 3.0. I'm close to what I want, but the backslash is causing me a lot of trouble.
I've looked at Swift Documentation and I thought changing the "\" to \u{005C} or u{005C} would resolve the issue, but it doesn't.
Here's the array I'm feeding my regex generation function:
var letterArray = ["a","","a","","","","","","",""]
Here's the relevant portion of my method:
var outputString = String()
// getMinimumWordLength returns 3
let minimumWordLength = getMinimumWordLength(letterArray: letterArray)
// for the array above, maximumWordLength returns 10
let maximumWordLength = letterArray.count
var index = 0
for letter in letterArray {
if index < minimumWordLength {
if letter as! String != "" {
outputString = outputString + letter.lowercased
} else {
// this puts an extra \ in my regex
outputString = outputString + "\\w" // first \ is an escape character, 2nd one gets read
// this puts an extra backslash in, too
// outputString = outputString + "\u{005C}w"
}
}
index += 1
}
outputString = outputString + ("{\(minimumWordLength),\(maximumWordLength)}$/")
return outputString
My desired output is:
a\wa{3,10}$/
My actual output is:
a\\wa{3,10}$/
If anyone has suggestions what I'm fouling up, I welcome them. Thank you for reading.
When string is printed in debugger, escape character will be displayed. When it is displayed for user, it will not.

Line break in UILabel (Xcode 7 Swift)

So I have an array of elements of type float. And I have a UILabel which is supposed to display each of these elements of the array on DIFFERENT LINES.
The Array is
var history = [Float]()
I used a for loop to go through each element and append it to the UILabel
for(i=0; i<size; i++){
currentString = String(history[i])
result.text = result.text! + "\n" + currentString
}
result is the UILabel.
I tried using \n but it doesn't seem to recognise it. Any solutions for this in Swift. Thanks in advance!
You can try below to solve your issue.
let history = [1.0, 2.0, 3.0, 4.0]
var result = ""
self.lbl.text = ""
for var i=0; i < history.count; i++
{
let currentString = NSString(format: "%.2f", history[i])
self.lbl.text = self.lbl.text! + "\n" + (currentString as String)
}
And Line Number should be 0. I set that from XIB.
Thanks
The first thing I would check is if your label has (it is stupid mistake, but can happen to all of us)
label.numberOfLines = 0
Assuming that is true, instead of building the string by hand, you could use in-built function join(). Sadly, since it is not String array, you can't use it directly, but you have to create string array first
// Get both storages
var history = [Float]() // Filled with your data
var stringHistory = [String]()
// Convert each number to string
for value in history {
stringHistory.append(String(value))
}
// Finally, join array to one string using \n as separator
let finalString = "\n".join(stringHistory)
Hope some of this helps!

IOS insert line break to data retrieved from parse.com

i am trying to retrieve some string data from parse.com and i've added "\n, \n & \r" into the data, but it still does not give me the line breaks, how do format the line below to get line breaks.
self.label.text = [NSString stringWithFormat:#"%#", [object objectForKey:#"ParseStringDate"]];
if you get the text that should be presented in multiple lines then you should try to set for your label this property:
self.label.numberOfLines = 0;
it basically means that label can be multiline, make sure that frame of that label is sufficient to show more that one line of text
if some has the same challenge in swift - please find attached a short string extension:
extension String {
func formatStringFromParseWithLineBreaks() -> String {
let sourceString: String = self
let resultString = sourceString.stringByReplacingOccurrencesOfString("\\n", withString: "\n")
return resultString
}
}
It is easy to use:
let stringFromParse = "Great\\nExample"
let newString = stringFromParse.formatStringFromParseWithLineBreaks()
Have fun
self.label.text = [NSString stringWithFormat:#"first\n%#\n another", [object objectForKey:#"ParseStringDate"]];
Make sure you have numberOfLines of the label set to 0. This should work.

Resources