Truncating head of a UILabel with 2 lines - ios

I have a UIlabel which has numberOfLines as 2
and lineBreakMode = Truncate Head
But when i run it
Instead of truncating head on first line like
...1st Line Content
2nd Line Content
It truncates like
1st Line Content
...2nd Line Content
How do i truncate the head of the label in the first line itself?

Try this code tested in Swift 3
let text = "Hello World! Hello World! Hello World! Hello World! "
let attString = NSMutableAttributedString(string: text)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .left
paragraphStyle.firstLineHeadIndent = 0
paragraphStyle.headIndent = 40 // set any vallue
paragraphStyle.lineBreakMode = .byTruncatingHead
attString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attString.length))
attString.addAttribute(NSFontAttributeName, value: UIFont(name: "Arial", size: 25)!, range: NSMakeRange(0, attString.length))
label.attributedText = attString // label is your UILabel
label.numberOfLines = 2
Output 1:
paragraphStyle.firstLineHeadIndent = 40 // set any vallue
paragraphStyle.headIndent = 0
Output 2:
Updated: You can achieve by combain two attribute string
let dotAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont(name: "Arial", size: 25)]
let textAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont(name: "Arial", size: 25)]
let dotString = NSMutableAttributedString(string: ". . . ", attributes: dotAttributes)
let textString = NSMutableAttributedString(string: "Hello World! Hello World! Hello World! Hello World!", attributes: textAttributes)
let totalString = NSMutableAttributedString()
totalString.append(dotString)
totalString.append(textString)
label.numberOfLines = 2
label.attributedText = totalString
Note: You can use paragraphStyle(headIndent) to create left margin.
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .left
paragraphStyle.firstLineHeadIndent = 5
paragraphStyle.headIndent = 5 // set any vallue
paragraphStyle.lineBreakMode = .byTruncatingHead
totalString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, totalString.length))
Output:

Related

Can I change the location of the ellipsis on the uilabel in swift?

I used UiLabel with byTruncatingTail attribute.
I want to fix the ellipsis from the middle to the bottom vertically.
It is now stated as follows.
But I want to have the ellipsis placed on the floor as follows.
My code is as follows.
let paragraphStyle = NSMutableParagraphStyle()
let range = NSRange(location: 0, length: mutableAttributedString.length)
paragraphStyle.lineSpacing = 2
paragraphStyle.alignment = .center
paragraphStyle.lineBreakMode = .byTruncatingTail
mutableAttributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
titleLabel.attributedText = mutableAttributedString
Is this possible?

UILabel text truncation vs. line breaks in text

I have a UILabel that is put as titleView in the navigation bar. I want it to have 2 lines, where the first line can be truncated and the second is center aligned.
In code it looks more less like this:
let label = UILabel()
let text = NSAttributedString(string: "Long long long text\nsecond line")
label.attributedText = text
label.textAlignment = .Center
label.numberOfLines = 0
label.lineBreakMode = .ByTruncatingTail
label.sizeToFit()
self.navigationItem.titleView = label
The effect in case of the first line text is not exceeding available
space is like this:
It's pretty good, but when the first line text is longer than:
let text = NSAttributedString(string: "Very very very very very long text\nsecond line")
I want to achieve like below.
How it can be done? I experimented with numberOfLines and lineBreakMode but it's not worked.
change your line breakmode to ByTruncatingMiddle instead of ByTruncatingTail. Something like below,
label.lineBreakMode = .ByTruncatingMiddle
Hope this will help :)
Navigation Tittle with sub-Tittle (Multiline Navigation Tittle)
Use NSMutableAttributedString with UITextView instead of UILabel
(because, if tittle is large then UILabel lineBreakMode with .byTruncatingTail is not working for first line in UILabel)
func multilineNavigation(title:String,subTitle:String) {
DispatchQueue.main.async {
let titleAttributedStr = NSMutableAttributedString(string: title, attributes: [NSAttributedStringKey.foregroundColor: UIColor.orange,NSAttributedStringKey.font: UIFont(name: "Helvetica Neue", size: 17.0) ?? UIFont()])
let subTitleAttributedStr = NSMutableAttributedString(string: "\n\(subTitle)", attributes: [NSAttributedStringKey.foregroundColor: UIColor.green,NSAttributedStringKey.font: UIFont(name: "Helvetica Neue", size: 12.0) ?? UIFont()])
titleAttributedStr.append(subTitleAttributedStr)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1
paragraphStyle.lineBreakMode = .byTruncatingTail
titleAttributedStr.addAttribute(.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, titleAttributedStr.length))
let textView = UITextView()
textView.attributedText = titleAttributedStr
textView.backgroundColor = .clear
textView.isUserInteractionEnabled = false
textView.textContainerInset = .zero
textView.textAlignment = .center
textView.frame = CGRect(x: 0, y: 0, width: textView.intrinsicContentSize.width, height: 44)
self.navigationItem.titleView = textView
}
}

How to adjust a UILabel line spacing programmatically in Swift?

I have a multiline UILabel as shown here:
I achieved this using the following code:
label.lineBreakMode = .ByWordWrapping
label.numberOfLines = 2
I'm trying to "decrease" the line spacing between the 1st line and 2nd line, and I tried to use the following code:
let text = label.attributedText
let mas = NSMutableAttributedString(attributedString:text!)
mas.replaceCharactersInRange(NSMakeRange(0, mas.string.utf16.count),
withString: label.text!)
label.attributedText = mas
However, it does not seem to work.
Thanks
Programmatically with Swift 4
Using label extension
extension UILabel {
// Pass value for any one of both parameters and see result
func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) {
guard let labelText = self.text else { return }
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing
paragraphStyle.lineHeightMultiple = lineHeightMultiple
let attributedString:NSMutableAttributedString
if let labelattributedText = self.attributedText {
attributedString = NSMutableAttributedString(attributedString: labelattributedText)
} else {
attributedString = NSMutableAttributedString(string: labelText)
}
// Line spacing attribute
attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
self.attributedText = attributedString
}
}
Now call extension function
let label = UILabel()
let stringValue = "How\nto\nadjust\na\nUILabel\nline\nspacing\nprogrammatically\nin\nSwift"
// Pass value for any one argument - lineSpacing or lineHeightMultiple
label.setLineSpacing(lineSpacing: 2.0) . // try values 1.0 to 5.0
// or try lineHeightMultiple
//label.setLineSpacing(lineHeightMultiple = 2.0) // try values 0.5 to 2.0
Or using label instance (Just copy & execute this code to see result)
let label = UILabel()
let stringValue = "How\nto\nadjust\na\nUILabel\nline\nspacing\nprogrammatically\nin\nSwift"
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
// Line spacing attribute
attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count))
// Character spacing attribute
attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length))
label.attributedText = attrString
Swift 3
let label = UILabel()
let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel"
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count))
label.attributedText = attrString
From Interface Builder:
You're on the right track with NSAttributedString. You need to set the line spacing of the paragraph style:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 30 // Whatever line spacing you want in points
attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
label.attributedText = attributedString;
Do this in the storyboard.....
func updateLabel(with title: String) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 0.8
paragraphStyle.alignment = .center
let string = NSAttributedString(string: title, attributes: [.paragraphStyle: paragraphStyle])
label.attributedText = string
}

Center justify UILabel text?

I'm trying to center-justify the text of my UILabel like the picture :
Sample of my code :
myLabel.textAlignment = .Justified
myLabel.lineBreakMode = .ByWordWrapping
The result I have :
Did I miss something ?
I found the answer there : https://stackoverflow.com/a/27548566/833816
It works by adding paragraphStyle.firstLineHeadIndent = 0.001
Full sample:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Justified
paragraphStyle.firstLineHeadIndent = 0.001
let mutableAttrStr = NSMutableAttributedString(attributedString: detailsLabel.attributedText)
mutableAttrStr.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, mutableAttrStr.length))
myLabel.attributedText = mutableAttrStr
Try
myLabel.textAlignment = NSTextAlignment.Center

Attributed Text Center Alignment

I have tried everything but cannot seem to center this text. Can someone please tell me where the error is.
NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
paragraphStyle.alignment = NSTextAlignmentCenter;
label.attributedText = [[NSAttributedString alloc] initWithString:cell.EventTitle.text attributes:#{NSForegroundColorAttributeName : [UIColor whiteColor],NSParagraphStyleAttributeName:paragraphStyle,NSBaselineOffsetAttributeName : #0,NSFontAttributeName : [UIFont fontWithName:#"BrandonGrotesque-Black" size:34]}];
In Swift 5
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
textView.attributedText = NSAttributedString(string: "String",
attributes: [.paragraphStyle: paragraph])
In Swift-4
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.paragraphStyle: paragraph]
let attrString = NSAttributedString(string:"string", attributes: attributes)
textView.attributedText = attrString
In Swift-3
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let attributes: [String : Any] = [NSParagraphStyleAttributeName: paragraph]
let attrString = NSAttributedString(string:"string", attributes: attributes)
textView.attributedText = attrString
You can set the center alignment using this. Remember to set range.
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
In Swift 4
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
textView.attributedText = NSAttributedString(string: "string",
attributes: [.paragraphStyle: paragraph])
Another way:
Swift:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributedString = NSAttributedString(string: "This will be centered.", attributes: [ NSAttributedString.Key.paragraphStyle: paragraphStyle])
Obj-C:
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSAttributedString *attributedString = [NSAttributedString.alloc initWithString:#"This will be centered."
attributes: #{NSParagraphStyleAttributeName:paragraphStyle}];
Swift 4+
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
// Swift 4.2++
let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedString.Key.paragraphStyle:paragraphStyle])
// Swift 4.1--
let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedStringKey.paragraphStyle:paragraphStyle])
let yourLabel = UILabel()
yourLabel.attributedText = attributedString
Objective-C
NSString *string = #"Your String";
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string attributes: #{NSParagraphStyleAttributeName:paragraphStyle}];
UILabel *label = [[UILabel alloc] init];
label.attributedText = attributedString;
In Swift
let titleString = "title here"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Center
let attributedString = NSAttributedString(
string: titleString,
attributes: [NSParagraphStyleAttributeName: paragraphStyle]
)
titleAttributedLabel.attributedText = attributedString
Swift4
let attributedString = NSMutableAttributedString(string: "Example text that is centered using a paragraph style. With the ability to change the width between lines.", attributes: [NSAttributedStringKey.font: GothamFont.medium(with: 14)])
let myParagraphStyle = NSMutableParagraphStyle()
myParagraphStyle.alignment = .center // center the text
myParagraphStyle.lineSpacing = 14 //Change spacing between lines
myParagraphStyle.paragraphSpacing = 38 //Change space between paragraphs
attributedString.addAttributes([.paragraphStyle: myParagraphStyle], range: NSRange(location: 0, length: attributedString.length))
helper method based on the helpful answers above
public extension NSAttributedString
{
var centered: NSAttributedString
{
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let m = NSMutableAttributedString(attributedString: self)
m.addAttribute(.paragraphStyle, value: paragraph, range: NSMakeRange(0, length))
return m
}
}
in case you want the Is dotted and Ts crossed el verbositas version
var centered: NSAttributedString {
let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
let attributedString = NSMutableAttributedString(attributedString: self)
attributedString.addAttributes([NSAttributedString.Key.paragraphStyle : paragraphStyle],
range: NSRange(location: 0, length: attributedString.length))
return attributedString
}
To do it in Swift 2.x
let attributeString = NSMutableAttributedString(string: "text")
style.alignment = .Center
attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: range)
Sometimes when text is in Arabic or other right align languages then when doing alignment Justified, last line text ends at left side. for this we can add baseWritingDirection below is sample code
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .justified
paragraphStyle.baseWritingDirection = .rightToLeft
attribute.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:range)
txtView.attributedText = attribute
Set line breakmode, if you set attributed text on UIButton.
Swift 5
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
paragraph.lineBreakMode = .byClipping
Objective-C
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = NSTextAlignmentCenter;
style.lineBreakMode = NSLineBreakByClipping;
This works for me
label.textAlignment = .center

Resources