6

Is there a way of orthographic hyphenation word?

Something like this:

NSStiring *string = @"In June 2010 at the World Wide Developers Conference, Apple announced version 4 of Xcode during the Developer Tools State of the Union address.";
UILabel *label = [[UILabel alloc] init];
label.text = string;

And if frame UILabel will be narrow, get next:

In June 2010 at
the World Wide
Developers Con-
ference, Apple
announced ver-
sion 4 of Xcode
during the Devel-
oper Tools State
of the Union ad-
dress.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Dickordia
  • 109
  • 1
  • 9

3 Answers3

19

Use an NSAttributedString with an NSParagraphStyle with a hyphenationFactor of 1.

matt
  • 515,959
  • 87
  • 875
  • 1,141
5

Objective-C

NSString *string = @"In June 2010 at the World Wide Developers Conference, Apple announced version 4 of Xcode during the Developer Tools State of the Union address.";
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.hyphenationFactor = 1.0;
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string];
UILabel *label = [[UILabel alloc] init];
label.attributedText = attributedString;

Swift 4

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

Swift 3

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor = 1.0
let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSParagraphStyleAttributeName:paragraphStyle])
self.yourLabel.attributedText = attributedString
Krunal
  • 77,632
  • 48
  • 245
  • 261
0

This will help you. Please Change Uilabel Text plain to Attributed I tried Xcode 7+enter image description here

Binoy jose
  • 461
  • 4
  • 9