Orthographic hyphenation word in IOS

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 06:00:37

问题


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.


回答1:


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




回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/16502556/orthographic-hyphenation-word-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!