gpt4 book ai didi

ios - 如何从按钮中的第一行和第二行标题中获取文本

转载 作者:行者123 更新时间:2023-11-28 06:41:49 29 4
gpt4 key购买 nike

如何从按钮的第一行和第二行获取文本?以及如何获得按钮标题中使用了多少行?

我创建了一个按钮:

let button = UIButton(type: UIButtonType.Custom) as UIButton
button.frame = CGRectMake(15, 30, 150, 30)
button.setTitle(title, forState: .Normal)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;

然后我想获取按钮用于显示标题的行数。我想获取按钮标题第一行的文本。

最佳答案

通过一些计算,您可以获得UIButton 标题的行数并获得第一行字符串。我想创建一些扩展以使代码“最便携”

从您的代码开始:

let button = UIButton(type: UIButtonType.Custom) as UIButton
let title = "Lorem ipsum dolor sit amet, legimus tacimates eam in. Inani petentium iudicabit nam ut, verear nostrud in sea. Everti repudiare comprehensam et has"
button.frame = CGRectMake(15, 150, 150, 30)
button.setTitle(title, forState: .Normal)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
button.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
self.view.addSubview(button)

enter image description here

因此,首先添加这些扩展:

extension UIFont {
func sizeOfString(string:String) -> CGSize {
return (string as NSString).sizeWithAttributes([NSFontAttributeName:self])
}
func sizeOfStringConstrained(string: String, constrainedToWidth width: Double) -> CGSize {
return NSString(string: string).boundingRectWithSize(CGSize(width: width, height: DBL_MAX),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: self],
context: nil).size
}
}

extension UIButton {
func getLinesArrayOfString() -> [String] {

let text:NSString = (self.titleLabel?.text)!
let font:UIFont = self.titleLabel!.font
let rect:CGRect = self.frame

let myFont:CTFontRef = CTFontCreateWithName(font.fontName, font.pointSize, nil)
let attStr:NSMutableAttributedString = NSMutableAttributedString(string: text as String)
attStr.addAttribute(String(kCTFontAttributeName), value:myFont, range: NSMakeRange(0, attStr.length))
let frameSetter:CTFramesetterRef = CTFramesetterCreateWithAttributedString(attStr as CFAttributedStringRef)
let path:CGMutablePathRef = CGPathCreateMutable()
CGPathAddRect(path, nil, CGRectMake(0, 0, rect.size.width, 100000))
let frame:CTFrameRef = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
let lines = CTFrameGetLines(frame) as NSArray
var linesArray = [String]()

for line in lines {
let lineRange = CTLineGetStringRange(line as! CTLine)
let range:NSRange = NSMakeRange(lineRange.location, lineRange.length)
let lineString = text.substringWithRange(range)
linesArray.append(lineString as String)
}
return linesArray
}
}

然后,编写此代码以获得结果:

let font = button.titleLabel?.font
let sizeString = font?.sizeOfString(title)
let sizeStringConstrained = font?.sizeOfStringConstrained(title, constrainedToWidth: Double(button.frame.width))
let division = (sizeStringConstrained?.height)!/(sizeString?.height)!
let numLines = Int(ceil(division))
print("Number of lines used in my button title: \(numLines)")

let array = button.getLinesArrayOfString()
print("The first line of my button title is: \(array.first)")

enter image description here

关于ios - 如何从按钮中的第一行和第二行标题中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37770814/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com