Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 502 Vote(s) - 3.41 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if UILabel is truncated?

#11
Wouldnt it be easy to set the title attribute for the label , setting this will display full label when hovered.

you can calculate the length of the label and div width (convert to length -

[To see links please register here]

).

set jquery to set title if length is greater than the div width.

var divlen = parseInt(jQuery("#yourdivid").width,10);
var lablen =jQuery("#yourlabelid").text().length;
if(lablen < divlen){
jQuery("#yourlabelid").attr("title",jQuery("#yourlabelid").text());
}
Reply

#12
***Swift 3 solution***

I think the best solution is to **(1)** create a `UILabel` with the same properties as the label you're checking for truncation, **(2)** call `.sizeToFit()`, **(3)** compare the attributes of the dummy label with your actual label.

For example, if you want to check whether a one lined label that has varying width truncates or not, then you can use this extension:

extension UILabel {
func isTruncated() -> Bool {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: CGFloat.greatestFiniteMagnitude, height: self.bounds.height))
label.numberOfLines = 1
label.font = self.font
label.text = self.text
label.sizeToFit()
if label.frame.width > self.frame.width {
return true
} else {
return false
}
}
}
...but again, you can easily modify the above code to fit your needs. So let's say your label is multilined and has varying height. Then the extension would look something like this:

extension UILabel {
func isTruncated() -> Bool {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.font = self.font
label.text = self.text
label.sizeToFit()
if label.frame.height > self.frame.height {
return true
} else {
return false
}
}
}
Reply

#13
Swift (as extension) - works for multi line uilabel:

swift4: (`attributes` param of `boundingRect` changed slightly)

extension UILabel {

var isTruncated: Bool {

guard let labelText = text else {
return false
}

let labelTextSize = (labelText as NSString).boundingRect(
with: CGSize(width: frame.size.width, height: .greatestFiniteMagnitude),
options: .usesLineFragmentOrigin,
attributes: [.font: font],
context: nil).size

return labelTextSize.height > bounds.size.height
}
}


swift3:

extension UILabel {

var isTruncated: Bool {

guard let labelText = text else {
return false
}

let labelTextSize = (labelText as NSString).boundingRect(
with: CGSize(width: frame.size.width, height: .greatestFiniteMagnitude),
options: .usesLineFragmentOrigin,
attributes: [NSFontAttributeName: font],
context: nil).size

return labelTextSize.height > bounds.size.height
}
}

swift2:

extension UILabel {

func isTruncated() -> Bool {

if let string = self.text {

let size: CGSize = (string as NSString).boundingRectWithSize(
CGSize(width: self.frame.size.width, height: CGFloat(FLT_MAX)),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: self.font],
context: nil).size

if (size.height > self.bounds.size.height) {
return true
}
}

return false
}

}


Reply

#14
You can calculate the [width of the string][1] and see if the width is greater than `label.bounds.size.width`

[NSString UIKit Additions][2] has several methods for computing the size of the string with a specific font. However, if you have a minimumFontSize for your label that allows the system to shrink the text down to that size. You may want to use [sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:][3] in that case.

CGSize size = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];
if (size.width > label.bounds.size.width) {
...
}


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

:
Reply

#15
Here's the selected answer in Swift 3 (as an extension). The OP was asking about 1 line labels. Many of the swift answers I tried here are specific to multi-line labels and aren't flagging correctly on single line labels.

extension UILabel {
var isTruncated: Bool {
guard let labelText = text as? NSString else {
return false
}
let size = labelText.size(attributes: [NSFontAttributeName: font])
return size.width > self.bounds.width
}
}
Reply

#16


extension UILabel {

public func resizeIfNeeded() -> CGFloat? {
guard let text = text, !text.isEmpty else { return nil }

if isTruncated() {
numberOfLines = 0
sizeToFit()
return frame.height
}
return nil
}

func isTruncated() -> Bool {
guard let text = text, !text.isEmpty else { return false }

let size: CGSize = text.size(withAttributes: [NSAttributedStringKey.font: font])
return size.width > self.bounds.size.width
}
}


You can calculate the width of the string and see if the width is greater than label width.
Reply

#17
I had issues with [`boundingRect(with:options:attributes:context:)`](

[To see links please register here]

) when using autolayout (to set a max height) and an attributed text with [`NSParagraph.lineSpacing`](

[To see links please register here]

)

The spacing between lines was ignored (even when passed in `attributes` to the `boundingRect` method) so the label might be considered as not truncated when it was.

The solution I found is to use [`UIView.sizeThatFits`](

[To see links please register here]

) :

extension UILabel {
var isTruncated: Bool {
layoutIfNeeded()
let heightThatFits = sizeThatFits(bounds.size).height
return heightThatFits > bounds.size.height
}
}
Reply

#18
This is it. This works with `attributedText`, before falling back to plain `text`, which makes a lot of sense for us folks who deal with multiple font families, sizes, and even NSTextAttachments!

Works fine with autolayout, but obviously the constraints must be defined and set before we check `isTruncated`, otherwise the label itself wont even know how to lay itself out, so no way it would even know if its truncated.

It doesnt work to approach this problem with just a plain `NSString` and `sizeThatFits`. Im not sure how people were getting positive results like that. BTW, as mentioned numerous times, using `sizeThatFits` is not ideal at all because it takes into account `numberOfLines` for the resulting size, which defeats the whole purpose of what we are trying to do, because `isTruncated` would always return `false` regardless if its truncated or not.

extension UILabel {
var isTruncated: Bool {
layoutIfNeeded()

let rectBounds = CGSize(width: bounds.width, height: .greatestFiniteMagnitude)
var fullTextHeight: CGFloat?

if attributedText != nil {
fullTextHeight = attributedText?.boundingRect(with: rectBounds, options: .usesLineFragmentOrigin, context: nil).size.height
} else {
fullTextHeight = text?.boundingRect(with: rectBounds, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil).size.height
}

return (fullTextHeight ?? 0) > bounds.size.height
}
}
Reply

#19
Make sure to call either of these in viewDidLayoutSubviews.

public extension UILabel {

var isTextTruncated: Bool {
layoutIfNeeded()
return text?.boundingRect(with: CGSize(width: bounds.width, height: .greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font!], context: nil).size.height ?? 0 > bounds.size.height
}

var isAttributedTextTruncated: Bool {
layoutIfNeeded()
return attributedText?.boundingRect(with: CGSize(width: bounds.width, height: .greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil).size.height ?? 0 > bounds.size.height
}

}
Reply

#20
**SWIFT 5**

Example for a multiple lined UILabel that is set to display only 3 lines.

let labelSize: CGSize = myLabel.text!.size(withAttributes: [.font: UIFont.systemFont(ofSize: 14, weight: .regular)])

if labelSize.width > myLabel.intrinsicContentSize.width * 3 {
// your label will truncate
}


Though the user may select the return key adding an extra line without adding to the "text width" in that case something like this may also be useful.

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

if text == "\n" {
// return pressed

}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through