如何根据字符串长度得到CGFloat/CGSize?
How to get CGFloat/CGSize based on String length?
我有一个UIView,它作为一个容器,这个容器的宽度需要根据最长的字符串动态变化。所以我想根据字符串长度得到CGFloat/CGSize,是否仍然可以?
似乎最有用的方法现在已贬值...
myStringSize : CGSize = [longestString sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:self.myLabel.lineBreakMode];
与其基于标签,不如基于字符串
您必须提供已知宽度或高度:
extension String {
func getHeight(with width: CGFloat, font: UIFont) -> CGFloat {
let maxSize = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
let actualSize = self.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName: font], context: nil)
return actualSize.height
}
func getWidth(with height: CGFloat, font: UIFont) -> CGFloat {
let maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: height)
let actualSize = self.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName: font], context: nil)
return actualSize.width
}
}
1 - 首先定义你想要的最大尺寸 MaxWidth
CGSize constraint = CGSizeMake(MaxWidth, CGFLOAT_MAX);
2- 您的属性 text
:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont systemFontOfSize:FONT_SIZE], NSFontAttributeName, nil];
3-* 这是如何从 text
得到 paraRect
:
CGRect paraRect = [text boundingRectWithSize:constraint options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil];
4-* 终于得到尺寸:
CGSize size = paraRect.size;
感谢@Venkat 的帮助,这是对我有用的解决方案
@objc func containerWidth() -> (CGFloat){
let containerSize = self.myLabel.intrinsicContentSize
containerWidth = containerSize.width
return containerWidth
}
我有一个UIView,它作为一个容器,这个容器的宽度需要根据最长的字符串动态变化。所以我想根据字符串长度得到CGFloat/CGSize,是否仍然可以?
似乎最有用的方法现在已贬值...
myStringSize : CGSize = [longestString sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:self.myLabel.lineBreakMode];
与其基于标签,不如基于字符串
您必须提供已知宽度或高度:
extension String {
func getHeight(with width: CGFloat, font: UIFont) -> CGFloat {
let maxSize = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
let actualSize = self.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName: font], context: nil)
return actualSize.height
}
func getWidth(with height: CGFloat, font: UIFont) -> CGFloat {
let maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: height)
let actualSize = self.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName: font], context: nil)
return actualSize.width
}
}
1 - 首先定义你想要的最大尺寸 MaxWidth
CGSize constraint = CGSizeMake(MaxWidth, CGFLOAT_MAX);
2- 您的属性 text
:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont systemFontOfSize:FONT_SIZE], NSFontAttributeName, nil];
3-* 这是如何从 text
得到 paraRect
:
CGRect paraRect = [text boundingRectWithSize:constraint options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil];
4-* 终于得到尺寸:
CGSize size = paraRect.size;
感谢@Venkat 的帮助,这是对我有用的解决方案
@objc func containerWidth() -> (CGFloat){
let containerSize = self.myLabel.intrinsicContentSize
containerWidth = containerSize.width
return containerWidth
}