Swift: 给定 UILabel 的宽度拆分字符串
Swift: Split string given width of UILabel
如何根据 UILabel 的宽度拆分字符串。
str = "Here is a sample of a long string that wraps at the end of the screen three times."
width = UIScreen.Main.Bounds.Width
预期结果
strArray = ["Here is a sample of a long", "string that wraps at the end", "of the screen three times."]
看图:
目标是将行提取为字符串,而不是换行。
try below function
func getStringArrayFromLabel(in label: UILabel) -> [String] {
/// An empty string's array
var arrLines = [String]()
guard let text = label.text, let font = label.font else {return arrLines}
let rect = label.frame
let myFont: CTFont = CTFontCreateWithName(font.fontName as CFString, font.pointSize, nil)
let attStr = NSMutableAttributedString(string: text)
attStr.addAttribute(kCTFontAttributeName as NSAttributedString.Key, value: myFont, range: NSRange(location: 0, length: attStr.length))
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 arrLines}
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)
arrLines.append(lineString)
}
return arrLines
}
希望对您有所帮助:
let sampleFont = UIFont.systemFont(ofSize: 15)
let oneLineHeight = NSString(string: "aaa").boundingRect(with: .init(width: 50, height: 10000), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font : sampleFont], context: nil).height
let estimatedHeight = NSString(string: "Here is a sample of a long string that wraps at the end of the screen three times.").boundingRect(with: .init(width: UIScreen.main.bounds.width, height: 10000), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font : sampleFont], context: nil).height
let numOfLines = Int(estimatedHeight / oneLineHeight)
如何根据 UILabel 的宽度拆分字符串。
str = "Here is a sample of a long string that wraps at the end of the screen three times."
width = UIScreen.Main.Bounds.Width
预期结果
strArray = ["Here is a sample of a long", "string that wraps at the end", "of the screen three times."]
看图:
目标是将行提取为字符串,而不是换行。
try below function
func getStringArrayFromLabel(in label: UILabel) -> [String] {
/// An empty string's array
var arrLines = [String]()
guard let text = label.text, let font = label.font else {return arrLines}
let rect = label.frame
let myFont: CTFont = CTFontCreateWithName(font.fontName as CFString, font.pointSize, nil)
let attStr = NSMutableAttributedString(string: text)
attStr.addAttribute(kCTFontAttributeName as NSAttributedString.Key, value: myFont, range: NSRange(location: 0, length: attStr.length))
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 arrLines}
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)
arrLines.append(lineString)
}
return arrLines
}
希望对您有所帮助:
let sampleFont = UIFont.systemFont(ofSize: 15)
let oneLineHeight = NSString(string: "aaa").boundingRect(with: .init(width: 50, height: 10000), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font : sampleFont], context: nil).height
let estimatedHeight = NSString(string: "Here is a sample of a long string that wraps at the end of the screen three times.").boundingRect(with: .init(width: UIScreen.main.bounds.width, height: 10000), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font : sampleFont], context: nil).height
let numOfLines = Int(estimatedHeight / oneLineHeight)