Swift 在xcode8中的标签中添加上标
我正在创建一个数学应用程序,希望有一个上标2。我一直无法找到一个xcode 8的解决方案,它不是基线偏移Swift 在xcode8中的标签中添加上标,swift,xcode8,Swift,Xcode8,我正在创建一个数学应用程序,希望有一个上标2。我一直无法找到一个xcode 8的解决方案,它不是基线偏移 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 attStr
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解决方案,您可能需要签出。将其导入项目后(例如,通过迦太基-参见自述文件中的说明),您可以执行以下操作:
import HandyUIKit
let someFont = UIFont.systemFont(ofSize: 20, weight: .medium)
"Converted: \(printA)(x+\(printB))^{2}+\(printC)".superscripted(font: someFont)
最后一行将返回一个NSAttributedString
,它看起来与您要查找的内容完全相同。只需将其分配给UILabel
sattributedText
属性,就这样强>
如果你想订阅一个文本,只需使用
subscripted(字体:)
。它将识别像CO{2}
这样的结构。如果您想同时使用和,还有super和subscripted(font:)
有关更多信息和其他示例,请参见。针对Swift4
let font:UIFont? = UIFont(name: "Helvetica", size:20)
let fontSuper:UIFont? = UIFont(name: "Helvetica", size:10)
let attString:NSMutableAttributedString = NSMutableAttributedString(string: "xcode8", attributes: [NSAttributedString.Key.font:font!])
attString.setAttributes([NSAttributedString.Key.font:fontSuper!,NSAttributedString.Key.baselineOffset:10], range: NSRange(location:5,length:1))
labelname.attributedText = attString
你检查过答案了吗?我试过了,有两处错误。上面写着字符串和字体不能在初始化器中使用:(有什么想法吗?谢谢你的帮助!你能告诉我你尝试过的代码哪里有错误吗?我在你答案的第3行和第4行有错误?错误是什么?