我收到了错误......
Could not find an overload for 'init' that accepts the supplied arguments
......当我尝试使用时......
extension UIFont {
func sizeOfString (string: String, constrainedToWidth width: Double) -> CGSize {
NSString(string).boundingRectWithSize(CGSize(width, DBL_MAX),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: self],
context: nil).size
}
}
NSString
不再支持此方法了,还是我搞砸了语法?
答案 0 :(得分:71)
初始化程序期望命名参数。
extension UIFont {
func sizeOfString (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
}
}
注意:String
可以投射到NSString
s。
extension UIFont {
func sizeOfString (string: String, constrainedToWidth width: Double) -> CGSize {
return (string as NSString).boundingRectWithSize(CGSize(width: width, height: DBL_MAX),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: self],
context: nil).size
}
}
或
extension UIFont {
func sizeOfString (string: NSString, constrainedToWidth width: Double) -> CGSize {
return string.boundingRectWithSize(CGSize(width: width, height: DBL_MAX),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: self],
context: nil).size
}
}
-
<强>已更新强>
对于Swift 4语法
extension UIFont {
func sizeOfString (string: String, constrainedToWidth width: Double) -> CGSize {
return NSString(string: string).boundingRect(
with: CGSize(width: width, height: .greatestFiniteMagnitude),
options: .usesLineFragmentOrigin,
attributes: [.font: self],
context: nil).size
}
}
答案 1 :(得分:4)
或者你可以把它转换成NSString
if let ns_str:NSString = str as NSString? {
let sizeOfString = ns_str.boundingRectWithSize(
CGSizeMake(self.titleLabel.frame.size.width, CGFloat.infinity),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: lbl.font],
context: nil).size
}
答案 2 :(得分:4)
最新的Swift
<!DOCTYPE html>
<html>
<head>
<base href="http://demos.telerik.com/kendo-ui/dropdownlist/angular">
<style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style>
<title></title>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css" />
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.1.118/styles/kendo.blueopal.min.css" />
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.1.118/styles/kendo.blueopal.mobile.min.css" />
<script src="https://kendo.cdn.telerik.com/2017.1.118/js/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2017.1.118/js/angular.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>
</head>
<body>
<div id="example" ng-app="KendoDemos">
<div class="demo-section k-content" ng-controller="MyCtrl">
<h4>Static data</h4>
<select kendo-drop-down-list="mydropdown">
<option>Albania</option>
<option>Andorra</option>
<option>Armenia</option>
<option>Austria</option>
</select>
</div>
</div>
<script>
var app = angular.module("KendoDemos", [ "kendo.directives" ])
.controller("MyCtrl", function($scope){
$scope.$on("kendoWidgetCreated", function(event, widget){
if (widget === $scope.mydropdown) {
widget.wrapper.on("focus", $scope.onFocus);
widget.wrapper.on("blur", $scope.onBlur);
}
});
$scope.onFocus = function(){
console.log("Kendo DropDownList focused!");
};
$scope.onBlur = function(){
console.log("Kendo DropDownList blured!");
};
});
</script>
答案 3 :(得分:1)
最新的斯威夫特:
import UIKit
extension UIFont {
func sizeOfString(string: String, constrainedToWidth width: Double) -> CGSize {
return NSString(string: string).boundingRect(with: CGSize(width: width, height: .greatestFiniteMagnitude),
options: .usesLineFragmentOrigin,
attributes: [NSFontAttributeName: self],
context: nil).size
}
}
答案 4 :(得分:0)
您可以使用objective-c桥接
let lblRegisterlinkWidth: CGFloat = lblRegisterLink.text!._bridgeToObjectiveC().boundingRectWithSize(lblRegisterLink.frame.size, options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName : lblRegisterLink.font], context: nil).size.width
答案 5 :(得分:0)
Swift 4.1版本
extension UIFont {
func sizeOfString (string: String, constrainedToWidth width: Double) -> CGSize {
return NSString(string: string).boundingRect(with: CGSize(width: width, height: .greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: self], context: nil).size
}
}
答案 6 :(得分:0)
最新的Swift 5:
extension UIFont {
func size(OfString string: String, constrainedToWidth width: Double) -> CGSize {
return NSString(string: string).boundingRect(with: CGSize(width: width, height: .greatestFiniteMagnitude),
options: .usesLineFragmentOrigin,
attributes: [.font: self],
context: nil).size
}
}
答案 7 :(得分:0)
快速5
extension String {
func sizeOfString(maxWidth:CGFloat, font: UIFont) -> CGSize {
let tmp = NSMutableAttributedString(string: self, attributes:[NSAttributedString.Key.font:font])
let limitSize = CGSize(width: maxWidth, height: CGFloat(MAXFLOAT))
let contentSize = tmp.boundingRect(with: limitSize, options: .usesLineFragmentOrigin, context: nil)
return contentSize.size
}
}