Ios 避免换行符的多行UILabel中的单个字符

Ios 避免换行符的多行UILabel中的单个字符,ios,swift,uilabel,Ios,Swift,Uilabel,我有一个(动态大小)UILabel,numberOfLines=2 根据来自BE的文本,文本是否被很好地包装 关于如何防止(长的)单词被分成两行,并且新行中只有一个字符,有人有什么建议吗 当前行为: Thisisalongwor d Thisisalong word 通缉行为: Thisisalongwor d Thisisalong word 基本上:我想设置每行的最小字符数(将项目从第一行包装到第二行时) 谢谢 这里有一种方法 使用CoreText函数从标签中获取一个换行数组。如果最

我有一个(动态大小)UILabel,numberOfLines=2

根据来自BE的文本,文本是否被很好地包装

关于如何防止(长的)单词被分成两行,并且新行中只有一个字符,有人有什么建议吗

当前行为:

Thisisalongwor
d
Thisisalong
word
通缉行为:

Thisisalongwor
d
Thisisalong
word
基本上:我想设置每行的最小字符数(将项目从第一行包装到第二行时)


谢谢

这里有一种方法

使用CoreText函数从标签中获取一个换行数组。如果最后一行至少有1个字符,但少于4个字符,且全文大于4个字符,请从文本末尾插入换行符4个字符并更新标签

因此,基于默认的
UILabel
-17磅系统字体,固定宽度为
123磅
,包装设置为
字符包装
,看起来如下所示:

运行
fixLabelWrap(…)
函数后,它看起来如下所示:

示例代码:

class CharWrapViewController: UIViewController {

    @IBOutlet var theLabel: UILabel!

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        fixLabelWrap(theLabel)
    }

    func fixLabelWrap(_ label: UILabel) -> Void {

        // get the text from the label
        var text = theLabel.text ?? ""

        // get an array of the char-wrapped text
        let lines = getLinesArrayOfString(in: theLabel)

        // if it is more than one line
        if lines.count > 1 {
            // get the last line
            if let lastLine = lines.last {
                // if the last line has at least 1 char, is less than 4 chars, and
                // the full text is greater than 4 chars
                if lastLine.count > 0 && lastLine.count < 4 && text.count > 4 {
                    // insert a line-feed 4 chars before the end
                    text.insert("\n", at: text.index(text.endIndex, offsetBy: -4))
                    // update the text in the label
                    theLabel.text = text
                }
            }
        }

    }

    func getLinesArrayOfString(in label: UILabel) -> [String] {

        /// An empty string's array
        var linesArray = [String]()

        guard let text = label.text, let attStr = label.attributedText else { return linesArray }

        let rect = label.frame

        let frameSetter: CTFramesetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
        let path: CGMutablePath = CGMutablePath()
        path.addRect(CGRect(x: 0, y: 0, width: rect.size.width, height: 100000), transform: .identity)

        let frame: CTFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
        guard let lines = CTFrameGetLines(frame) as? [Any] else {return linesArray}

        for line in lines {
            let lineRef = line as! CTLine
            let lineRange: CFRange = CTLineGetStringRange(lineRef)
            let range = NSRange(location: lineRange.location, length: lineRange.length)
            let lineString: String = (text as NSString).substring(with: range)
            linesArray.append(lineString)
        }

        return linesArray
    }

}
class CharWrapViewController:UIViewController{
@IBVAR标签:UILabel!
重写func viewdilayoutsubviews(){
super.viewDidLayoutSubviews()
固定包装(标签)
}
func fixLabelWrap(u标签:UILabel)->Void{
//从标签中获取文本
var text=label.text??“”
//获取字符包装文本的数组
让lines=GetLinesArrayOfsString(在:标签中)
//如果它不止一行
如果lines.count>1{
//拿到最后一行
如果让lastLine=lines.last{
//如果最后一行至少有1个字符,则小于4个字符,并且
//全文大于4个字符
如果lastLine.count>0&&lastLine.count<4&&text.count>4{
//在结束前插入一个换行符4个字符
text.insert(“\n”,位于:text.index(text.endIndex,offsetBy:-4))
//更新标签中的文本
theLabel.text=文本
}
}
}
}
func GetLinesArrayOfsString(在标签中:UILabel)->[字符串]{
///空字符串的数组
var linesArray=[String]()
guard let text=label.text,let attStr=label.attributedText else{return linesArray}
设rect=label.frame
让frameSetter:CTFramesetter=CTFramesetterCreateWithAttributedString(ATTSR作为CFAttributedString)
let path:CGMutablePath=CGMutablePath()
addRect(CGRect(x:0,y:0,width:rect.size.width,height:100000),transform:.identity)
let frame:CTFrame=CTFramesetterCreateFrame(frameSetter,CFRangeMake(0,0),path,nil)
guard let lines=CTFrameGetLines(帧)as?[任何]其他{return linesArray}
排队{
让lineRef=line作为!CTLine
让lineRange:CFRange=CTLineGetStringRange(lineRef)
让range=NSRange(位置:lineRange.location,长度:lineRange.length)
让lineString:String=(文本为NSString)。子字符串(带:范围)
linesArray.append(lineString)
}
回程线射线
}
}

注意:
getLinesArrayOfString(…)
函数是此处帖子的一个稍加修改的版本:

可能会获得一些帮助如果只有1个(长)单词,则换行将没有帮助:(