Expand UILabel Text - ios

I'm trying to make my app so that long text posts you don't have to scroll through but have an option to show all and show less (show less is default). I've been trying to do it but I'm not able to do it without changing all of the cells to "Show more" or "Show less". Here is how I make it initially show less by how many characters it has in it (Message = the post text, postCellObj = the object I use to call the cell class) this code is done in cellForRowAtIndexPath...
if messages.isEmpty == false {
var messageString = messages[indexPath.row] as String
var messageFinal = ""
if count(messageString) >= 200 {
var messageNs = messageString as NSString
var messageFinal = messageNs.substringWithRange(NSRange(location: 0, length: 200))
postCellObj.message.text = messageFinal as String + "..."
} else {
postCellObj.message.text = messages[indexPath.row]
}
}
If anyone knows how to do it, it would be much appreciated. Thanks for reading!

I will use two labels for this design. The first label is for your original text, and the other one will show "Show All" or "Show Less".
The only one thing you have to do is to set the frame of the first label (original text).
The frame height for "Show All" could be calculated by line height (firstLabel.font.lineHeight)* number_of_lines_you_want. Remember to set the label property "numberOfLines" and "lineBreakMode" to "ByTruncatingTail".
The frame height for "Show Less" could be calculated by "sizeThatFits", which is supported by UILabel. The parameter "size" could be set to (frame.size.width, MAXFLOAT);
I don't familiar with swift, so I just describe what I will do with this design. Hope this helps!

Related

IOS intercepts the string of the specified width

effectThe requirement is to intercept the display of the string 3/2 of the screen width over the display...
Hope to get your help, thank you!
if (meetModel.title.length > 18) {
title = [NSString stringWithFormat:#"%#...", [meetModel.title substringToIndex:18]];
} else {
title = meetModel.title;
}
This method does not meet my needs. The page display is in Chinese.
I didn't clearly understand your question, but I think you need to add something like this (the example is in Swift 3.1). The idea is that you should set the width constraint, and if the text larger than this width, it should be automatically truncated.
yourLabel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.66).isActive = true
// It's needed for adding three dots in the end, if the string is larger then it should be
yourLabel.adjustsFontSizeToFitWidth = false
yourLabel.lineBreakMode = .byTruncatingTail

Unicode non-breaking space is removed end of label

I'm using tableview and i use one cell to create many columns.My column number is not fixed so i had to create columns using for loop inside cellForRowAt function.I want to give some spaces between my labels inside my cell.This piece of code succesfully inserts non-breaking spaces to head of the label but it removes end of the label.Thanks for your help.
let spaces = String(repeating:"\u{00A0}",count : columnSpaceCount)
if globalSubviewHeaderStructArray[i].type == "numeric"{
if globalSubviewHeaderStructArray[i].digitsAfterComma != "0" {
var dataNumber = applyComma(comma : globalSubviewHeaderStructArray[i].digitsAfterComma , data : globalSubviewDataArray[indexPath.row][i])
var newString = "\(spaces)\(checkCurrency(option: globalSubviewHeaderStructArray[i].currency, data: dataNumber))\(spaces)"
newLabel.text = newString
}else{
var newString = "\(spaces)\(checkCurrency(option: globalSubviewHeaderStructArray[i].currency, data: globalSubviewDataArray[indexPath.row][i]))\(spaces)"
newLabel.text = newString
}
}else{
var newString = "\(spaces)\(checkCurrency(option: globalSubviewHeaderStructArray[i].currency, data: globalSubviewDataArray[indexPath.row][i]))\(spaces)"
newLabel.lineBreakMode = .byTruncatingMiddle
newLabel.text = newString
}
I want to give some spaces between my labels inside my cell
Don't do this by messing with the content of the label. Do it by messing with the position / size of the labels themselves. Insert actual space (that is, empty pixels) between the labels.
To insert space in between UILabels within a UITableViewCell, set up Autolayout Contraints, either in Storyboard or in code. Depending on the target iOS version, embedding the UILabels in a StackView is probably the easiest way, but it's available in iOS >= 8 only.
Select the labels:
Choose Embed in -> Stack View:
Set spacing:
Also set the contraints for the StackView to your needs:

elipsis dots after UITextfield for secured text entry

I have multiple uitextfields setup and they are all connected with IBOutlets. I have one textfield that is a password and I have the 'Secure text Entry' selected. when I have this check I get this
Any ideas why this happens? If i deselect the secured entry the textfield rises fine depending on the size of the password with no ellipsis dots.
It does not matter how long the password is. Same thing.
If i don't have the security text selected it works fine
Any idea why? It can be a width issue because it does autosize. But why does the 'secure text entry' cause the issue?
I have faced the same problem. I think it is a bug of UITextfield. It calculates size for the text but not for the secure text(dots). I have the problem when the text includes slim characters like 1, l etc...
As workaround I have subclassed the UITextfield class and overridden intrinsicContentSize function. You might need to adjust letter spacing. I couldn't find how to get it dynamically depending on font.
override var intrinsicContentSize: CGSize {
let size = super.intrinsicContentSize
if !self.isSecureTextEntry {
return size
}
var width = size.width
if let font = self.font,
let charCount = self.text?.count {
width = "•".size(withAttributes: [NSAttributedString.Key.font : font]).width * CGFloat(charCount)
width += (CGFloat(charCount)+1) * 4.5 // this magic number is for letter spacing
}
return CGSize(width: width, height: size.height)
}

Adding multiple Lines of text into UILabel in swift

Hi I want to display some text in my UILabel line by line when an action is occurred
Hi, I have a textbox, button and a UILabel, lets say I've typed in
"This text will be displayed line by line in a UILabel"
and pressed the button to display the text in the UILabel..
so inside the UILabel it will look like:
This
text
will
be
displayed
line
by
line
in
a
UILabel
....
I've been searching forums but could not found an answer.. so I don't know if this is possible.. please help
p.s I've set number of lines to 0 and line breaks is set to word wrap
let label = UILabel(frame: CGRect(x: 100, y: 100, width: 100, height: 300)) // make the height biggest
// label.lineBreakMode = NSLineBreakMode.ByWordWrapping
// label.numberOfLines = 0
dont work if label width bigger word width
var string = "This text will be displayed line by line in a UILabel"
var array = string.componentsSeparatedByString(" ")
var newstring = join("\r", array)
label.text = newstring
let labelText = "Write\rthe\rtext\rwith\rline\rbreaks."
In order to wrap your text within a UILabel on Xcode 6 and above
Select the label you are working with.
Navigate to the Utilities panel.
Select the "Attributes inspector"
Select the number of lines you would like to display on the label (2,3,4) depending on the size of your UILabel field
Select "Word Wrap" under Line Breaks, or you can select any option of your preference.
You may manipulate the way your text appears to your taste.
Does it have to be a UILabel?
You could use UITextField instead which has some advantageous handling options

Inset text in UILabel from left

I have a UILabel which I am using to display multiple lines of text. At the moment when the text is displayed, it is right up against the left hand side of the label which doesn't look too great. I would like the text to be inset slightly from the left.
This is my code so far:
if notes.objectAtIndex(indexPath.row) as NSString == "" {
cell.notesLabel.text = "No notes to display."
cell.notesLabel.textAlignment = NSTextAlignment.Center
} else {
cell.notesLabel.textAlignment = NSTextAlignment.Left
}
I was looking at some Objective-C examples but I couldn't get them to work and I don't really think they were what I was looking for.
Also, I was trying to do the same thing with a different label and in that case I assumed I could have just added " " to the end of the string (as it is a single line label) to move it in from the right, but I was surprised to see that this doesn't work?
Thanks.
To inset the text from the left edge, you should create a UILabel subclass, and override drawTextInRect:,
class RDLabel: UILabel {
override func drawTextInRect(rect: CGRect) {
let newRect = CGRectOffset(rect, 10, 0) // move text 10 points to the right
super.drawTextInRect(newRect)
}
}

Resources