let strNo = "2222555" // size 18, this should be bold
let remainingStr = "Call to" + "\(strNo)" + "Number" 现在,在我的UIButton中,比如说按钮,如何设置这个标题“调用2222555号码”,我需要根据设备更改大小,所以我必须通过编码来实现。
更新
我需要像下面这样的图像。

并且需要通过编码来改变标题的大小。上面的屏幕截图来自iPhone 7,在iPhone 5中它变得更大,在iPad中它变得更小,所以我必须根据需要设置标题的大小。
任何帮助都是有价值的。
谢谢
发布于 2017-08-04 07:07:21
我通过几天的努力找到了答案。而且这很容易。
guard
let font1 = UIFont(name: "HelveticaNeue-Bold", size: 22),
let font2 = UIFont(name: "HelveticaNeue-Medium", size: 22) else { return }
let dict1:[String:Any] = [
// NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle.rawValue,
NSFontAttributeName:font2,
NSParagraphStyleAttributeName:style,
NSForegroundColorAttributeName: UIColor.white
]
let dict2:[String:Any] = [
// NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
NSFontAttributeName:font1,
NSParagraphStyleAttributeName:style,
NSForegroundColorAttributeName: UIColor.white,
// NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20)
]
let dict3:[String:Any] = [
// NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
NSFontAttributeName:font2,
NSParagraphStyleAttributeName:style,
NSForegroundColorAttributeName: UIColor.white
]
let dict4:[String:Any] = [
// NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
NSFontAttributeName:font1,
NSParagraphStyleAttributeName:style,
NSForegroundColorAttributeName: UIColor.white
]
let attString = NSMutableAttributedString()
attString.append(NSAttributedString(string: "Call to", attributes: dict1))
attString.append(NSAttributedString(string: " 2222555 ", attributes: dict2))
attString.append(NSAttributedString(string: " To Book Your Skip ", attributes: dict3))
attString.append(NSAttributedString(string: "NOW", attributes: dict4))
btnCall.setAttributedTitle(attString, for: .normal)和预期一样完美地运作。
https://stackoverflow.com/questions/45431347
复制相似问题