国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

swift-自動計算字符串的寬高

2019-11-14 17:59:28
字體:
來源:轉載
供稿:網友

寫一個方法來繼承String

//自動控制文字換行及寬度

extension String {

    func textSizeWithFont(font: UIFont, constrainedToSize size:CGSize) -> CGSize {

        var textSize:CGSize!

        if CGSizeEqualToSize(size, CGSizeZero) {

            let attributes = NSDictionary(object: font, forKey: NSFontAttributeName)

            textSize = self.sizeWithAttributes(attributes as! [String : AnyObject] as [String : AnyObject])

        } else {

            let option = NSStringDrawingOptions.UsesLineFragmentOrigin

            let attributes = NSDictionary(object: font, forKey: NSFontAttributeName)

            let stringRect = self.boundingRectWithSize(size, options: option, attributes: attributes as! [String : AnyObject] as [String : AnyObject], context: nil)

            textSize = stringRect.size

        }

        return textSize

    }

}

用法:

   let PRojectText="我是一段字符串,來計算我的高度吧";

        let projectSize=projectText.textSizeWithFont(UIFont.systemFontOfSize(14), constrainedToSize:CGSizeMake(100, 200))

        let comProjectW:CGFloat=projectSize.width;

        let comProjectH:CGFloat=projectSize.height;

  //記得要在計算的字符串UILable中加上

  UIlable.font=UIFont.systemFontOfSize(14);

  //顯示幾行

     UIlable.numberOfLines=1;

  

  UIlable.frame = CGRectMake(50, 50, comProjectW, comProjectH);

 

 

 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贺兰县| 景泰县| 大化| 仪陇县| 垦利县| 淮滨县| 吉林省| 精河县| 台湾省| 连山| 宜章县| 浦城县| 浦江县| 鱼台县| 福安市| 磴口县| 漳平市| 灯塔市| 东阿县| 稻城县| 十堰市| 嵊泗县| 莱芜市| 乌拉特后旗| 巧家县| 德安县| 宝鸡市| 大荔县| 楚雄市| 涞水县| 湄潭县| 和平区| 宁武县| 历史| 抚松县| 蓬溪县| 静海县| 温州市| 微博| 观塘区| 瓦房店市|