Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add hyphens on word break in a UILabel

How do I set a UILabel lineBreakMode to break words and add hyphens to broken words?

a label with a broken wo-

rd should look like this

like image 970
David Ben Ari Avatar asked Sep 24 '13 10:09

David Ben Ari


3 Answers

Elaborating on Matt's answer here: https://stackoverflow.com/a/16502598/196358 it can be done using NSAttributedString and NSParagraphStyle. See below:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.hyphenationFactor = 1.0f;
    
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleString attributes:@{ NSParagraphStyleAttributeName : paragraphStyle }];
    
self.titleLabel.attributedText = attributedString;

This will cause the label to break at logical places mid-word using hyphens. It looks great, and is pretty simple to do. It requires iOS 6.0, but I've only tried it under 7.0.

WARNING: Font, Color, Alignment --- basically nothing is inherited by attributed-text's render from it's hosting and/or owner UILabel (you need to add those attributes manually).

like image 52
Kenny Winker Avatar answered Nov 18 '22 13:11

Kenny Winker


Swift 5.0

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor = 1.0

let hyphenAttribute = [
    NSAttributedString.Key.paragraphStyle : paragraphStyle,
] as [NSAttributedString.Key : Any]

let attributedString = NSMutableAttributedString(string: "Your String", attributes: hyphenAttribute)
self.yourLabel.attributedText = attributedString

Swift 4.0

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor = 1.0

let hyphenAttribute = [
    NSAttributedStringKey.paragraphStyle : paragraphStyle,
    ] as [NSAttributedStringKey : Any]

let attributedString = NSMutableAttributedString(string: "Your String", attributes: hyphenAttribute)
self.yourLabel.attributedText = attributedString

Swift 3.0

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor = 1.0
let attributedString = NSMutableAttributedString(string: “Your String”, attributes: [NSParagraphStyleAttributeName:paragraphStyle])
self.yourLabel.attributedText = attributedString

From Storyboard

enter image description here

like image 44
Krunal Avatar answered Nov 18 '22 13:11

Krunal


Sometimes it's crucial to add a locale attribute key.

NSString *lorem = @"Lorem ipsum <et cetera>.";

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.hyphenationFactor = 1;
paragraph.alignment = NSTextAlignmentJustified;
paragraph.lineBreakMode = NSLineBreakByWordWrapping;

self.label.attributedText = [[NSAttributedString alloc] initWithString:lorem attributes:@{
    NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody],
    NSForegroundColorAttributeName: [UIColor darkGrayColor],
    NSParagraphStyleAttributeName: paragraph,
    @"locale": @"la", // Latin, use @"en-US" for American English, for example.
}];
like image 5
bteapot Avatar answered Nov 18 '22 14:11

bteapot