Center justify UILabel text? - ios

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

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?

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
}

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

How do you programmatically center the alignment of a text label for iOS?

I want to set the alignment of a text label, how can I do that?
I think there are the answers who helped you out. The correct way to do this is:
yourLabelName.textAlignment = NSTextAlignmentCenter;
for more documentation you can read this:
https://developer.apple.com/documentation/uikit/uilabel
In Swift :-
yourLabelName.textAlignment = .center
Here .center is NSTextAlignment.center
Here you are,
yourLabel.textAlignment = UITextAlignmentCenter
EDIT
if you target above iOS6 use NSTextAlignmentCenter as UITextAlignmentCenter is depreciated
Hope it helps.
This has changed as of iOS 6.0, UITextAlignment has been deprecated. The correct way to do this now is:
yourLabel.textAlignment = NSTextAlignmentCenter;
Here is the NSTextAlignment enumerable that gives the options for text alignment:
Objective-C:
enum {
NSTextAlignmentLeft = 0,
NSTextAlignmentCenter = 1,
NSTextAlignmentRight = 2,
NSTextAlignmentJustified = 3,
NSTextAlignmentNatural = 4,
};
typedef NSInteger NSTextAlignment;
Swift:
enum NSTextAlignment : Int {
case Left
case Center
case Right
case Justified
case Natural
}
Source
label.textAlignment = NSTextAlignmentCenter;
see UILabel documentation
If you have a multiline UILabel you should use a NSMutableParagraphStyle
yourLabelName.numberOfLines = 0
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Center
let attributes : [String : AnyObject] = [NSFontAttributeName : UIFont(name: "HelveticaNeue", size: 15)!, NSParagraphStyleAttributeName: paragraphStyle]
let attributedText = NSAttributedString.init(string: subTitleText, attributes: attributes)
yourLabelName.attributedText = attributedText
In Swift 3 and beyond it should be
yourlabel.textAlignment = NSTextAlignment.center
in swift 4:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
// in Swift 4.2
let attributedString = NSMutableAttributedString(string: "Your String", attributes:[NSAttributedString.Key.paragraphStyle:paragraphStyle])
// in Swift 4.1--
let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedStringKey.paragraphStyle:paragraphStyle])
let yourLabel = UILabel()
yourLabel.attributedText = attributedString
Yourlabel.textAlignment = UITextAlignmentCenter;

Truncating head of a UILabel with 2 lines

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:

Resources