把一个标在标签中xcode8
问题描述:
我创建一个应用数学和想有一个标2.我一直无法找到的Xcode 8的解决方案是不是一个基线关闭设置把一个标在标签中xcode8
finalFunc.text = "Converted: \(printA)(x+\(printB))^2+\(printC)"
答
使用此代码 -
let font:UIFont? = UIFont(name: "Helvetica", size:20)
let fontSuper:UIFont? = UIFont(name: "Helvetica", size:10)
let attString:NSMutableAttributedString = NSMutableAttributedString(string: "xcode8", attributes: [NSFontAttributeName:font!])
attString.setAttributes([NSFontAttributeName:fontSuper!,NSBaselineOffsetAttributeName:10], range: NSRange(location:5,length:1))
label1.attributedText = attString;
输出 -
答
对于简单易用的Swift解决方案,您可能需要结账HandyUIKit。将它导入到你的项目后(例如,通过迦太基 - 见README说明),你可以做这样的事情:
import HandyUIKit
let someFont = UIFont.systemFont(ofSize: 20, weight: .medium)
"Converted: \(printA)(x+\(printB))^{2}+\(printC)".superscripted(font: someFont)
最后一行将返回一个NSAttributedString
将看起来就像你正在寻找 。只需将它分配到UILabel
s attributedText
财产和就是这样!
如果您正在寻找下标文本,只需使用subscripted(font:)
代替。它将识别像CO_{2}
这样的结构。还有superAndSubscripted(font:)
,如果你想结合都。
有关更多信息和其他示例,请参阅docs。
你检查了答案吗? –
我已经尝试过了,我得到2个错误。它说预计减速attString和该字体不能在初始化程序中使用:(任何想法?并感谢您的帮助! –
你可以显示你的代码在哪里得到错误? –