2016-06-12 5 views
1

ボタンの1行目と2行目のテキストをどのように取得できますか?ボタンのタイトルで使用される線の数はどのように取得できますか?ボタンの1行目と2行目のテキストを取得する方法

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; 

をそして私は、ボタンがタイトルを表示するために使用する行数を取得したい:

は私がボタンを作成します。ボタンのタイトルの最初の行のテキストを取得したいと思います。

答えて

1

一部の計算では、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

+1

はどうもありがとうございました!!! – Dim

関連する問題